Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Views / DebugCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Views\DebugCommand.
6  */
7
8 namespace Drupal\Console\Command\Views;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Console\Command\Command;
15 use Drupal\views\Entity\View;
16 use Drupal\Console\Core\Command\Shared\CommandTrait;
17 use Drupal\Core\Entity\EntityTypeManagerInterface;
18 use Drupal\Console\Core\Style\DrupalStyle;
19
20 /**
21  * Class DebugCommand
22  *
23  * @package Drupal\Console\Command\Views
24  */
25 class DebugCommand extends Command
26 {
27     use CommandTrait;
28
29     /**
30      * @var EntityTypeManagerInterface
31      */
32     protected $entityTypeManager;
33
34     /**
35      * DebugCommand constructor.
36      *
37      * @param EntityTypeManagerInterface $entityTypeManager
38      */
39     public function __construct(EntityTypeManagerInterface $entityTypeManager)
40     {
41         $this->entityTypeManager = $entityTypeManager;
42         parent::__construct();
43     }
44
45     /**
46      * {@inheritdoc}
47      */
48     protected function configure()
49     {
50         $this
51             ->setName('views:debug')
52             ->setDescription($this->trans('commands.views.debug.description'))
53             ->addArgument(
54                 'view-id',
55                 InputArgument::OPTIONAL,
56                 $this->trans('commands.views.debug.arguments.view-id')
57             )
58             ->addOption(
59                 'tag',
60                 null,
61                 InputOption::VALUE_OPTIONAL,
62                 $this->trans('commands.views.debug.arguments.view-tag')
63             )->addOption(
64                 'status',
65                 null,
66                 InputOption::VALUE_OPTIONAL,
67                 $this->trans('commands.views.debug.arguments.view-status')
68             );
69     }
70
71     /**
72      * {@inheritdoc}
73      */
74     protected function execute(InputInterface $input, OutputInterface $output)
75     {
76         $io = new DrupalStyle($input, $output);
77         $view_id = $input->getArgument('view-id');
78         $view_tag = $input->getOption('tag');
79         $view_status = $input->getOption('status');
80
81         if ($view_status == $this->trans('commands.common.status.enabled')) {
82             $view_status = 1;
83         } elseif ($view_status == $this->trans('commands.common.status.disabled')) {
84             $view_status = 0;
85         } else {
86             $view_status = -1;
87         }
88
89         if ($view_id) {
90             $this->viewDetail($io, $view_id);
91         } else {
92             $this->viewList($io, $view_tag, $view_status);
93         }
94     }
95
96
97     /**
98      * @param \Drupal\Console\Core\Style\DrupalStyle $io
99      * @param $view_id
100      * @return bool
101      */
102     private function viewDetail(DrupalStyle $io, $view_id)
103     {
104         $view = $this->entityTypeManager->getStorage('view')->load($view_id);
105
106         if (empty($view)) {
107             $io->error(sprintf($this->trans('commands.views.debug.messages.not-found'), $view_id));
108
109             return false;
110         }
111
112         $configuration = [];
113         $configuration [] = [$this->trans('commands.views.debug.messages.view-id'), $view->get('id')];
114         $configuration [] = [$this->trans('commands.views.debug.messages.view-name'), (string) $view->get('label')];
115         $configuration [] = [$this->trans('commands.views.debug.messages.tag'), $view->get('tag')];
116         $configuration [] = [$this->trans('commands.views.debug.messages.status'), $view->status() ? $this->trans('commands.common.status.enabled') : $this->trans('commands.common.status.disabled')];
117         $configuration [] = [$this->trans('commands.views.debug.messages.description'), $view->get('description')];
118
119         $io->comment($view_id);
120
121         $io->table([], $configuration);
122
123         $tableHeader = [
124           $this->trans('commands.views.debug.messages.display-id'),
125           $this->trans('commands.views.debug.messages.display-name'),
126           $this->trans('commands.views.debug.messages.display-description'),
127           $this->trans('commands.views.debug.messages.display-paths'),
128         ];
129         $displays = $this->viewDisplayList($view);
130
131         $io->info(sprintf($this->trans('commands.views.debug.messages.display-list'), $view_id));
132
133         $tableRows = [];
134         foreach ($displays as $display_id => $display) {
135             $tableRows[] = [
136               $display_id,
137               $display['name'],
138               $display['description'],
139               $this->viewDisplayPaths($view, $display_id),
140             ];
141         }
142
143         $io->table($tableHeader, $tableRows);
144     }
145
146     /**
147      * @param \Drupal\Console\Core\Style\DrupalStyle $io
148      * @param $tag
149      * @param $status
150      */
151     protected function viewList(DrupalStyle $io, $tag, $status)
152     {
153         $views = $this->entityTypeManager->getStorage('view')->loadMultiple();
154
155         $tableHeader = [
156           $this->trans('commands.views.debug.messages.view-id'),
157           $this->trans('commands.views.debug.messages.view-name'),
158           $this->trans('commands.views.debug.messages.tag'),
159           $this->trans('commands.views.debug.messages.status'),
160           $this->trans('commands.views.debug.messages.path')
161         ];
162
163         $tableRows = [];
164         foreach ($views as $view) {
165             if ($status != -1 && $view->status() != $status) {
166                 continue;
167             }
168
169             if (isset($tag) && $view->get('tag') != $tag) {
170                 continue;
171             }
172             $tableRows[] = [
173               $view->get('id'),
174               $view->get('label'),
175               $view->get('tag'),
176               $view->status() ? $this->trans('commands.common.status.enabled') : $this->trans('commands.common.status.disabled'),
177               $this->viewDisplayPaths($view),
178             ];
179         }
180         $io->table($tableHeader, $tableRows, 'compact');
181     }
182
183
184     /**
185      * @param \Drupal\views\Entity\View $view
186      * @param null                      $display_id
187      * @return string
188      */
189     protected function viewDisplayPaths(View $view, $display_id = null)
190     {
191         $all_paths = [];
192         $executable = $view->getExecutable();
193         $executable->initDisplay();
194         foreach ($executable->displayHandlers as $display) {
195             if ($display->hasPath()) {
196                 $path = $display->getPath();
197                 if (strpos($path, '%') === false) {
198                     //  @see Views should expect and store a leading /. See:
199                     //  https://www.drupal.org/node/2423913
200                     $all_paths[] = '/'.$path;
201                 } else {
202                     $all_paths[] = '/'.$path;
203                 }
204
205                 if ($display_id !== null && $display_id == $display->getBaseId()) {
206                     return '/'.$path;
207                 }
208             }
209         }
210
211         return implode(', ', array_unique($all_paths));
212     }
213
214     /**
215      * @param \Drupal\views\Entity\View $view
216      * @return array
217      */
218     protected function viewDisplayList(View $view)
219     {
220         $displayManager = $this->getViewDisplayManager();
221         $displays = [];
222         foreach ($view->get('display') as $display) {
223             $definition = $displayManager->getDefinition($display['display_plugin']);
224             if (!empty($definition['admin'])) {
225                 // Cast the admin label to a string since it is an object.
226                 $displays[$definition['id']]['name'] = (string) $definition['admin'];
227                 $displays[$definition['id']]['description'] = (string) $definition['help'];
228             }
229         }
230         asort($displays);
231
232         return $displays;
233     }
234 }