From 8e7b9c28696513dedc5849a61b336eee2eda6cbe Mon Sep 17 00:00:00 2001 From: Dieter Holvoet Date: Sun, 12 Dec 2021 21:47:21 +0100 Subject: [PATCH] Add base-field:info command --- src/Drupal/Commands/core/AskBundleTrait.php | 45 +++++++++++ .../Commands/core/BaseFieldInfoCommands.php | 76 +++++++++++++++++++ .../core/FieldDefinitionRowsOfFieldsTrait.php | 59 ++++++++++++++ .../Commands/core/ValidateEntityTypeTrait.php | 46 +++++++++++ src/Drupal/Commands/core/drush.services.yml | 8 ++ 5 files changed, 234 insertions(+) create mode 100644 src/Drupal/Commands/core/AskBundleTrait.php create mode 100644 src/Drupal/Commands/core/BaseFieldInfoCommands.php create mode 100644 src/Drupal/Commands/core/FieldDefinitionRowsOfFieldsTrait.php create mode 100644 src/Drupal/Commands/core/ValidateEntityTypeTrait.php diff --git a/src/Drupal/Commands/core/AskBundleTrait.php b/src/Drupal/Commands/core/AskBundleTrait.php new file mode 100644 index 0000000000..1e692474fd --- /dev/null +++ b/src/Drupal/Commands/core/AskBundleTrait.php @@ -0,0 +1,45 @@ +input->getArgument('entityType'); + $entityTypeDefinition = $this->entityTypeManager->getDefinition($entityTypeId); + $bundleEntityType = $entityTypeDefinition->getBundleEntityType(); + $bundleInfo = $this->entityTypeBundleInfo->getBundleInfo($entityTypeId); + $choices = []; + + if (empty($bundleInfo)) { + if ($bundleEntityType) { + throw new \InvalidArgumentException( + t('Entity type with id \':entityType\' does not have any bundles.', [':entityType' => $entityTypeId]) + ); + } + + return null; + } + + foreach ($bundleInfo as $bundle => $data) { + $label = $this->input->getOption('show-machine-names') ? $bundle : $data['label']; + $choices[$bundle] = $label; + } + + if (!$answer = $this->io()->choice('Bundle', $choices)) { + throw new \InvalidArgumentException(t('The bundle argument is required.')); + } + + return $answer; + } +} diff --git a/src/Drupal/Commands/core/BaseFieldInfoCommands.php b/src/Drupal/Commands/core/BaseFieldInfoCommands.php new file mode 100644 index 0000000000..e97fab62cd --- /dev/null +++ b/src/Drupal/Commands/core/BaseFieldInfoCommands.php @@ -0,0 +1,76 @@ +entityTypeManager = $entityTypeManager; + $this->entityTypeBundleInfo = $entityTypeBundleInfo; + $this->entityFieldManager = $entityFieldManager; + } + + /** + * List all base fields of an entity type + * + * @command base-field:info + * @aliases base-field-info,bfi + * + * @param string $entityType + * The machine name of the entity type + * + * @option show-machine-names + * Show machine names instead of labels in option lists. + * + * @default-fields field_name,required,field_type,cardinality + * @field-labels + * label: Label + * description: Description + * field_name: Field name + * field_type: Field type + * required: Required + * translatable: Translatable + * cardinality: Cardinality + * default_value: Default value + * default_value_callback: Default value callback + * allowed_values: Allowed values + * allowed_values_function: Allowed values function + * handler: Selection handler + * target_bundles: Target bundles + * @filter-default-field field_name + * @table-style default + * + * @usage drush base-field-info taxonomy_term + * List all base fields. + * @usage drush base-field:info + * List all base fields and fill in the remaining information through prompts. + */ + public function info(string $entityType, array $options = [ + 'format' => 'table', + ]): RowsOfFields + { + $fieldDefinitions = $this->entityFieldManager->getBaseFieldDefinitions($entityType); + + return $this->getRowsOfFieldsByFieldDefinitions($fieldDefinitions); + } +} diff --git a/src/Drupal/Commands/core/FieldDefinitionRowsOfFieldsTrait.php b/src/Drupal/Commands/core/FieldDefinitionRowsOfFieldsTrait.php new file mode 100644 index 0000000000..eec31e803d --- /dev/null +++ b/src/Drupal/Commands/core/FieldDefinitionRowsOfFieldsTrait.php @@ -0,0 +1,59 @@ +getFieldStorageDefinition(); + $handlerSettings = $field->getSetting('handler_settings'); + + $rows[$field->getName()] = [ + 'label' => $field->getLabel(), + 'description' => $field->getDescription(), + 'field_name' => $field->getName(), + 'field_type' => $field->getType(), + 'required' => $field->isRequired(), + 'translatable' => $field->isTranslatable(), + 'cardinality' => $storage->getCardinality(), + 'default_value' => empty($field->getDefaultValueLiteral()) ? null : $field->getDefaultValueLiteral(), + 'default_value_callback' => $field->getDefaultValueCallback(), + 'allowed_values' => $storage->getSetting('allowed_values'), + 'allowed_values_function' => $storage->getSetting('allowed_values_function'), + 'handler' => $field->getSetting('handler'), + 'target_bundles' => $handlerSettings['target_bundles'] ?? null, + ]; + } + + $result = new RowsOfFields($rows); + $result->addRendererFunction([$this, 'renderArray']); + $result->addRendererFunction([$this, 'renderBoolean']); + + return $result; + } +} diff --git a/src/Drupal/Commands/core/ValidateEntityTypeTrait.php b/src/Drupal/Commands/core/ValidateEntityTypeTrait.php new file mode 100644 index 0000000000..7c968efc30 --- /dev/null +++ b/src/Drupal/Commands/core/ValidateEntityTypeTrait.php @@ -0,0 +1,46 @@ +entityTypeManager->hasDefinition($entityTypeId)) { + throw new \InvalidArgumentException( + t("Entity type with id ':entityType' does not exist.", [':entityType' => $entityTypeId]) + ); + } + } + + protected function validateBundle(string $entityTypeId, string $bundle): void + { + if (!$entityTypeDefinition = $this->entityTypeManager->getDefinition($entityTypeId)) { + return; + } + + $bundleEntityType = $entityTypeDefinition->getBundleEntityType(); + + if ($bundleEntityType === null && $bundle === $entityTypeId) { + return; + } + + $bundleDefinition = $this->entityTypeManager + ->getStorage($bundleEntityType) + ->load($bundle); + + if (!$bundleDefinition) { + throw new \InvalidArgumentException( + t("Bundle ':bundle' does not exist on entity type with id ':entityType'.", [ + ':bundle' => $bundle, + ':entityType' => $entityTypeId, + ]) + ); + } + } +} diff --git a/src/Drupal/Commands/core/drush.services.yml b/src/Drupal/Commands/core/drush.services.yml index e70ea33f66..0b2bc214b2 100644 --- a/src/Drupal/Commands/core/drush.services.yml +++ b/src/Drupal/Commands/core/drush.services.yml @@ -35,6 +35,14 @@ services: - [ setContentTranslationManager, [ '@?content_translation.manager' ] ] tags: - { name: drush.command } + base-field.info.commands: + class: \Drush\Drupal\Commands\core\BaseFieldInfoCommands + arguments: + - '@entity_type.manager' + - '@entity_type.bundle.info' + - '@entity_field.manager' + tags: + - { name: drush.command } link.hooks: class: \Drush\Drupal\Commands\core\LinkHooks arguments: