EntityReference.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Drupal\Core\Entity\Plugin\DataType;
  3. use Drupal\Core\Entity\EntityInterface;
  4. use Drupal\Core\TypedData\DataReferenceBase;
  5. /**
  6. * Defines an 'entity_reference' data type.
  7. *
  8. * This serves as 'entity' property of entity reference field items and gets
  9. * its value set from the parent, i.e. LanguageItem.
  10. *
  11. * The plain value of this reference is the entity object, i.e. an instance of
  12. * \Drupal\Core\Entity\EntityInterface. For setting the value the entity object
  13. * or the entity ID may be passed.
  14. *
  15. * Note that the definition of the referenced entity's type is required, whereas
  16. * defining referenceable entity bundle(s) is optional. A reference defining the
  17. * type and bundle of the referenced entity can be created as following:
  18. * @code
  19. * $definition = \Drupal\Core\Entity\EntityDefinition::create($entity_type)
  20. * ->addConstraint('Bundle', $bundle);
  21. * \Drupal\Core\TypedData\DataReferenceDefinition::create('entity')
  22. * ->setTargetDefinition($definition);
  23. * @endcode
  24. *
  25. * @DataType(
  26. * id = "entity_reference",
  27. * label = @Translation("Entity reference"),
  28. * definition_class = "\Drupal\Core\TypedData\DataReferenceDefinition"
  29. * )
  30. */
  31. class EntityReference extends DataReferenceBase {
  32. /**
  33. * The entity ID.
  34. *
  35. * @var int|string
  36. */
  37. protected $id;
  38. /**
  39. * Gets the definition of the referenced entity.
  40. *
  41. * @return \Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface
  42. * The reference target's definition.
  43. */
  44. public function getTargetDefinition() {
  45. return $this->definition->getTargetDefinition();
  46. }
  47. /**
  48. * Checks whether the target entity has not been saved yet.
  49. *
  50. * @return bool
  51. * TRUE if the entity is new, FALSE otherwise.
  52. */
  53. public function isTargetNew() {
  54. // If only an ID is given, the reference cannot be a new entity.
  55. return !isset($this->id) && isset($this->target) && $this->target->getValue()->isNew();
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function getTarget() {
  61. if (!isset($this->target) && isset($this->id)) {
  62. // If we have a valid reference, return the entity's TypedData adapter.
  63. $entity = \Drupal::entityTypeManager()
  64. ->getStorage($this->getTargetDefinition()->getEntityTypeId())
  65. ->load($this->id);
  66. $this->target = isset($entity) ? $entity->getTypedData() : NULL;
  67. }
  68. return $this->target;
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function getTargetIdentifier() {
  74. if (isset($this->id)) {
  75. return $this->id;
  76. }
  77. elseif ($entity = $this->getValue()) {
  78. return $entity->id();
  79. }
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function setValue($value, $notify = TRUE) {
  85. unset($this->target);
  86. unset($this->id);
  87. // Both the entity ID and the entity object may be passed as value. The
  88. // reference may also be unset by passing NULL as value.
  89. if (!isset($value)) {
  90. $this->target = NULL;
  91. }
  92. elseif ($value instanceof EntityInterface) {
  93. $this->target = $value->getTypedData();
  94. }
  95. elseif (!is_scalar($value) || $this->getTargetDefinition()->getEntityTypeId() === NULL) {
  96. throw new \InvalidArgumentException('Value is not a valid entity.');
  97. }
  98. else {
  99. $this->id = $value;
  100. }
  101. // Notify the parent of any changes.
  102. if ($notify && isset($this->parent)) {
  103. $this->parent->onChange($this->name);
  104. }
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function getString() {
  110. if ($entity = $this->getValue()) {
  111. return $entity->label();
  112. }
  113. return '';
  114. }
  115. }