Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Config / ExportContentTypeCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Config\ExportContentTypeCommand.
6  */
7
8 namespace Drupal\Console\Command\Config;
9
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;
22
23 class ExportContentTypeCommand extends Command
24 {
25     use ModuleTrait;
26     use ExportTrait;
27
28     /**
29      * @var EntityTypeManagerInterface
30      */
31     protected $entityTypeManager;
32
33     /**
34      * @var CachedStorage
35      */
36     protected $configStorage;
37
38     /**
39      * @var Manager
40      */
41     protected $extensionManager;
42
43     protected $configExport;
44
45     /**
46      * @var Validator
47      */
48     protected $validator;
49
50     /**
51      * ExportContentTypeCommand constructor.
52      *
53      * @param EntityTypeManagerInterface $entityTypeManager
54      * @param CachedStorage              $configStorage
55      * @param Manager                    $extensionManager
56      * @param Validator                  $validator
57      */
58     public function __construct(
59         EntityTypeManagerInterface $entityTypeManager,
60         CachedStorage $configStorage,
61         Manager $extensionManager,
62         Validator $validator
63     ) {
64         $this->entityTypeManager = $entityTypeManager;
65         $this->configStorage = $configStorage;
66         $this->extensionManager = $extensionManager;
67         $this->validator = $validator;
68         parent::__construct();
69     }
70
71     /**
72      * {@inheritdoc}
73      */
74     protected function configure()
75     {
76         $this
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'))
80             ->addArgument(
81                 'content-type',
82                 InputArgument::REQUIRED,
83                 $this->trans('commands.config.export.content.type.arguments.content-type')
84             )->addOption(
85                 'optional-config',
86                 null,
87                 InputOption::VALUE_OPTIONAL,
88                 $this->trans('commands.config.export.content.type.options.optional-config')
89             )->addOption(
90                 'remove-uuid',
91                 null,
92                 InputOption::VALUE_NONE,
93                 $this->trans('commands.config.export.content.type.options.remove-uuid')
94             )->addOption(
95                 'remove-config-hash',
96                 null,
97                 InputOption::VALUE_NONE,
98                 $this->trans('commands.config.export.content.type.options.remove-config-hash')
99             )
100             ->setAliases(['cect']);
101
102         $this->configExport = [];
103     }
104
105     /**
106      * {@inheritdoc}
107      */
108     protected function interact(InputInterface $input, OutputInterface $output)
109     {
110         // --module option
111         $this->getModuleOption();
112
113         // --content-type argument
114         $contentType = $input->getArgument('content-type');
115         if (!$contentType) {
116             $bundles_entities = $this->entityTypeManager->getStorage('node_type')->loadMultiple();
117             $bundles = [];
118             foreach ($bundles_entities as $entity) {
119                 $bundles[$entity->id()] = $entity->label();
120             }
121
122             $contentType = $this->getIo()->choice(
123                 $this->trans('commands.config.export.content.type.questions.content-type'),
124                 $bundles
125             );
126         }
127         $input->setArgument('content-type', $contentType);
128
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'),
133                 true
134             );
135         }
136         $input->setOption('optional-config', $optionalConfig);
137
138
139         if (!$input->getOption('remove-uuid')) {
140             $removeUuid = $this->getIo()->confirm(
141                 $this->trans('commands.config.export.content.type.questions.remove-uuid'),
142                 true
143             );
144             $input->setOption('remove-uuid', $removeUuid);
145         }
146         if (!$input->getOption('remove-config-hash')) {
147             $removeHash = $this->getIo()->confirm(
148                 $this->trans('commands.config.export.content.type.questions.remove-config-hash'),
149                 true
150             );
151             $input->setOption('remove-config-hash', $removeHash);
152         }
153     }
154
155     /**
156      * {@inheritdoc}
157      */
158     protected function execute(InputInterface $input, OutputInterface $output)
159     {
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');
165
166         $contentTypeDefinition = $this->entityTypeManager->getDefinition('node_type');
167         $contentTypeName = $contentTypeDefinition->getConfigPrefix() . '.' . $contentType;
168
169         $contentTypeNameConfig = $this->getConfiguration($contentTypeName, $removeUuid, $removeHash);
170
171         if (empty($contentTypeNameConfig)) {
172             throw new InvalidOptionException(sprintf('The content type %s does not exist.', $contentType));
173         }
174
175         $this->configExport[$contentTypeName] = ['data' => $contentTypeNameConfig, 'optional' => $optionalConfig];
176
177         $this->getFields($contentType, $optionalConfig, $removeUuid, $removeHash);
178
179         $this->getFormDisplays($contentType, $optionalConfig, $removeUuid, $removeHash);
180
181         $this->getViewDisplays($contentType, $optionalConfig, $removeUuid, $removeHash);
182
183         $this->exportConfigToModule($module, $this->trans('commands.config.export.content.type.messages.content-type-exported'));
184     }
185
186     protected function getFields($contentType, $optional = false, $removeUuid = false, $removeHash = false)
187     {
188         $fields_definition = $this->entityTypeManager->getDefinition('field_config');
189
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);
194
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);
201                 }
202             }
203         }
204     }
205
206     protected function getFormDisplays($contentType, $optional = false, $removeUuid = false, $removeHash = false)
207     {
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);
219                 }
220             }
221         }
222     }
223
224     protected function getViewDisplays($contentType, $optional = false, $removeUuid = false, $removeHash = false)
225     {
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);
237                 }
238             }
239         }
240     }
241 }