5 * Contains \Drupal\Console\Command\Config\ExportContentTypeCommand.
8 namespace Drupal\Console\Command\Config;
10 use Drupal\Console\Command\Shared\ModuleTrait;
11 use Drupal\Console\Utils\Validator;
12 use Symfony\Component\Console\Exception\InvalidOptionException;
13 use Symfony\Component\Console\Input\InputArgument;
14 use Symfony\Component\Console\Input\InputOption;
15 use Symfony\Component\Console\Input\InputInterface;
16 use Symfony\Component\Console\Output\OutputInterface;
17 use Drupal\Console\Core\Command\Command;
18 use Drupal\Core\Config\CachedStorage;
19 use Drupal\Core\Entity\EntityTypeManagerInterface;
20 use Drupal\Console\Command\Shared\ExportTrait;
21 use Drupal\Console\Extension\Manager;
23 class ExportContentTypeCommand extends Command
29 * @var EntityTypeManagerInterface
31 protected $entityTypeManager;
36 protected $configStorage;
41 protected $extensionManager;
43 protected $configExport;
51 * ExportContentTypeCommand constructor.
53 * @param EntityTypeManagerInterface $entityTypeManager
54 * @param CachedStorage $configStorage
55 * @param Manager $extensionManager
56 * @param Validator $validator
58 public function __construct(
59 EntityTypeManagerInterface $entityTypeManager,
60 CachedStorage $configStorage,
61 Manager $extensionManager,
64 $this->entityTypeManager = $entityTypeManager;
65 $this->configStorage = $configStorage;
66 $this->extensionManager = $extensionManager;
67 $this->validator = $validator;
68 parent::__construct();
74 protected function configure()
77 ->setName('config:export:content:type')
78 ->setDescription($this->trans('commands.config.export.content.type.description'))
79 ->addOption('module', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
82 InputArgument::REQUIRED,
83 $this->trans('commands.config.export.content.type.arguments.content-type')
87 InputOption::VALUE_OPTIONAL,
88 $this->trans('commands.config.export.content.type.options.optional-config')
92 InputOption::VALUE_NONE,
93 $this->trans('commands.config.export.content.type.options.remove-uuid')
97 InputOption::VALUE_NONE,
98 $this->trans('commands.config.export.content.type.options.remove-config-hash')
100 ->setAliases(['cect']);
102 $this->configExport = [];
108 protected function interact(InputInterface $input, OutputInterface $output)
111 $this->getModuleOption();
113 // --content-type argument
114 $contentType = $input->getArgument('content-type');
116 $bundles_entities = $this->entityTypeManager->getStorage('node_type')->loadMultiple();
118 foreach ($bundles_entities as $entity) {
119 $bundles[$entity->id()] = $entity->label();
122 $contentType = $this->getIo()->choice(
123 $this->trans('commands.config.export.content.type.questions.content-type'),
127 $input->setArgument('content-type', $contentType);
129 $optionalConfig = $input->getOption('optional-config');
130 if (!$optionalConfig) {
131 $optionalConfig = $this->getIo()->confirm(
132 $this->trans('commands.config.export.content.type.questions.optional-config'),
136 $input->setOption('optional-config', $optionalConfig);
139 if (!$input->getOption('remove-uuid')) {
140 $removeUuid = $this->getIo()->confirm(
141 $this->trans('commands.config.export.content.type.questions.remove-uuid'),
144 $input->setOption('remove-uuid', $removeUuid);
146 if (!$input->getOption('remove-config-hash')) {
147 $removeHash = $this->getIo()->confirm(
148 $this->trans('commands.config.export.content.type.questions.remove-config-hash'),
151 $input->setOption('remove-config-hash', $removeHash);
158 protected function execute(InputInterface $input, OutputInterface $output)
160 $module = $input->getOption('module');
161 $contentType = $input->getArgument('content-type');
162 $optionalConfig = $input->getOption('optional-config');
163 $removeUuid = $input->getOption('remove-uuid');
164 $removeHash = $input->getOption('remove-config-hash');
166 $contentTypeDefinition = $this->entityTypeManager->getDefinition('node_type');
167 $contentTypeName = $contentTypeDefinition->getConfigPrefix() . '.' . $contentType;
169 $contentTypeNameConfig = $this->getConfiguration($contentTypeName, $removeUuid, $removeHash);
171 if (empty($contentTypeNameConfig)) {
172 throw new InvalidOptionException(sprintf('The content type %s does not exist.', $contentType));
175 $this->configExport[$contentTypeName] = ['data' => $contentTypeNameConfig, 'optional' => $optionalConfig];
177 $this->getFields($contentType, $optionalConfig, $removeUuid, $removeHash);
179 $this->getFormDisplays($contentType, $optionalConfig, $removeUuid, $removeHash);
181 $this->getViewDisplays($contentType, $optionalConfig, $removeUuid, $removeHash);
183 $this->exportConfigToModule($module, $this->trans('commands.config.export.content.type.messages.content-type-exported'));
186 protected function getFields($contentType, $optional = false, $removeUuid = false, $removeHash = false)
188 $fields_definition = $this->entityTypeManager->getDefinition('field_config');
190 $fields_storage = $this->entityTypeManager->getStorage('field_config');
191 foreach ($fields_storage->loadMultiple() as $field) {
192 $field_name = $fields_definition->getConfigPrefix() . '.' . $field->id();
193 $field_name_config = $this->getConfiguration($field_name, $removeUuid, $removeHash);
195 // Only select fields related with content type
196 if ($field_name_config['bundle'] == $contentType) {
197 $this->configExport[$field_name] = ['data' => $field_name_config, 'optional' => $optional];
198 // Include dependencies in export files
199 if ($dependencies = $this->fetchDependencies($field_name_config, 'config')) {
200 $this->resolveDependencies($dependencies, $optional);
206 protected function getFormDisplays($contentType, $optional = false, $removeUuid = false, $removeHash = false)
208 $form_display_definition = $this->entityTypeManager->getDefinition('entity_form_display');
209 $form_display_storage = $this->entityTypeManager->getStorage('entity_form_display');
210 foreach ($form_display_storage->loadMultiple() as $form_display) {
211 $form_display_name = $form_display_definition->getConfigPrefix() . '.' . $form_display->id();
212 $form_display_name_config = $this->getConfiguration($form_display_name, $removeUuid, $removeHash);
213 // Only select fields related with content type
214 if ($form_display_name_config['bundle'] == $contentType) {
215 $this->configExport[$form_display_name] = ['data' => $form_display_name_config, 'optional' => $optional];
216 // Include dependencies in export files
217 if ($dependencies = $this->fetchDependencies($form_display_name_config, 'config')) {
218 $this->resolveDependencies($dependencies, $optional);
224 protected function getViewDisplays($contentType, $optional = false, $removeUuid = false, $removeHash = false)
226 $view_display_definition = $this->entityTypeManager->getDefinition('entity_view_display');
227 $view_display_storage = $this->entityTypeManager->getStorage('entity_view_display');
228 foreach ($view_display_storage->loadMultiple() as $view_display) {
229 $view_display_name = $view_display_definition->getConfigPrefix() . '.' . $view_display->id();
230 $view_display_name_config = $this->getConfiguration($view_display_name, $removeUuid, $removeHash);
231 // Only select fields related with content type
232 if ($view_display_name_config['bundle'] == $contentType) {
233 $this->configExport[$view_display_name] = ['data' => $view_display_name_config, 'optional' => $optional];
234 // Include dependencies in export files
235 if ($dependencies = $this->fetchDependencies($view_display_name_config, 'config')) {
236 $this->resolveDependencies($dependencies, $optional);