-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
FieldDeleteCommands.php
278 lines (233 loc) · 10.6 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
declare(strict_types=1);
namespace Drush\Commands\field;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\FieldConfigInterface;
use Drupal\field\FieldStorageConfigInterface;
use Drush\Attributes as CLI;
use Drush\Commands\AutowireTrait;
use Drush\Commands\DrushCommands;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Input\InputOption;
use function count;
use function dt;
use function field_purge_batch;
final class FieldDeleteCommands extends DrushCommands
{
use AutowireTrait;
use EntityTypeBundleAskTrait;
use EntityTypeBundleValidationTrait;
const DELETE = 'field:delete';
public function __construct(
protected EntityTypeManagerInterface $entityTypeManager,
protected EntityTypeBundleInfoInterface $entityTypeBundleInfo
) {
}
/**
* Delete a field
*
* @see \Drupal\field_ui\Form\FieldConfigDeleteForm
*/
#[CLI\Command(name: self::DELETE, aliases: ['field-delete', 'fd'])]
#[CLI\Argument(name: 'entityType', description: 'The machine name of the entity type.')]
#[CLI\Argument(name: 'bundle', description: 'The machine name of the bundle.')]
#[CLI\Option(name: 'field-name', description: 'The machine name of the field.')]
#[CLI\Option(name: 'all-bundles', description: 'Whether to delete the field from all bundles.')]
#[CLI\Option(name: 'show-machine-names', description: 'Show machine names instead of labels in option lists.')]
#[CLI\Usage(name: 'field:delete', description: 'Delete a field by answering the prompts.')]
#[CLI\Usage(name: 'field-delete taxonomy_term tag', description: 'Delete a field and fill in the remaining information through prompts.')]
#[CLI\Usage(name: 'field-delete taxonomy_term tag --field-name=field_tag_label', description: 'Delete a field in a non-interactive way.')]
#[CLI\Usage(name: 'field-delete taxonomy_term --field-name=field_tag_label --all-bundles', description: 'Delete a field from all bundles.')]
#[CLI\Complete(method_name_or_callable: 'complete')]
#[CLI\Version(version: '11.0')]
public function delete(?string $entityType = null, ?string $bundle = null, array $options = [
'field-name' => InputOption::VALUE_REQUIRED,
'show-machine-names' => InputOption::VALUE_OPTIONAL,
'all-bundles' => InputOption::VALUE_OPTIONAL,
]): void
{
$this->input->setArgument('entityType', $entityType ??= $this->askEntityType());
$this->validateEntityType($entityType);
$fieldName = $this->input->getOption('field-name') ?: $this->askExisting($entityType, $bundle);
$this->input->setOption('field-name', $fieldName);
if ($fieldName === null) {
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,
]);
if ($results === []) {
throw new \InvalidArgumentException(
dt("Field with name '!fieldName' does not exist.", [
'!fieldName' => $fieldName,
])
);
}
if (!$options['all-bundles']) {
$this->input->setArgument('bundle', $bundle = $bundle ?? $this->askBundle());
$this->validateBundle($entityType, $bundle);
/** @var FieldConfig[] $results */
$results = $this->entityTypeManager
->getStorage('field_config')
->loadByProperties([
'field_name' => $fieldName,
'entity_type' => $entityType,
'bundle' => $bundle,
]);
if ($results === []) {
throw new \InvalidArgumentException(
dt("Field with name '!fieldName' does not exist on bundle '!bundle'.", [
'!fieldName' => $fieldName,
'!bundle' => $bundle,
])
);
}
}
foreach ($results as $result) {
$this->deleteFieldConfig($result);
}
// 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);
}
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->getCompletionType() === CompletionInput::TYPE_ARGUMENT_VALUE) {
if ($input->getCompletionName() === 'entityType') {
$suggestions->suggestValues(array_keys($this->getFieldableEntityTypes()));
}
if ($input->getCompletionName() === 'bundle') {
$entityTypeId = $input->getArgument('entityType');
$bundleInfo = $this->entityTypeBundleInfo->getBundleInfo($entityTypeId);
$suggestions->suggestValues(array_keys($bundleInfo));
}
}
if ($input->getCompletionType() === CompletionInput::TYPE_OPTION_VALUE) {
if ($input->getCompletionName() === 'field-name') {
$entityTypeId = $input->getArgument('entityType');
if ($entityTypeId) {
$bundle = $input->getArgument('bundle');
$fieldNames = array_map(
fn (FieldConfig $fieldConfig) => $fieldConfig->get('field_name'),
$this->getFieldConfigs($entityTypeId, $bundle),
);
$suggestions->suggestValues($fieldNames);
}
}
}
}
protected function askExisting(string $entityType, ?string $bundle): ?string
{
$fieldConfigs = $this->getFieldConfigs($entityType, $bundle);
$choices = [];
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) ?: null;
}
protected function askBundle(): ?string
{
$entityTypeId = $this->input->getArgument('entityType');
$entityTypeDefinition = $this->entityTypeManager->getDefinition($entityTypeId);
$bundleEntityType = $entityTypeDefinition->getBundleEntityType();
$bundleInfo = $this->entityTypeBundleInfo->getBundleInfo($entityTypeId);
$choices = [];
if ($bundleEntityType && $bundleInfo === []) {
throw new \InvalidArgumentException(
dt("Entity type with id '!entityType' does not have any bundles.", ['!entityType' => $entityTypeId])
);
}
if ($fieldName = $this->input->getOption('field-name')) {
$bundleInfo = array_filter($bundleInfo, function (string $bundle) use ($entityTypeId, $fieldName) {
return $this->entityTypeManager->getStorage('field_config')->load("$entityTypeId.$bundle.$fieldName");
}, ARRAY_FILTER_USE_KEY);
}
if (!$bundleEntityType && count($bundleInfo) === 1) {
// eg. User
return $entityTypeId;
}
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(dt('The bundle argument is required.'));
}
return $answer;
}
/**
* Returns all field configs for the given entity type and bundle.
*
* @return FieldConfigInterface[]
*/
protected function getFieldConfigs(string $entityType, ?string $bundle): array
{
/** @var FieldConfigInterface[] $fieldConfigs */
$fieldConfigs = $this->entityTypeManager
->getStorage('field_config')
->loadByProperties([
'entity_type' => $entityType,
]);
if ($fieldConfigs === []) {
throw new \InvalidArgumentException(
dt("Entity type '!entityType' has no fields.", [
'!entityType' => $entityType,
])
);
}
if ($bundle !== null) {
/** @var FieldConfigInterface[] $fieldConfigs */
$fieldConfigs = $this->entityTypeManager
->getStorage('field_config')
->loadByProperties([
'entity_type' => $entityType,
'bundle' => $bundle,
]);
if ($fieldConfigs === []) {
throw new \InvalidArgumentException(
dt("Bundle '!bundle' has no fields.", [
'!bundle' => $bundle,
])
);
}
}
return $fieldConfigs;
}
protected function deleteFieldConfig(FieldConfigInterface $fieldConfig): void
{
$fieldStorage = $fieldConfig->getFieldStorageDefinition();
assert($fieldStorage instanceof FieldStorageConfigInterface);
$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(
dt($message, ['!field' => $fieldConfig->label(), '!type' => $bundleLabel])
);
}
}