Skip to content

Commit

Permalink
Add base-field:info command
Browse files Browse the repository at this point in the history
  • Loading branch information
DieterHolvoet committed Dec 12, 2021
1 parent 33c5631 commit 8e7b9c2
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/Drupal/Commands/core/AskBundleTrait.php
@@ -0,0 +1,45 @@
<?php

namespace Drush\Drupal\Commands\core;

use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\Console\Input\InputInterface;

/**
* @property InputInterface $input
* @property EntityTypeBundleInfoInterface $entityTypeBundleInfo
* @property EntityTypeManagerInterface $entityTypeManager
*/
trait AskBundleTrait
{
protected function askBundle(): ?string
{
$entityTypeId = $this->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;
}
}
76 changes: 76 additions & 0 deletions src/Drupal/Commands/core/BaseFieldInfoCommands.php
@@ -0,0 +1,76 @@
<?php

namespace Drush\Drupal\Commands\core;

use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfo;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drush\Commands\DrushCommands;

class BaseFieldInfoCommands extends DrushCommands
{
use AskBundleTrait;
use FieldDefinitionRowsOfFieldsTrait;
use ValidateEntityTypeTrait;

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

public function __construct(
EntityTypeManagerInterface $entityTypeManager,
EntityTypeBundleInfo $entityTypeBundleInfo,
EntityFieldManagerInterface $entityFieldManager
) {
$this->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);
}
}
59 changes: 59 additions & 0 deletions src/Drupal/Commands/core/FieldDefinitionRowsOfFieldsTrait.php
@@ -0,0 +1,59 @@
<?php

namespace Drush\Drupal\Commands\core;

use Consolidation\OutputFormatters\Options\FormatterOptions;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;

trait FieldDefinitionRowsOfFieldsTrait
{
public function renderArray($key, $value, FormatterOptions $options)
{
if (is_array($value)) {
return implode(', ', $value);
}

return $value;
}

public function renderBoolean($key, $value, FormatterOptions $options)
{
if (is_bool($value)) {
return $value ? '✔' : '';
}

return $value;
}

protected function getRowsOfFieldsByFieldDefinitions(array $fieldDefinitions): array
{
$rows = [];

foreach ($fieldDefinitions as $field) {
$storage = $field->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;
}
}
46 changes: 46 additions & 0 deletions src/Drupal/Commands/core/ValidateEntityTypeTrait.php
@@ -0,0 +1,46 @@
<?php

namespace Drush\Drupal\Commands\core;

use Drupal\Core\Entity\EntityTypeManagerInterface;

/**
* @property EntityTypeManagerInterface $entityTypeManager
*/
trait ValidateEntityTypeTrait
{
protected function validateEntityType(string $entityTypeId): void
{
if (!$this->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,
])
);
}
}
}
8 changes: 8 additions & 0 deletions src/Drupal/Commands/core/drush.services.yml
Expand Up @@ -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:
Expand Down

0 comments on commit 8e7b9c2

Please sign in to comment.