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