From a038896a4c098d49097ecda8449eb41fd9411816 Mon Sep 17 00:00:00 2001 From: Dieter Holvoet Date: Sun, 12 Dec 2021 21:47:21 +0100 Subject: [PATCH 1/4] Add base-field:info command --- .../Commands/core/BaseFieldInfoCommands.php | 76 +++++++++++++++++++ src/Drupal/Commands/core/drush.services.yml | 8 ++ 2 files changed, 84 insertions(+) create mode 100644 src/Drupal/Commands/core/BaseFieldInfoCommands.php 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/drush.services.yml b/src/Drupal/Commands/core/drush.services.yml index 2aaac6c711..b1fb896abc 100644 --- a/src/Drupal/Commands/core/drush.services.yml +++ b/src/Drupal/Commands/core/drush.services.yml @@ -49,6 +49,14 @@ services: - '@entity_type.bundle.info' 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: From 576c76bb47440a56e70cbedbf48cc65b139a18b9 Mon Sep 17 00:00:00 2001 From: Moshe Weitzman Date: Thu, 16 Dec 2021 23:28:28 -0500 Subject: [PATCH 2/4] WIP tests. --- .../Commands/core/BaseFieldInfoCommands.php | 2 ++ tests/functional/FieldTest.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/Drupal/Commands/core/BaseFieldInfoCommands.php b/src/Drupal/Commands/core/BaseFieldInfoCommands.php index e97fab62cd..cfc2acecc1 100644 --- a/src/Drupal/Commands/core/BaseFieldInfoCommands.php +++ b/src/Drupal/Commands/core/BaseFieldInfoCommands.php @@ -64,6 +64,8 @@ public function __construct( * List all base fields. * @usage drush base-field:info * List all base fields and fill in the remaining information through prompts. + * + * @version 11.0 */ public function info(string $entityType, array $options = [ 'format' => 'table', diff --git a/tests/functional/FieldTest.php b/tests/functional/FieldTest.php index 3524ac3d35..8aed28dc5e 100644 --- a/tests/functional/FieldTest.php +++ b/tests/functional/FieldTest.php @@ -92,4 +92,20 @@ public function testFieldDelete() $this->drush('field:delete', ['unish_article', 'alpha'], ['field-name' => 'field_test5']); $this->assertStringContainsString(" The field Test has been deleted from the Alpha bundle.", $this->getErrorOutputRaw()); } + + public function testBaseFieldInfo() + { + // $this->drush('field:create', ['unish_article', 'alpha'], ['field-label' => 'Test', 'field-name' => 'field_test4', 'field-description' => 'baz', 'field-type' => 'entity_reference', 'is-required' => true, 'field-widget' => 'entity_reference_autocomplete', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, 'target-type' => 'unish_article', 'target-bundle' => 'beta']); + // $this->assertStringContainsString("Successfully created field 'field_test4' on unish_article type with bundle 'alpha'", $this->getSimplifiedErrorOutput()); + +// $this->drush('base-field:info', ['user'], ['format' => 'json', 'fields' => '*']); +// $json = $this->getOutputFromJSON(); +// $this->assertSame('field_test4', $json['field_name']); +// $this->assertTrue($json['required']); +// $this->assertSame('entity_reference', $json['field_type']); +// $this->assertSame('baz', $json['description']); +// $this->assertSame(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, $json['cardinality']); +// $this->assertFalse($json['translatable']); +// $this->assertArrayHasKey('beta', $json['target_bundles']); + } } From 181dc9644d06c59f36cf948c883c7cd77468420f Mon Sep 17 00:00:00 2001 From: Dieter Holvoet Date: Fri, 17 Dec 2021 13:11:06 +0100 Subject: [PATCH 3/4] Rename command to field:base-info + change class type hints to interfaces + fix renamed traits + make entityType argument optional --- ...Commands.php => FieldBaseInfoCommands.php} | 26 +++++++++++-------- .../Commands/core/FieldInfoCommands.php | 7 +++-- src/Drupal/Commands/core/drush.services.yml | 4 +-- 3 files changed, 20 insertions(+), 17 deletions(-) rename src/Drupal/Commands/core/{BaseFieldInfoCommands.php => FieldBaseInfoCommands.php} (74%) diff --git a/src/Drupal/Commands/core/BaseFieldInfoCommands.php b/src/Drupal/Commands/core/FieldBaseInfoCommands.php similarity index 74% rename from src/Drupal/Commands/core/BaseFieldInfoCommands.php rename to src/Drupal/Commands/core/FieldBaseInfoCommands.php index cfc2acecc1..19017008da 100644 --- a/src/Drupal/Commands/core/BaseFieldInfoCommands.php +++ b/src/Drupal/Commands/core/FieldBaseInfoCommands.php @@ -2,27 +2,28 @@ namespace Drush\Drupal\Commands\core; +use Consolidation\OutputFormatters\StructuredData\RowsOfFields; use Drupal\Core\Entity\EntityFieldManagerInterface; -use Drupal\Core\Entity\EntityTypeBundleInfo; +use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drush\Commands\DrushCommands; -class BaseFieldInfoCommands extends DrushCommands +class FieldBaseInfoCommands extends DrushCommands { - use AskBundleTrait; + use EntityTypeBundleAskTrait; + use EntityTypeBundleValidationTrait; use FieldDefinitionRowsOfFieldsTrait; - use ValidateEntityTypeTrait; /** @var EntityTypeManagerInterface */ protected $entityTypeManager; - /** @var EntityTypeBundleInfo */ + /** @var EntityTypeBundleInfoInterface */ protected $entityTypeBundleInfo; /** @var EntityFieldManagerInterface */ protected $entityFieldManager; public function __construct( EntityTypeManagerInterface $entityTypeManager, - EntityTypeBundleInfo $entityTypeBundleInfo, + EntityTypeBundleInfoInterface $entityTypeBundleInfo, EntityFieldManagerInterface $entityFieldManager ) { $this->entityTypeManager = $entityTypeManager; @@ -33,8 +34,8 @@ public function __construct( /** * List all base fields of an entity type * - * @command base-field:info - * @aliases base-field-info,bfi + * @command field:base-info + * @aliases field-base-info,fbi * * @param string $entityType * The machine name of the entity type @@ -60,17 +61,20 @@ public function __construct( * @filter-default-field field_name * @table-style default * - * @usage drush base-field-info taxonomy_term + * @usage drush field:base-info taxonomy_term * List all base fields. - * @usage drush base-field:info + * @usage drush field:base-info * List all base fields and fill in the remaining information through prompts. * * @version 11.0 */ - public function info(string $entityType, array $options = [ + public function info(?string $entityType = null, array $options = [ 'format' => 'table', ]): RowsOfFields { + $this->input->setArgument('entityType', $entityType = $entityType ?? $this->askEntityType()); + $this->validateEntityType($entityType); + $fieldDefinitions = $this->entityFieldManager->getBaseFieldDefinitions($entityType); return $this->getRowsOfFieldsByFieldDefinitions($fieldDefinitions); diff --git a/src/Drupal/Commands/core/FieldInfoCommands.php b/src/Drupal/Commands/core/FieldInfoCommands.php index 5560ffcea7..68c6770b71 100644 --- a/src/Drupal/Commands/core/FieldInfoCommands.php +++ b/src/Drupal/Commands/core/FieldInfoCommands.php @@ -2,9 +2,8 @@ namespace Drush\Drupal\Commands\core; -use Consolidation\OutputFormatters\Options\FormatterOptions; use Consolidation\OutputFormatters\StructuredData\RowsOfFields; -use Drupal\Core\Entity\EntityTypeBundleInfo; +use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drush\Commands\DrushCommands; @@ -16,12 +15,12 @@ class FieldInfoCommands extends DrushCommands /** @var EntityTypeManagerInterface */ protected $entityTypeManager; - /** @var EntityTypeBundleInfo */ + /** @var EntityTypeBundleInfoInterface */ protected $entityTypeBundleInfo; public function __construct( EntityTypeManagerInterface $entityTypeManager, - EntityTypeBundleInfo $entityTypeBundleInfo + EntityTypeBundleInfoInterface $entityTypeBundleInfo ) { $this->entityTypeManager = $entityTypeManager; $this->entityTypeBundleInfo = $entityTypeBundleInfo; diff --git a/src/Drupal/Commands/core/drush.services.yml b/src/Drupal/Commands/core/drush.services.yml index b1fb896abc..a5d6faf5e9 100644 --- a/src/Drupal/Commands/core/drush.services.yml +++ b/src/Drupal/Commands/core/drush.services.yml @@ -49,8 +49,8 @@ services: - '@entity_type.bundle.info' tags: - { name: drush.command } - base-field.info.commands: - class: \Drush\Drupal\Commands\core\BaseFieldInfoCommands + field.base-info.commands: + class: \Drush\Drupal\Commands\core\FieldBaseInfoCommands arguments: - '@entity_type.manager' - '@entity_type.bundle.info' From 7c29d0085d87af3b425f0662d142ceba2481a435 Mon Sep 17 00:00:00 2001 From: Moshe Weitzman Date: Fri, 17 Dec 2021 14:57:49 -0500 Subject: [PATCH 4/4] Add tests --- tests/functional/FieldTest.php | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/tests/functional/FieldTest.php b/tests/functional/FieldTest.php index 8aed28dc5e..0a4d4488ea 100644 --- a/tests/functional/FieldTest.php +++ b/tests/functional/FieldTest.php @@ -93,19 +93,11 @@ public function testFieldDelete() $this->assertStringContainsString(" The field Test has been deleted from the Alpha bundle.", $this->getErrorOutputRaw()); } - public function testBaseFieldInfo() + public function testFieldBaseInfo() { - // $this->drush('field:create', ['unish_article', 'alpha'], ['field-label' => 'Test', 'field-name' => 'field_test4', 'field-description' => 'baz', 'field-type' => 'entity_reference', 'is-required' => true, 'field-widget' => 'entity_reference_autocomplete', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, 'target-type' => 'unish_article', 'target-bundle' => 'beta']); - // $this->assertStringContainsString("Successfully created field 'field_test4' on unish_article type with bundle 'alpha'", $this->getSimplifiedErrorOutput()); - -// $this->drush('base-field:info', ['user'], ['format' => 'json', 'fields' => '*']); -// $json = $this->getOutputFromJSON(); -// $this->assertSame('field_test4', $json['field_name']); -// $this->assertTrue($json['required']); -// $this->assertSame('entity_reference', $json['field_type']); -// $this->assertSame('baz', $json['description']); -// $this->assertSame(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, $json['cardinality']); -// $this->assertFalse($json['translatable']); -// $this->assertArrayHasKey('beta', $json['target_bundles']); + $this->drush('field:base-info', ['user'], ['format' => 'json', 'fields' => '*']); + $json = $this->getOutputFromJSON(); + $this->assertArrayHasKey('name', $json); + $this->assertSame('Name', $json['name']['label']); } }