5 * Contains \Drupal\Console\Command\Config\ExportViewCommand.
8 namespace Drupal\Console\Command\Config;
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\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Command\Shared\ModuleTrait;
17 use Drupal\Core\Entity\EntityTypeManagerInterface;
18 use Drupal\Core\Config\CachedStorage;
19 use Drupal\Console\Core\Style\DrupalStyle;
20 use Drupal\Console\Command\Shared\ExportTrait;
21 use Drupal\Console\Extension\Manager;
23 class ExportViewCommand extends Command
29 protected $configExport;
33 * @var EntityTypeManagerInterface
35 protected $entityTypeManager;
40 protected $configStorage;
45 protected $extensionManager;
48 * ExportViewCommand constructor.
50 * @param EntityTypeManagerInterface $entityTypeManager
51 * @param CachedStorage $configStorage
52 * @param Manager $extensionManager
54 public function __construct(
55 EntityTypeManagerInterface $entityTypeManager,
56 CachedStorage $configStorage,
57 Manager $extensionManager
59 $this->entityTypeManager = $entityTypeManager;
60 $this->configStorage = $configStorage;
61 $this->extensionManager = $extensionManager;
62 parent::__construct();
66 protected function configure()
69 ->setName('config:export:view')
70 ->setDescription($this->trans('commands.config.export.view.description'))
73 InputOption::VALUE_REQUIRED,
74 $this->trans('commands.common.options.module')
78 InputArgument::OPTIONAL,
79 $this->trans('commands.config.export.view.arguments.view-id')
84 InputOption::VALUE_OPTIONAL,
85 $this->trans('commands.config.export.view.options.optional-config')
88 'include-module-dependencies',
90 InputOption::VALUE_OPTIONAL,
91 $this->trans('commands.config.export.view.options.include-module-dependencies')
98 protected function interact(InputInterface $input, OutputInterface $output)
100 $io = new DrupalStyle($input, $output);
103 $module = $input->getOption('module');
105 // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
106 $module = $this->moduleQuestion($io);
107 $input->setOption('module', $module);
111 $viewId = $input->getArgument('view-id');
113 $views = $this->entityTypeManager->getStorage('view')->loadMultiple();
116 foreach ($views as $view) {
117 $viewList[$view->get('id')] = $view->get('label');
120 $viewId = $io->choiceNoList(
121 $this->trans('commands.config.export.view.questions.view'),
124 $input->setArgument('view-id', $viewId);
127 $optionalConfig = $input->getOption('optional-config');
128 if (!$optionalConfig) {
129 $optionalConfig = $io->confirm(
130 $this->trans('commands.config.export.view.questions.optional-config'),
133 $input->setOption('optional-config', $optionalConfig);
136 $includeModuleDependencies = $input->getOption('include-module-dependencies');
137 if (!$includeModuleDependencies) {
138 $includeModuleDependencies = $io->confirm(
139 $this->trans('commands.config.export.view.questions.include-module-dependencies'),
142 $input->setOption('include-module-dependencies', $includeModuleDependencies);
146 protected function execute(InputInterface $input, OutputInterface $output)
148 $io = new DrupalStyle($input, $output);
150 $module = $input->getOption('module');
151 $viewId = $input->getArgument('view-id');
152 $optionalConfig = $input->getOption('optional-config');
153 $includeModuleDependencies = $input->getOption('include-module-dependencies');
155 $viewTypeDefinition = $this->entityTypeManager->getDefinition('view');
156 $viewTypeName = $viewTypeDefinition->getConfigPrefix() . '.' . $viewId;
158 $viewNameConfig = $this->getConfiguration($viewTypeName);
160 $this->configExport[$viewTypeName] = ['data' => $viewNameConfig, 'optional' => $optionalConfig];
162 // Include config dependencies in export files
163 if ($dependencies = $this->fetchDependencies($viewNameConfig, 'config')) {
164 $this->resolveDependencies($dependencies, $optionalConfig);
167 // Include module dependencies in export files if export is not optional
168 if ($includeModuleDependencies) {
169 if ($dependencies = $this->fetchDependencies($viewNameConfig, 'module')) {
170 $this->exportModuleDependencies($io, $module, $dependencies);
174 $this->exportConfigToModule($module, $io, $this->trans('commands.views.export.messages.view_exported'));