5 * Contains \Drupal\Console\Command\Config\ExportContentTypeCommand.
8 namespace Drupal\Console\Command\Config;
10 use Drupal\Console\Command\Shared\ModuleTrait;
11 use Symfony\Component\Console\Input\InputArgument;
12 use Symfony\Component\Console\Input\InputOption;
13 use Symfony\Component\Console\Input\InputInterface;
14 use Symfony\Component\Console\Output\OutputInterface;
15 use Symfony\Component\Console\Command\Command;
16 use Drupal\Core\Config\CachedStorage;
17 use Drupal\Core\Entity\EntityTypeManagerInterface;
18 use Drupal\Console\Core\Command\Shared\CommandTrait;
19 use Drupal\Console\Core\Style\DrupalStyle;
20 use Drupal\Console\Command\Shared\ExportTrait;
21 use Drupal\Console\Extension\Manager;
23 class ExportContentTypeCommand extends Command
30 * @var EntityTypeManagerInterface
32 protected $entityTypeManager;
37 protected $configStorage;
42 protected $extensionManager;
44 protected $configExport;
47 * ExportContentTypeCommand constructor.
49 * @param EntityTypeManagerInterface $entityTypeManager
50 * @param CachedStorage $configStorage
51 * @param Manager $extensionManager
53 public function __construct(
54 EntityTypeManagerInterface $entityTypeManager,
55 CachedStorage $configStorage,
56 Manager $extensionManager
58 $this->entityTypeManager = $entityTypeManager;
59 $this->configStorage = $configStorage;
60 $this->extensionManager = $extensionManager;
61 parent::__construct();
67 protected function configure()
70 ->setName('config:export:content:type')
71 ->setDescription($this->trans('commands.config.export.content.type.description'))
72 ->addOption('module', '', InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
75 InputArgument::REQUIRED,
76 $this->trans('commands.config.export.content.type.arguments.content-type')
80 InputOption::VALUE_OPTIONAL,
81 $this->trans('commands.config.export.content.type.options.optional-config')
84 $this->configExport = [];
90 protected function interact(InputInterface $input, OutputInterface $output)
92 $io = new DrupalStyle($input, $output);
95 $module = $input->getOption('module');
97 // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
98 $module = $this->moduleQuestion($io);
100 $input->setOption('module', $module);
102 // --content-type argument
103 $contentType = $input->getArgument('content-type');
105 $bundles_entities = $this->entityTypeManager->getStorage('node_type')->loadMultiple();
107 foreach ($bundles_entities as $entity) {
108 $bundles[$entity->id()] = $entity->label();
111 $contentType = $io->choice(
112 $this->trans('commands.config.export.content.type.questions.content-type'),
116 $input->setArgument('content-type', $contentType);
118 $optionalConfig = $input->getOption('optional-config');
119 if (!$optionalConfig) {
120 $optionalConfig = $io->confirm(
121 $this->trans('commands.config.export.content.type.questions.optional-config'),
125 $input->setOption('optional-config', $optionalConfig);
131 protected function execute(InputInterface $input, OutputInterface $output)
133 $io = new DrupalStyle($input, $output);
135 $module = $input->getOption('module');
136 $contentType = $input->getArgument('content-type');
137 $optionalConfig = $input->getOption('optional-config');
139 $contentTypeDefinition = $this->entityTypeManager->getDefinition('node_type');
140 $contentTypeName = $contentTypeDefinition->getConfigPrefix() . '.' . $contentType;
142 $contentTypeNameConfig = $this->getConfiguration($contentTypeName);
144 $this->configExport[$contentTypeName] = ['data' => $contentTypeNameConfig, 'optional' => $optionalConfig];
146 $this->getFields($contentType, $optionalConfig);
148 $this->getFormDisplays($contentType, $optionalConfig);
150 $this->getViewDisplays($contentType, $optionalConfig);
152 $this->exportConfigToModule($module, $io, $this->trans('commands.config.export.content.type.messages.content_type_exported'));
155 protected function getFields($contentType, $optional = false)
157 $fields_definition = $this->entityTypeManager->getDefinition('field_config');
159 $fields_storage = $this->entityTypeManager->getStorage('field_config');
160 foreach ($fields_storage->loadMultiple() as $field) {
161 $field_name = $fields_definition->getConfigPrefix() . '.' . $field->id();
162 $field_name_config = $this->getConfiguration($field_name);
163 // Only select fields related with content type
164 if ($field_name_config['bundle'] == $contentType) {
165 $this->configExport[$field_name] = ['data' => $field_name_config, 'optional' => $optional];
166 // Include dependencies in export files
167 if ($dependencies = $this->fetchDependencies($field_name_config, 'config')) {
168 $this->resolveDependencies($dependencies, $optional);
174 protected function getFormDisplays($contentType, $optional = false)
176 $form_display_definition = $this->entityTypeManager->getDefinition('entity_form_display');
177 $form_display_storage = $this->entityTypeManager->getStorage('entity_form_display');
178 foreach ($form_display_storage->loadMultiple() as $form_display) {
179 $form_display_name = $form_display_definition->getConfigPrefix() . '.' . $form_display->id();
180 $form_display_name_config = $this->getConfiguration($form_display_name);
181 // Only select fields related with content type
182 if ($form_display_name_config['bundle'] == $contentType) {
183 $this->configExport[$form_display_name] = ['data' => $form_display_name_config, 'optional' => $optional];
184 // Include dependencies in export files
185 if ($dependencies = $this->fetchDependencies($form_display_name_config, 'config')) {
186 $this->resolveDependencies($dependencies, $optional);
192 protected function getViewDisplays($contentType, $optional = false)
194 $view_display_definition = $this->entityTypeManager->getDefinition('entity_view_display');
195 $view_display_storage = $this->entityTypeManager->getStorage('entity_view_display');
196 foreach ($view_display_storage->loadMultiple() as $view_display) {
197 $view_display_name = $view_display_definition->getConfigPrefix() . '.' . $view_display->id();
198 $view_display_name_config = $this->getConfiguration($view_display_name);
199 // Only select fields related with content type
200 if ($view_display_name_config['bundle'] == $contentType) {
201 $this->configExport[$view_display_name] = ['data' => $view_display_name_config, 'optional' => $optional];
202 // Include dependencies in export files
203 if ($dependencies = $this->fetchDependencies($view_display_name_config, 'config')) {
204 $this->resolveDependencies($dependencies, $optional);