-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
EntityTypeBundleValidationTrait.php
46 lines (38 loc) · 1.35 KB
/
EntityTypeBundleValidationTrait.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
<?php
namespace Drush\Drupal\Commands\core;
use Drupal\Core\Entity\EntityTypeManagerInterface;
/**
* @property EntityTypeManagerInterface $entityTypeManager
*/
trait EntityTypeBundleValidationTrait
{
protected function validateEntityType(string $entityTypeId): void
{
if (!$this->entityTypeManager->hasDefinition($entityTypeId)) {
throw new \InvalidArgumentException(
t("Entity type with id ':entityType' does not exist.", [':entityType' => $entityTypeId])
);
}
}
protected function validateBundle(string $entityTypeId, string $bundle): void
{
if (!$entityTypeDefinition = $this->entityTypeManager->getDefinition($entityTypeId)) {
return;
}
$bundleEntityType = $entityTypeDefinition->getBundleEntityType();
if ($bundleEntityType === null && $bundle === $entityTypeId) {
return;
}
$bundleDefinition = $this->entityTypeManager
->getStorage($bundleEntityType)
->load($bundle);
if (!$bundleDefinition) {
throw new \InvalidArgumentException(
t("Bundle ':bundle' does not exist on entity type with id ':entityType'.", [
':bundle' => $bundle,
':entityType' => $entityTypeId,
])
);
}
}
}