Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add field:base-info command #4930

Merged
merged 4 commits into from Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
82 changes: 82 additions & 0 deletions src/Drupal/Commands/core/FieldBaseInfoCommands.php
@@ -0,0 +1,82 @@
<?php

namespace Drush\Drupal\Commands\core;

use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drush\Commands\DrushCommands;

class FieldBaseInfoCommands extends DrushCommands
{
use EntityTypeBundleAskTrait;
use EntityTypeBundleValidationTrait;
use FieldDefinitionRowsOfFieldsTrait;

/** @var EntityTypeManagerInterface */
protected $entityTypeManager;
/** @var EntityTypeBundleInfoInterface */
protected $entityTypeBundleInfo;
/** @var EntityFieldManagerInterface */
protected $entityFieldManager;

public function __construct(
EntityTypeManagerInterface $entityTypeManager,
EntityTypeBundleInfoInterface $entityTypeBundleInfo,
EntityFieldManagerInterface $entityFieldManager
) {
$this->entityTypeManager = $entityTypeManager;
$this->entityTypeBundleInfo = $entityTypeBundleInfo;
$this->entityFieldManager = $entityFieldManager;
}

/**
* List all base fields of an entity type
*
* @command field:base-info
* @aliases field-base-info,fbi
*
* @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 field:base-info taxonomy_term
* List all base fields.
* @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 = 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);
}
}
7 changes: 3 additions & 4 deletions src/Drupal/Commands/core/FieldInfoCommands.php
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/Drupal/Commands/core/drush.services.yml
Expand Up @@ -49,6 +49,14 @@ services:
- '@entity_type.bundle.info'
tags:
- { name: drush.command }
field.base-info.commands:
class: \Drush\Drupal\Commands\core\FieldBaseInfoCommands
arguments:
- '@entity_type.manager'
- '@entity_type.bundle.info'
- '@entity_field.manager'
tags:
- { name: drush.command }
link.hooks:
class: \Drush\Drupal\Commands\core\LinkHooks
arguments:
Expand Down
8 changes: 8 additions & 0 deletions tests/functional/FieldTest.php
Expand Up @@ -92,4 +92,12 @@ 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 testFieldBaseInfo()
{
$this->drush('field:base-info', ['user'], ['format' => 'json', 'fields' => '*']);
$json = $this->getOutputFromJSON();
$this->assertArrayHasKey('name', $json);
$this->assertSame('Name', $json['name']['label']);
}
}