5 * Contains \Drupal\Console\Command\Views\DebugCommand.
8 namespace Drupal\Console\Command\Views;
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;
23 * @package Drupal\Console\Command\Views
25 class DebugCommand extends Command
30 * @var EntityTypeManagerInterface
32 protected $entityTypeManager;
35 * DebugCommand constructor.
37 * @param EntityTypeManagerInterface $entityTypeManager
39 public function __construct(EntityTypeManagerInterface $entityTypeManager)
41 $this->entityTypeManager = $entityTypeManager;
42 parent::__construct();
48 protected function configure()
51 ->setName('views:debug')
52 ->setDescription($this->trans('commands.views.debug.description'))
55 InputArgument::OPTIONAL,
56 $this->trans('commands.views.debug.arguments.view-id')
61 InputOption::VALUE_OPTIONAL,
62 $this->trans('commands.views.debug.arguments.view-tag')
66 InputOption::VALUE_OPTIONAL,
67 $this->trans('commands.views.debug.arguments.view-status')
74 protected function execute(InputInterface $input, OutputInterface $output)
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');
81 if ($view_status == $this->trans('commands.common.status.enabled')) {
83 } elseif ($view_status == $this->trans('commands.common.status.disabled')) {
90 $this->viewDetail($io, $view_id);
92 $this->viewList($io, $view_tag, $view_status);
98 * @param \Drupal\Console\Core\Style\DrupalStyle $io
102 private function viewDetail(DrupalStyle $io, $view_id)
104 $view = $this->entityTypeManager->getStorage('view')->load($view_id);
107 $io->error(sprintf($this->trans('commands.views.debug.messages.not-found'), $view_id));
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')];
119 $io->comment($view_id);
121 $io->table([], $configuration);
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'),
129 $displays = $this->viewDisplayList($view);
131 $io->info(sprintf($this->trans('commands.views.debug.messages.display-list'), $view_id));
134 foreach ($displays as $display_id => $display) {
138 $display['description'],
139 $this->viewDisplayPaths($view, $display_id),
143 $io->table($tableHeader, $tableRows);
147 * @param \Drupal\Console\Core\Style\DrupalStyle $io
151 protected function viewList(DrupalStyle $io, $tag, $status)
153 $views = $this->entityTypeManager->getStorage('view')->loadMultiple();
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')
164 foreach ($views as $view) {
165 if ($status != -1 && $view->status() != $status) {
169 if (isset($tag) && $view->get('tag') != $tag) {
176 $view->status() ? $this->trans('commands.common.status.enabled') : $this->trans('commands.common.status.disabled'),
177 $this->viewDisplayPaths($view),
180 $io->table($tableHeader, $tableRows, 'compact');
185 * @param \Drupal\views\Entity\View $view
186 * @param null $display_id
189 protected function viewDisplayPaths(View $view, $display_id = null)
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;
202 $all_paths[] = '/'.$path;
205 if ($display_id !== null && $display_id == $display->getBaseId()) {
211 return implode(', ', array_unique($all_paths));
215 * @param \Drupal\views\Entity\View $view
218 protected function viewDisplayList(View $view)
220 $displayManager = $this->getViewDisplayManager();
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'];