Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
33c5631
commit 8e7b9c2
Showing
5 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
59
src/Drupal/Commands/core/FieldDefinitionRowsOfFieldsTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
]) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters