-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
FieldDeleteCommands.php
165 lines (141 loc) · 5.75 KB
/
FieldDeleteCommands.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace Drush\Drupal\Commands\field;
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;
use function count;
use function dt;
use function field_purge_batch;
use function t;
class FieldDeleteCommands extends DrushCommands
{
use EntityTypeBundleAskTrait;
use EntityTypeBundleValidationTrait;
/** @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
* 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 non-interactive way.
*
* @version 11.0
* @see \Drupal\field_ui\Form\FieldConfigDeleteForm
*/
public function delete(?string $entityType = null, ?string $bundle = null, array $options = [
'field-name' => InputOption::VALUE_REQUIRED,
'show-machine-names' => InputOption::VALUE_OPTIONAL,
]): void
{
$this->input->setArgument('entityType', $entityType = $entityType ?? $this->askEntityType());
$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;
}
if ($choices === []) {
throw new \InvalidArgumentException(
t("Bundle ':bundle' has no fields.", [
':bundle' => $bundle,
])
);
}
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 are no bundles left for this field storage, it will be
// deleted too, notify the user about dependencies.
if ($fieldStorage->getBundles() === []) {
$fieldStorage->delete();
}
$message = 'The field :field has been deleted from the :type bundle.';
} else {
$message = 'There was a problem removing the :field from the :type content type.';
}
$this->logger()->success(
t($message, [':field' => $fieldConfig->label(), ':type' => $bundleLabel])
);
}
}