10df663cdc85f2919d05c349f949c1a0b6d18267
[yaffs-website] / vendor / drupal / console / src / Command / Field / InfoCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Field\InfoCommand.
6 */
7
8 namespace Drupal\Console\Command\Field;
9
10 use Symfony\Component\Console\Input\InputOption;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Drupal\Console\Core\Command\Command;
14 use Drupal\Core\Entity\EntityFieldManagerInterface;
15 use Drupal\Core\Entity\EntityTypeManagerInterface;
16 use Drupal\field\FieldConfigInterface;
17
18 /**
19  * Class InfoCommand.
20  */
21 class InfoCommand extends Command
22 {
23     /**
24      * @var EntityTypeManagerInterface
25      */
26     protected $entityTypeManager;
27
28     /**
29      * @var EntityFieldManagerInterface
30      */
31     protected $entityFieldManager;
32
33     /**
34      * InfoCommand constructor.
35      *
36      * @param EntityTypeManagerInterface  $entityTypeManager
37      * @param EntityFieldManagerInterface $entityFieldManager
38      */
39     public function __construct(
40         EntityTypeManagerInterface $entityTypeManager,
41         EntityFieldManagerInterface $entityFieldManager
42     ) {
43         $this->entityTypeManager = $entityTypeManager;
44         $this->entityFieldManager = $entityFieldManager;
45         parent::__construct();
46     }
47
48     /**
49      * @{@inheritdoc}
50      */
51     public function configure()
52     {
53         $this
54             ->setName('field:info')
55             ->setDescription($this->trans('commands.field.info.description'))
56             ->setHelp($this->trans('commands.field.info.help'))
57             ->addOption(
58                 'detailed',
59                 null,
60                 InputOption::VALUE_NONE,
61                 $this->trans('commands.field.info.options.detailed')
62             )
63             ->addOption(
64                 'entity',
65                 null,
66                 InputOption::VALUE_OPTIONAL,
67                 $this->trans('commands.field.info.options.entity')
68             )
69             ->addOption(
70                 'bundle',
71                 null,
72                 InputOption::VALUE_OPTIONAL,
73                 $this->trans('commands.field.info.options.bundle')
74             )->setAliases(['fii']);
75     }
76
77     /**
78      * {@inheritdoc}
79      */
80     protected function execute(InputInterface $input, OutputInterface $output)
81     {
82         // Retrieve whether detailed option has been selected.
83         $detailedOutput = $input->getOption('detailed');
84
85         // Retrieve whether an entity type has been specified.
86         $entityTypeOption = $input->getOption('entity');
87
88         // Retrieve whether a specific bundle type has been specified.
89         $bundleTypeOption = $input->getOption('bundle');
90
91         $entityList = $this->entityTypeManager->getDefinitions();
92         $allFields = $this->entityFieldManager->getFieldMap();
93
94         // Set a flag so we can error if a specific entity type selected but not found.
95         $entityTypeOptionFound = false;
96
97         // Set a flag so we can error if a specific bundle type selected but not found.
98         $bundleTypeOptionFound = false;
99
100         // Let's count the fields found so we can display a message if none found.
101         $fieldCounter = 0;
102
103         foreach ($entityList as $entityTypeId => $entityValue) {
104             // If the Entity has bundleEntityType set we grab it.
105             $bundleEntityType = $entityValue->get('bundle_entity_type');
106
107             // Check to see if the entity has any bundle before continuing.
108             if (!empty($bundleEntityType)) {
109                 $bundleTypes = $this->entityTypeManager
110                     ->getStorage($bundleEntityType)->loadMultiple();
111
112                 // If a specific entity type has been selected and this is it then we continue else we skip.
113                 if ((!empty($entityTypeOption) && ($entityTypeOption == $entityTypeId))| empty($entityTypeOption)
114                 ) {
115                     // Store the fact that we found the entity type specified so we can error if not found.
116                     $entityTypeOptionFound = true;
117
118                     // Get the entity type label.
119                     $bundleParent = $entityValue->get('label');
120
121                     // Using counter to know whether to output header.
122                     $bundleTypeCounter = 0;
123                     foreach ($bundleTypes as $bundleType) {
124                         // If a specific bundle type has been selected and this is it then we continue else we skip.
125                         if ((!empty($bundleTypeOption) && ($bundleTypeOption == $bundleType->id()))| empty($bundleTypeOption)
126                         ) {
127                             // Store the fact that we found the bundle type specified so we can error if not found.
128                             $bundleTypeOptionFound = true;
129
130                             // Increase the bundle type counter so we know whether to output header.
131                             $bundleTypeCounter++;
132
133                             if ($bundleTypeCounter == 1) {
134                                 // Output the Parent Entity label if we haven't already.
135                                 if ($detailedOutput) {
136                                     // If detailed output then display the id as well.
137                                     $this->getIo()->info(strtoupper($bundleParent) . ' (' . $entityTypeId . '):');
138                                 } else {
139                                     // otherwise just display the label for normal output.
140                                     $this->getIo()->info(strtoupper($bundleParent . ':'));
141                                 }
142                                 $this->getIo()->newLine();
143                             }
144
145                             // Load in the entityType fields.
146                             $fields = $this->getBundleFields(
147                                 $entityTypeId,
148                                 $bundleType->id()
149                             );
150
151                             foreach ($fields as $field => $fieldArray) {
152                                 // We found a field so increase the field counter.
153                                 $fieldCounter++;
154
155                                 // Get the related / used in bundles from the field.
156                                 $relatedBundles = "";
157                                 $relatedBundlesArray = $allFields[$entityTypeId][$field]['bundles'];
158
159                                 // Turn those related / used in bundles array into a string.
160                                 foreach ($relatedBundlesArray as $relatedBundlesValue) {
161                                     if ($bundleTypes[$relatedBundlesValue]->id() != $bundleType->id()) {
162                                         if (!empty($relatedBundles)) {
163                                             $relatedBundles .= ', ' . $bundleTypes[$relatedBundlesValue]->label();
164                                         } else {
165                                             $relatedBundles = $bundleTypes[$relatedBundlesValue]->label();
166                                         }
167                                     }
168                                 }
169
170                                 // Build out our table for the fields.
171                                 $tableRows[] = $detailedOutput ? [
172                                     $fieldArray->get('label'),
173                                     $fieldArray->get('field_type'),
174                                     $fieldArray->get('description'),
175                                     $relatedBundles
176                                 ] : [
177                                     $fieldArray->get('label'),
178                                     $fieldArray->get('field_type'),
179                                     $relatedBundles
180                                 ];
181
182                                 // Clear the related bundles ready for the next field.
183                                 unset($relatedBundles);
184                             }
185
186                             // If detailed output then display bundle id and description.
187                             if ($detailedOutput) {
188                                 // Output the bundle label and id.
189                                 $this->getIo()->info($bundleType->label() . ' (' . $bundleType->id() . ')');
190                                 $this->getIo()->info(strip_tags($bundleType->get('description')));
191                             } else {
192                                 // Else just output the bundle label.
193                                 $this->getIo()->info($bundleType->label());
194                             }
195
196                             // Fill out our table header.
197                             // If no rows exist for the fields then we display a no results message.
198                             if (!empty($tableRows)) {
199                                 $tableHeader = $detailedOutput ? [
200                                     $this->trans('commands.field.info.table.header-name'),
201                                     $this->trans('commands.field.info.table.header-type'),
202                                     $this->trans('commands.field.info.table.header-desc'),
203                                     $this->trans('commands.field.info.table.header-usage')
204                                 ] : [
205                                     $this->trans('commands.field.info.table.header-name'),
206                                     $this->trans('commands.field.info.table.header-type'),
207                                     $this->trans('commands.field.info.table.header-usage')
208                                 ];
209                                 $this->getIo()->table($tableHeader, $tableRows);
210                             } else {
211                                 $this->getIo()->comment(
212                                     $this->trans('commands.field.info.messages.fields-none')
213                                     . ' ' . $this->trans('commands.field.info.messages.in-bundle-type')
214                                     . " '" . $bundleType->label() . "'"
215                                 );
216                             }
217
218                             // Clear out the rows & headers arrays to start fresh.
219                             unset($tableHeader, $tableRows);
220
221                             // Create some space so the output looks nice.
222                             $this->getIo()->newLine();
223                         }
224                     }
225                 }
226             }
227         }
228
229         // If entity type was specified but not found then display error message.
230         if (!empty($entityTypeOption)) {
231             if (!$entityTypeOptionFound) {
232                 $this->getIo()->comment(
233                     $this->trans('commands.field.info.messages.entity-type') .
234                     ' ' . $entityTypeOption . ' ' .
235                     $this->trans('commands.field.info.messages.not-found')
236                 );
237             } elseif (!empty($bundleTypeOption) && !$bundleTypeOptionFound) {
238                 // If specified entity type found and bundle type specified but not found then display error message.
239                 $this->getIo()->comment(
240                     $this->trans('commands.field.info.messages.bundle-type') .
241                     ' ' . $bundleTypeOption . ' ' .
242                     $this->trans('commands.field.info.messages.not-found') .
243                     ' ' . $this->trans('commands.field.info.messages.in-entity-type') .
244                     ' ' . $entityTypeOption
245                 );
246             }
247         } elseif (!empty($bundleTypeOption) && !$bundleTypeOptionFound) {
248             // If specified bundle type not found then display error message.
249             $this->getIo()->comment(
250                 $this->trans('commands.field.info.messages.bundle-type') .
251                 ' ' . $bundleTypeOption . ' ' .
252                 $this->trans('commands.field.info.messages.not-found')
253             );
254         } elseif ($fieldCounter == 0) {
255             // If no fields found then display appropriate message.
256             $this->getIo()->comment($this->trans('commands.field.info.messages.fields-none'));
257         }
258
259         return 0;
260     }
261
262     /**
263      * Helper function to get the field definitions.
264      *
265      * @param  string $entityTypeId
266      *     The entity type we want to inspect.
267      * @param  string $bundle
268      *     The bundle we want to discover the fields of.
269      * @return array
270      *     An array of field storage definitions for the entity type,
271      *     keyed by field name.
272      */
273     private function getBundleFields($entityTypeId, $bundle)
274     {
275         $fields = [];
276         if (!empty($entityTypeId) && !empty($bundle)) {
277             $fields = array_filter(
278                 $this->entityFieldManager->getFieldDefinitions($entityTypeId, $bundle),
279                 function ($fieldDefinition) {
280                     return $fieldDefinition instanceof FieldConfigInterface;
281                 }
282             );
283         }
284
285         return $fields;
286     }
287 }