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:delete command #4926
Add field:delete command #4926
Changes from 3 commits
2e43e51
12e7564
09f0ffc
6bf370c
ec31865
420af5e
2baa05f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
<?php | ||
|
||
namespace Drush\Drupal\Commands\core; | ||
|
||
use Drupal\Core\Entity\EntityTypeBundleInfo; | ||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\field\Entity\FieldConfig; | ||
use Drupal\field\FieldConfigInterface; | ||
use Drush\Commands\DrushCommands; | ||
use Symfony\Component\Console\Input\InputOption; | ||
|
||
class FieldDeleteCommands extends DrushCommands | ||
{ | ||
use AskBundleTrait; | ||
use ValidateEntityTypeTrait; | ||
|
||
/** @var EntityTypeManagerInterface */ | ||
protected $entityTypeManager; | ||
/** @var EntityTypeBundleInfo */ | ||
protected $entityTypeBundleInfo; | ||
|
||
public function __construct( | ||
EntityTypeManagerInterface $entityTypeManager, | ||
EntityTypeBundleInfo $entityTypeBundleInfo | ||
) { | ||
$this->entityTypeManager = $entityTypeManager; | ||
$this->entityTypeBundleInfo = $entityTypeBundleInfo; | ||
} | ||
|
||
/** | ||
* Delete a field | ||
* | ||
* @command field:delete | ||
* @aliases field-delete,fd | ||
* | ||
* @param string $entityType | ||
* The machine name of the entity type | ||
* @param string $bundle | ||
* The machine name of the bundle | ||
* | ||
* @option field-name | ||
* The machine name of the field | ||
* | ||
* @option show-machine-names | ||
* Show machine names instead of labels in option lists. | ||
* | ||
* @usage drush field:delete | ||
DieterHolvoet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Delete a field by answering the prompts. | ||
* @usage drush field-delete taxonomy_term tag | ||
* Delete a field and fill in the remaining information through prompts. | ||
* @usage drush field-delete taxonomy_term tag --field-name=field_tag_label | ||
* Delete a field in a completely non-interactive way. | ||
* | ||
* @version 11.0 | ||
*/ | ||
public function delete(string $entityType, ?string $bundle = null, array $options = [ | ||
'field-name' => InputOption::VALUE_REQUIRED, | ||
'show-machine-names' => InputOption::VALUE_OPTIONAL, | ||
]): void | ||
{ | ||
$this->validateEntityType($entityType); | ||
|
||
$this->input->setArgument('bundle', $bundle = $bundle ?? $this->askBundle()); | ||
$this->validateBundle($entityType, $bundle); | ||
|
||
$fieldName = $this->input->getOption('field-name') ?? $this->askExisting($entityType, $bundle); | ||
$this->input->setOption('field-name', $fieldName); | ||
|
||
if ($fieldName === '') { | ||
throw new \InvalidArgumentException(dt('The %optionName option is required.', [ | ||
'%optionName' => 'field-name', | ||
])); | ||
} | ||
|
||
/** @var FieldConfig[] $results */ | ||
$results = $this->entityTypeManager | ||
->getStorage('field_config') | ||
->loadByProperties([ | ||
'field_name' => $fieldName, | ||
'entity_type' => $entityType, | ||
'bundle' => $bundle, | ||
]); | ||
|
||
if ($results === []) { | ||
throw new \InvalidArgumentException( | ||
t("Field with name ':fieldName' does not exist on bundle ':bundle'.", [ | ||
':fieldName' => $fieldName, | ||
':bundle' => $bundle, | ||
]) | ||
); | ||
} | ||
|
||
$this->deleteFieldConfig(reset($results)); | ||
|
||
// Fields are purged on cron. However field module prevents disabling modules | ||
// when field types they provided are used in a field until it is fully | ||
// purged. In the case that a field has minimal or no content, a single call | ||
// to field_purge_batch() will remove it from the system. Call this with a | ||
// low batch limit to avoid administrators having to wait for cron runs when | ||
// removing fields that meet this criteria. | ||
field_purge_batch(10); | ||
} | ||
|
||
protected function askExisting(string $entityType, string $bundle): string | ||
{ | ||
$choices = []; | ||
/** @var FieldConfigInterface[] $fieldConfigs */ | ||
$fieldConfigs = $this->entityTypeManager | ||
->getStorage('field_config') | ||
->loadByProperties([ | ||
'entity_type' => $entityType, | ||
'bundle' => $bundle, | ||
]); | ||
|
||
foreach ($fieldConfigs as $fieldConfig) { | ||
$label = $this->input->getOption('show-machine-names') | ||
? $fieldConfig->get('field_name') | ||
: $fieldConfig->get('label'); | ||
|
||
$choices[$fieldConfig->get('field_name')] = $label; | ||
} | ||
|
||
return $this->io()->choice('Choose a field to delete', $choices); | ||
} | ||
|
||
protected function deleteFieldConfig(FieldConfigInterface $fieldConfig): void | ||
{ | ||
$fieldStorage = $fieldConfig->getFieldStorageDefinition(); | ||
$bundles = $this->entityTypeBundleInfo->getBundleInfo($fieldConfig->getTargetEntityTypeId()); | ||
$bundleLabel = $bundles[$fieldConfig->getTargetBundle()]['label']; | ||
|
||
if ($fieldStorage && !$fieldStorage->isLocked()) { | ||
$fieldConfig->delete(); | ||
|
||
// If there is only one bundle left for this field storage, it will be | ||
// deleted too, notify the user about dependencies. | ||
if (count($fieldStorage->getBundles()) <= 1) { | ||
$fieldStorage->delete(); | ||
} | ||
|
||
$message = 'The field :field has been deleted from the :type content type.'; | ||
} else { | ||
$message = 'There was a problem removing the :field from the :type content type.'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Drupal will not let you delete a field with data in it. Can we show that exception message, instead of "there was a problem"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used the exact same error message as shown in the UI: https://git.drupalcode.org/project/drupal/-/blob/9.3.x/core/modules/field_ui/src/Form/FieldConfigDeleteForm.php#L104 Isn't it possible that the field storage being locked can have other causes? Because if that's the case, we should keep the error message generic, just like |
||
} | ||
|
||
$this->logger()->success( | ||
t($message, [':field' => $fieldConfig->label(), ':type' => $bundleLabel]) | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we don't use a CustomEvent directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do call the
getCustomEventHandlers
method inFieldCreateCommands
, right?