Yaffs site version 1.1
[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 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;
22
23 class ExportContentTypeCommand extends Command
24 {
25     use CommandTrait;
26     use ModuleTrait;
27     use ExportTrait;
28
29     /**
30      * @var EntityTypeManagerInterface
31      */
32     protected $entityTypeManager;
33
34     /**
35      * @var CachedStorage
36      */
37     protected $configStorage;
38
39     /**
40      * @var Manager
41      */
42     protected $extensionManager;
43
44     protected $configExport;
45
46     /**
47      * ExportContentTypeCommand constructor.
48      *
49      * @param EntityTypeManagerInterface $entityTypeManager
50      * @param CachedStorage              $configStorage
51      * @param Manager                    $extensionManager
52      */
53     public function __construct(
54         EntityTypeManagerInterface $entityTypeManager,
55         CachedStorage $configStorage,
56         Manager $extensionManager
57     ) {
58         $this->entityTypeManager = $entityTypeManager;
59         $this->configStorage = $configStorage;
60         $this->extensionManager = $extensionManager;
61         parent::__construct();
62     }
63
64     /**
65      * {@inheritdoc}
66      */
67     protected function configure()
68     {
69         $this
70             ->setName('config:export:content:type')
71             ->setDescription($this->trans('commands.config.export.content.type.description'))
72             ->addOption('module', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
73             ->addArgument(
74                 'content-type',
75                 InputArgument::REQUIRED,
76                 $this->trans('commands.config.export.content.type.arguments.content-type')
77             )->addOption(
78                 'optional-config',
79                 null,
80                 InputOption::VALUE_OPTIONAL,
81                 $this->trans('commands.config.export.content.type.options.optional-config')
82             );
83
84         $this->configExport = [];
85     }
86
87     /**
88      * {@inheritdoc}
89      */
90     protected function interact(InputInterface $input, OutputInterface $output)
91     {
92         $io = new DrupalStyle($input, $output);
93
94         // --module option
95         $module = $input->getOption('module');
96         if (!$module) {
97             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
98             $module = $this->moduleQuestion($io);
99         }
100         $input->setOption('module', $module);
101
102         // --content-type argument
103         $contentType = $input->getArgument('content-type');
104         if (!$contentType) {
105             $bundles_entities = $this->entityTypeManager->getStorage('node_type')->loadMultiple();
106             $bundles = [];
107             foreach ($bundles_entities as $entity) {
108                 $bundles[$entity->id()] = $entity->label();
109             }
110
111             $contentType = $io->choice(
112                 $this->trans('commands.config.export.content.type.questions.content-type'),
113                 $bundles
114             );
115         }
116         $input->setArgument('content-type', $contentType);
117
118         $optionalConfig = $input->getOption('optional-config');
119         if (!$optionalConfig) {
120             $optionalConfig = $io->confirm(
121                 $this->trans('commands.config.export.content.type.questions.optional-config'),
122                 true
123             );
124         }
125         $input->setOption('optional-config', $optionalConfig);
126     }
127
128     /**
129      * {@inheritdoc}
130      */
131     protected function execute(InputInterface $input, OutputInterface $output)
132     {
133         $io = new DrupalStyle($input, $output);
134
135         $module = $input->getOption('module');
136         $contentType = $input->getArgument('content-type');
137         $optionalConfig = $input->getOption('optional-config');
138
139         $contentTypeDefinition = $this->entityTypeManager->getDefinition('node_type');
140         $contentTypeName = $contentTypeDefinition->getConfigPrefix() . '.' . $contentType;
141
142         $contentTypeNameConfig = $this->getConfiguration($contentTypeName);
143
144         $this->configExport[$contentTypeName] = ['data' => $contentTypeNameConfig, 'optional' => $optionalConfig];
145
146         $this->getFields($contentType, $optionalConfig);
147
148         $this->getFormDisplays($contentType, $optionalConfig);
149
150         $this->getViewDisplays($contentType, $optionalConfig);
151
152         $this->exportConfigToModule($module, $io, $this->trans('commands.config.export.content.type.messages.content_type_exported'));
153     }
154
155     protected function getFields($contentType, $optional = false)
156     {
157         $fields_definition = $this->entityTypeManager->getDefinition('field_config');
158
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);
169                 }
170             }
171         }
172     }
173
174     protected function getFormDisplays($contentType, $optional = false)
175     {
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);
187                 }
188             }
189         }
190     }
191
192     protected function getViewDisplays($contentType, $optional = false)
193     {
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);
205                 }
206             }
207         }
208     }
209 }