-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
FieldBaseOverrideCreateCommands.php
201 lines (168 loc) · 8.45 KB
/
FieldBaseOverrideCreateCommands.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
<?php
declare(strict_types=1);
namespace Drush\Commands\field;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\Entity\BaseFieldOverride;
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 dt;
final class FieldBaseOverrideCreateCommands extends DrushCommands
{
use AutowireTrait;
use EntityTypeBundleAskTrait;
use EntityTypeBundleValidationTrait;
const BASE_OVERRIDE_CREATE = 'field:base-override-create';
public function __construct(
protected EntityTypeManagerInterface $entityTypeManager,
protected EntityTypeBundleInfoInterface $entityTypeBundleInfo,
protected EntityFieldManagerInterface $entityFieldManager
) {
}
/**
* Create a new base field override
*
* @see \Drupal\field_ui\Form\FieldConfigEditForm
* @see \Drupal\field_ui\Form\FieldStorageConfigEditForm
*/
#[CLI\Command(name: self::BASE_OVERRIDE_CREATE, aliases: ['bfoc'])]
#[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: 'A unique machine-readable name containing letters, numbers, and underscores.')]
#[CLI\Option(name: 'field-label', description: 'The field label')]
#[CLI\Option(name: 'field-description', description: 'The field description')]
#[CLI\Option(name: 'is-required', description: 'Whether the field is required')]
#[CLI\Option(name: 'show-machine-names', description: 'Show machine names instead of labels in option lists.')]
#[CLI\Usage(name: 'field:base-field-override-create', description: 'Create a base field override by answering the prompts.')]
#[CLI\Usage(name: 'field:base-field-override-create taxonomy_term tag', description: 'Create a base field override and fill in the remaining information through prompts.')]
#[CLI\Usage(name: 'field:base-field-override-create taxonomy_term tag --field-name=name --field-label=Label --is-required=1', description: 'Create a base field override in a completely non-interactive way.')]
#[CLI\Complete(method_name_or_callable: 'complete')]
#[CLI\Version(version: '11.0')]
public function baseOverrideCreateField(?string $entityType = null, ?string $bundle = null, array $options = [
'field-name' => InputOption::VALUE_REQUIRED,
'field-label' => InputOption::VALUE_REQUIRED,
'field-description' => InputOption::VALUE_REQUIRED,
'is-required' => 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->askFieldName($entityType);
$this->input->setOption('field-name', $fieldName);
if ($fieldName === '') {
throw new \InvalidArgumentException(dt('The !optionName option is required.', [
'!optionName' => 'field-name',
]));
}
/** @var BaseFieldOverride|BaseFieldDefinition $definition */
$definition = BaseFieldOverride::loadByName($entityType, $bundle, $fieldName)
?? $this->getBaseFieldDefinition($entityType, $fieldName);
if ($definition === null) {
throw new \InvalidArgumentException(
dt("Base field with name '!fieldName' does not exist on bundle '!bundle'.", [
'!fieldName' => $fieldName,
'!bundle' => $bundle,
])
);
}
$this->input->setOption(
'field-label',
$this->input->getOption('field-label') ?? $this->askFieldLabel((string) $definition->getLabel())
);
$this->input->setOption(
'field-description',
$this->input->getOption('field-description') ?? $this->askFieldDescription((string) $definition->getDescription())
);
$this->input->setOption(
'is-required',
(bool) ($this->input->getOption('is-required') ?? $this->askRequired($definition->isRequired()))
);
$fieldName = $this->input->getOption('field-name');
$fieldLabel = $this->input->getOption('field-label');
$fieldDescription = $this->input->getOption('field-description');
$isRequired = $this->input->getOption('is-required');
$baseFieldOverride = $this->createBaseFieldOverride($entityType, $bundle, $fieldName, $fieldLabel, $fieldDescription, $isRequired);
$this->logResult($baseFieldOverride);
}
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');
$definitions = $this->entityFieldManager->getBaseFieldDefinitions($entityTypeId);
$suggestions->suggestValues(array_keys($definitions));
}
}
}
protected function askFieldName(string $entityType): ?string
{
/** @var BaseFieldDefinition[] $definitions */
$definitions = $this->entityFieldManager->getBaseFieldDefinitions($entityType);
$choices = [];
foreach ($definitions as $definition) {
$label = $this->input->getOption('show-machine-names') ? $definition->getName() : (string) $definition->getLabel();
$choices[$definition->getName()] = $label;
}
return $this->io()->select('Field name', $choices);
}
protected function askFieldLabel(string $default): string
{
return $this->io()->ask('Field label', default: $default);
}
protected function askFieldDescription(?string $default): ?string
{
return $this->io()->ask('Field description', default: $default);
}
protected function askRequired(bool $default): bool
{
return $this->io()->confirm('Required', default: $default);
}
protected function createBaseFieldOverride(string $entityType, string $bundle, string $fieldName, $fieldLabel, $fieldDescription, bool $isRequired): BaseFieldOverride
{
$definition = $this->getBaseFieldDefinition($entityType, $fieldName);
$override = BaseFieldOverride::loadByName($entityType, $bundle, $fieldName)
?? BaseFieldOverride::createFromBaseFieldDefinition($definition, $bundle);
$override
->setLabel($fieldLabel)
->setDescription($fieldDescription)
->setRequired($isRequired)
->save();
return $override;
}
protected function logResult(BaseFieldOverride $baseFieldOverride): void
{
$this->logger()->success(
sprintf(
'Successfully created base field override \'%s\' on %s with bundle \'%s\'',
$baseFieldOverride->getName(),
$baseFieldOverride->getTargetEntityTypeId(),
$baseFieldOverride->getTargetBundle()
)
);
}
protected function getBaseFieldDefinition(string $entityType, string $fieldName): ?BaseFieldDefinition
{
/** @var BaseFieldDefinition[] $definitions */
$definitions = $this->entityFieldManager->getBaseFieldDefinitions($entityType);
return $definitions[$fieldName] ?? null;
}
}