5 * Contains \Drupal\Console\Command\Debug\ViewsCommand.
8 namespace Drupal\Console\Command\Debug;
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 Drupal\Console\Core\Command\Command;
15 use Drupal\views\Entity\View;
16 use Drupal\Core\Entity\EntityTypeManagerInterface;
17 use Drupal\Component\Plugin\PluginManagerInterface;
18 use Drupal\Console\Annotations\DrupalCommand;
24 * extension = "views",
25 * extensionType = "module"
28 * @package Drupal\Console\Command\Debug
30 class ViewsCommand extends Command
33 * @var EntityTypeManagerInterface
35 protected $entityTypeManager;
37 * @var PluginManagerInterface
39 protected $viewsDisplayManager;
42 * DebugCommand constructor.
44 * @param EntityTypeManagerInterface $entityTypeManager
45 * @param PluginManagerInterface $viewsDisplayManager
47 public function __construct(EntityTypeManagerInterface $entityTypeManager, PluginManagerInterface $viewsDisplayManager)
49 $this->entityTypeManager = $entityTypeManager;
50 $this->viewsDisplayManager = $viewsDisplayManager;
51 parent::__construct();
57 protected function configure()
60 ->setName('debug:views')
61 ->setDescription($this->trans('commands.debug.views.description'))
64 InputArgument::OPTIONAL,
65 $this->trans('commands.debug.views.arguments.view-id')
70 InputOption::VALUE_OPTIONAL,
71 $this->trans('commands.debug.views.arguments.view-tag')
75 InputOption::VALUE_OPTIONAL,
76 $this->trans('commands.debug.views.arguments.view-status')
78 ->setAliases(['vde']);
84 protected function execute(InputInterface $input, OutputInterface $output)
86 $view_id = $input->getArgument('view-id');
87 $view_tag = $input->getOption('tag');
88 $view_status = $input->getOption('status');
90 if ($view_status == $this->trans('commands.common.status.enabled')) {
92 } elseif ($view_status == $this->trans('commands.common.status.disabled')) {
99 $this->viewDetail($view_id);
101 $this->viewList($view_tag, $view_status);
110 private function viewDetail($view_id)
112 $view = $this->entityTypeManager->getStorage('view')->load($view_id);
115 $this->getIo()->error(sprintf($this->trans('commands.debug.views.messages.not-found'), $view_id));
121 $configuration [] = [$this->trans('commands.debug.views.messages.view-id'), $view->get('id')];
122 $configuration [] = [$this->trans('commands.debug.views.messages.view-name'), (string) $view->get('label')];
123 $configuration [] = [$this->trans('commands.debug.views.messages.tag'), $view->get('tag')];
124 $configuration [] = [$this->trans('commands.debug.views.messages.status'), $view->status() ? $this->trans('commands.common.status.enabled') : $this->trans('commands.common.status.disabled')];
125 $configuration [] = [$this->trans('commands.debug.views.messages.description'), $view->get('description')];
127 $this->getIo()->comment($view_id);
129 $this->getIo()->table([], $configuration);
132 $this->trans('commands.debug.views.messages.display-id'),
133 $this->trans('commands.debug.views.messages.display-name'),
134 $this->trans('commands.debug.views.messages.display-description'),
135 $this->trans('commands.debug.views.messages.display-paths'),
137 $displays = $this->viewDisplayList($view);
139 $this->getIo()->info(sprintf($this->trans('commands.debug.views.messages.display-list'), $view_id));
142 foreach ($displays as $display_id => $display) {
146 $display['description'],
147 $this->viewDisplayPaths($view, $display_id),
151 $this->getIo()->table($tableHeader, $tableRows);
158 protected function viewList($tag, $status)
160 $views = $this->entityTypeManager->getStorage('view')->loadMultiple();
163 $this->trans('commands.debug.views.messages.view-id'),
164 $this->trans('commands.debug.views.messages.view-name'),
165 $this->trans('commands.debug.views.messages.tag'),
166 $this->trans('commands.debug.views.messages.status'),
167 $this->trans('commands.debug.views.messages.path')
171 foreach ($views as $view) {
172 if ($status != -1 && $view->status() != $status) {
176 if (isset($tag) && $view->get('tag') != $tag) {
183 $view->status() ? $this->trans('commands.common.status.enabled') : $this->trans('commands.common.status.disabled'),
184 $this->viewDisplayPaths($view),
187 $this->getIo()->table($tableHeader, $tableRows, 'compact');
192 * @param \Drupal\views\Entity\View $view
193 * @param null $display_id
196 protected function viewDisplayPaths(View $view, $display_id = null)
199 $executable = $view->getExecutable();
200 $executable->initDisplay();
201 foreach ($executable->displayHandlers as $display) {
202 if ($display->hasPath()) {
203 $path = $display->getPath();
204 if (strpos($path, '%') === false) {
205 // @see Views should expect and store a leading /. See:
206 // https://www.drupal.org/node/2423913
207 $all_paths[] = '/'.$path;
209 $all_paths[] = '/'.$path;
212 if ($display_id !== null && $display_id == $display->getBaseId()) {
218 return implode(', ', array_unique($all_paths));
222 * @param \Drupal\views\Entity\View $view
225 protected function viewDisplayList(View $view)
228 foreach ($view->get('display') as $display) {
229 $definition = $this->viewsDisplayManager->getDefinition($display['display_plugin']);
230 if (!empty($definition['admin'])) {
231 // Cast the admin label to a string since it is an object.
232 $displays[$definition['id']]['name'] = (string) $definition['admin'];
233 $displays[$definition['id']]['description'] = (string) $definition['help'];