forked from drush-ops/drush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntityTypeBundleAskTrait.php
63 lines (51 loc) · 2.08 KB
/
EntityTypeBundleAskTrait.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
<?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 EntityTypeBundleAskTrait
{
protected function askEntityType(): ?string
{
$entityTypeDefinitions = $this->entityTypeManager->getDefinitions();
$choices = [];
foreach ($entityTypeDefinitions as $entityTypeDefinition) {
$choices[$entityTypeDefinition->id()] = $this->input->getOption('show-machine-names')
? $entityTypeDefinition->id()
: $entityTypeDefinition->getLabel();
}
if (!$answer = $this->io()->choice('Entity type', $choices)) {
throw new \InvalidArgumentException(t('The entityType argument is required.'));
}
return $answer;
}
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;
}
}