-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add field:info command * Move formatting code to trait * Fix dependency injection * Add tests. * change name back. try to avoid conflict * phpcs Co-authored-by: Moshe Weitzman <weitzman@tejasa.com>
- Loading branch information
1 parent
cb2434e
commit 5794cdd
Showing
6 changed files
with
265 additions
and
2 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; | ||
} | ||
} |
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): RowsOfFields | ||
{ | ||
$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,87 @@ | ||
<?php | ||
|
||
namespace Drush\Drupal\Commands\core; | ||
|
||
use Consolidation\OutputFormatters\Options\FormatterOptions; | ||
use Consolidation\OutputFormatters\StructuredData\RowsOfFields; | ||
use Drupal\Core\Entity\EntityTypeBundleInfo; | ||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drush\Commands\DrushCommands; | ||
|
||
class FieldInfoCommands extends DrushCommands | ||
{ | ||
use AskBundleTrait; | ||
use FieldDefinitionRowsOfFieldsTrait; | ||
use ValidateEntityTypeTrait; | ||
|
||
/** @var EntityTypeManagerInterface */ | ||
protected $entityTypeManager; | ||
/** @var EntityTypeBundleInfo */ | ||
protected $entityTypeBundleInfo; | ||
|
||
public function __construct( | ||
EntityTypeManagerInterface $entityTypeManager, | ||
EntityTypeBundleInfo $entityTypeBundleInfo | ||
) { | ||
$this->entityTypeManager = $entityTypeManager; | ||
$this->entityTypeBundleInfo = $entityTypeBundleInfo; | ||
} | ||
|
||
/** | ||
* List all configurable fields of an entity bundle | ||
* | ||
* @command field:info | ||
* @aliases field-info,fi | ||
* | ||
* @param string $entityType | ||
* The machine name of the entity type | ||
* @param string $bundle | ||
* The machine name of the bundle | ||
* | ||
* @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-info taxonomy_term tag | ||
* List all fields. | ||
* @usage drush field:info | ||
* List all fields and fill in the remaining information through prompts. | ||
* | ||
* @version 11.0 | ||
*/ | ||
public function info(string $entityType, ?string $bundle = null, array $options = [ | ||
'format' => 'table', | ||
]): RowsOfFields | ||
{ | ||
$this->validateEntityType($entityType); | ||
|
||
$this->input->setArgument('bundle', $bundle = $bundle ?? $this->askBundle()); | ||
$this->validateBundle($entityType, $bundle); | ||
|
||
$fieldDefinitions = $this->entityTypeManager | ||
->getStorage('field_config') | ||
->loadByProperties([ | ||
'entity_type' => $entityType, | ||
'bundle' => $bundle, | ||
]); | ||
|
||
return $this->getRowsOfFieldsByFieldDefinitions($fieldDefinitions); | ||
} | ||
} |
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
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