Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Views / EnableCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Views\EnableCommand.
6  */
7
8 namespace Drupal\Console\Command\Views;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Config\Definition\Exception\Exception;
14 use Symfony\Component\Console\Command\Command;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Core\Entity\EntityTypeManagerInterface;
17 use Drupal\Core\Entity\Query\QueryFactory;
18 use Drupal\Console\Core\Style\DrupalStyle;
19
20 /**
21  * Class EnableCommand
22  *
23  * @package Drupal\Console\Command\Views
24  */
25 class EnableCommand extends Command
26 {
27     use CommandTrait;
28
29     /**
30      * @var EntityTypeManagerInterface
31      */
32     protected $entityTypeManager;
33
34     /**
35      * @var QueryFactory
36      */
37     protected $entityQuery;
38
39     /**
40      * EnableCommand constructor.
41      *
42      * @param EntityTypeManagerInterface $entityTypeManager
43      * @param QueryFactory               $entityQuery
44      */
45     public function __construct(
46         EntityTypeManagerInterface $entityTypeManager,
47         QueryFactory $entityQuery
48     ) {
49         $this->entityTypeManager = $entityTypeManager;
50         $this->entityQuery = $entityQuery;
51         parent::__construct();
52     }
53
54     /**
55      * {@inheritdoc}
56      */
57     protected function configure()
58     {
59         $this
60             ->setName('views:enable')
61             ->setDescription($this->trans('commands.views.enable.description'))
62             ->addArgument(
63                 'view-id',
64                 InputArgument::OPTIONAL,
65                 $this->trans('commands.views.debug.arguments.view-id')
66             );
67     }
68
69     /**
70      * {@inheritdoc}
71      */
72     protected function interact(InputInterface $input, OutputInterface $output)
73     {
74         $io = new DrupalStyle($input, $output);
75         $viewId = $input->getArgument('view-id');
76         if (!$viewId) {
77             $views = $this->entityQuery
78                 ->get('view')
79                 ->condition('status', 0)
80                 ->execute();
81             $viewId = $io->choiceNoList(
82                 $this->trans('commands.views.debug.arguments.view-id'),
83                 $views
84             );
85             $input->setArgument('view-id', $viewId);
86         }
87     }
88
89     /**
90      * {@inheritdoc}
91      */
92     protected function execute(InputInterface $input, OutputInterface $output)
93     {
94         $io = new DrupalStyle($input, $output);
95         $viewId = $input->getArgument('view-id');
96
97         $view = $this->entityTypeManager->getStorage('view')->load($viewId);
98
99         if (empty($view)) {
100             $io->error(
101                 sprintf(
102                     $this->trans('commands.views.debug.messages.not-found'),
103                     $viewId
104                 )
105             );
106             return 1;
107         }
108
109         try {
110             $view->enable()->save();
111             $io->success(
112                 sprintf(
113                     $this->trans('commands.views.enable.messages.enabled-successfully'),
114                     $view->get('label')
115                 )
116             );
117         } catch (Exception $e) {
118             $io->error($e->getMessage());
119
120             return 1;
121         }
122
123         return 0;
124     }
125 }