EntityDataDefinition.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace Drupal\Core\Entity\TypedData;
  3. use Drupal\Core\TypedData\ComplexDataDefinitionBase;
  4. /**
  5. * A typed data definition class for describing entities.
  6. */
  7. class EntityDataDefinition extends ComplexDataDefinitionBase implements EntityDataDefinitionInterface {
  8. /**
  9. * Creates a new entity definition.
  10. *
  11. * @param string $entity_type_id
  12. * (optional) The ID of the entity type, or NULL if the entity type is
  13. * unknown. Defaults to NULL.
  14. *
  15. * @return static
  16. */
  17. public static function create($entity_type_id = NULL, $bundle = NULL) {
  18. // If the entity type is known, use the derived definition.
  19. if (isset($entity_type_id)) {
  20. $data_type = "entity:{$entity_type_id}";
  21. // If a bundle was given, use the bundle-specific definition.
  22. if ($bundle) {
  23. $data_type .= ":{$bundle}";
  24. }
  25. // It's possible that the given entity type ID or bundle wasn't discovered
  26. // by the TypedData plugin manager and/or weren't created by the
  27. // EntityDeriver. In that case, this is a new definition and we'll just
  28. // create the definition from defaults by using an empty array.
  29. $values = \Drupal::typedDataManager()->getDefinition($data_type, FALSE);
  30. $definition = new static(is_array($values) ? $values : []);
  31. // Set the EntityType constraint using the given entity type ID.
  32. $definition->setEntityTypeId($entity_type_id);
  33. // If available, set the Bundle constraint.
  34. if ($bundle) {
  35. $definition->setBundles([$bundle]);
  36. }
  37. return $definition;
  38. }
  39. return new static([]);
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public static function createFromDataType($data_type) {
  45. $parts = explode(':', $data_type);
  46. if ($parts[0] != 'entity') {
  47. throw new \InvalidArgumentException('Data type must be in the form of "entity:ENTITY_TYPE:BUNDLE."');
  48. }
  49. return static::create(
  50. isset($parts[1]) ? $parts[1] : NULL,
  51. isset($parts[2]) ? $parts[2] : NULL
  52. );
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function getPropertyDefinitions() {
  58. if (!isset($this->propertyDefinitions)) {
  59. if ($entity_type_id = $this->getEntityTypeId()) {
  60. // Return an empty array for entities that are not content entities.
  61. $entity_type_class = \Drupal::entityTypeManager()->getDefinition($entity_type_id)->getClass();
  62. if (!in_array('Drupal\Core\Entity\FieldableEntityInterface', class_implements($entity_type_class))) {
  63. $this->propertyDefinitions = [];
  64. }
  65. else {
  66. // @todo: Add support for handling multiple bundles.
  67. // See https://www.drupal.org/node/2169813.
  68. $bundles = $this->getBundles();
  69. if (is_array($bundles) && count($bundles) == 1) {
  70. $this->propertyDefinitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type_id, reset($bundles));
  71. }
  72. else {
  73. $this->propertyDefinitions = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions($entity_type_id);
  74. }
  75. }
  76. }
  77. else {
  78. // No entity type given.
  79. $this->propertyDefinitions = [];
  80. }
  81. }
  82. return $this->propertyDefinitions;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function getDataType() {
  88. $type = 'entity';
  89. if ($entity_type = $this->getEntityTypeId()) {
  90. $type .= ':' . $entity_type;
  91. // Append the bundle only if we know it for sure and it is not the default
  92. // bundle.
  93. if (($bundles = $this->getBundles()) && count($bundles) == 1) {
  94. $bundle = reset($bundles);
  95. if ($bundle != $entity_type) {
  96. $type .= ':' . $bundle;
  97. }
  98. }
  99. }
  100. return $type;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function getEntityTypeId() {
  106. return isset($this->definition['constraints']['EntityType']) ? $this->definition['constraints']['EntityType'] : NULL;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function setEntityTypeId($entity_type_id) {
  112. return $this->addConstraint('EntityType', $entity_type_id);
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function getBundles() {
  118. $bundle = isset($this->definition['constraints']['Bundle']) ? $this->definition['constraints']['Bundle'] : NULL;
  119. return is_string($bundle) ? [$bundle] : $bundle;
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function setBundles(array $bundles = NULL) {
  125. if (isset($bundles)) {
  126. $this->addConstraint('Bundle', $bundles);
  127. }
  128. else {
  129. // Remove the constraint.
  130. unset($this->definition['constraints']['Bundle']);
  131. }
  132. return $this;
  133. }
  134. }