EntityReferenceFieldItemList.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Drupal\Core\Field;
  3. use Drupal\Core\Entity\FieldableEntityInterface;
  4. use Drupal\Core\Form\FormStateInterface;
  5. /**
  6. * Defines a item list class for entity reference fields.
  7. */
  8. class EntityReferenceFieldItemList extends FieldItemList implements EntityReferenceFieldItemListInterface {
  9. /**
  10. * {@inheritdoc}
  11. */
  12. public function getConstraints() {
  13. $constraints = parent::getConstraints();
  14. $constraint_manager = $this->getTypedDataManager()->getValidationConstraintManager();
  15. $constraints[] = $constraint_manager->create('ValidReference', []);
  16. return $constraints;
  17. }
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function referencedEntities() {
  22. if ($this->isEmpty()) {
  23. return [];
  24. }
  25. // Collect the IDs of existing entities to load, and directly grab the
  26. // "autocreate" entities that are already populated in $item->entity.
  27. $target_entities = $ids = [];
  28. foreach ($this->list as $delta => $item) {
  29. if ($item->target_id !== NULL) {
  30. $ids[$delta] = $item->target_id;
  31. }
  32. elseif ($item->hasNewEntity()) {
  33. $target_entities[$delta] = $item->entity;
  34. }
  35. }
  36. // Load and add the existing entities.
  37. if ($ids) {
  38. $target_type = $this->getFieldDefinition()->getSetting('target_type');
  39. $entities = \Drupal::entityManager()->getStorage($target_type)->loadMultiple($ids);
  40. foreach ($ids as $delta => $target_id) {
  41. if (isset($entities[$target_id])) {
  42. $target_entities[$delta] = $entities[$target_id];
  43. }
  44. }
  45. // Ensure the returned array is ordered by deltas.
  46. ksort($target_entities);
  47. }
  48. return $target_entities;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public static function processDefaultValue($default_value, FieldableEntityInterface $entity, FieldDefinitionInterface $definition) {
  54. $default_value = parent::processDefaultValue($default_value, $entity, $definition);
  55. if ($default_value) {
  56. // Convert UUIDs to numeric IDs.
  57. $uuids = [];
  58. foreach ($default_value as $delta => $properties) {
  59. if (isset($properties['target_uuid'])) {
  60. $uuids[$delta] = $properties['target_uuid'];
  61. }
  62. }
  63. if ($uuids) {
  64. $target_type = $definition->getSetting('target_type');
  65. $entity_ids = \Drupal::entityQuery($target_type)
  66. ->condition('uuid', $uuids, 'IN')
  67. ->execute();
  68. $entities = \Drupal::entityManager()
  69. ->getStorage($target_type)
  70. ->loadMultiple($entity_ids);
  71. $entity_uuids = [];
  72. foreach ($entities as $id => $entity) {
  73. $entity_uuids[$entity->uuid()] = $id;
  74. }
  75. foreach ($uuids as $delta => $uuid) {
  76. if (isset($entity_uuids[$uuid])) {
  77. $default_value[$delta]['target_id'] = $entity_uuids[$uuid];
  78. unset($default_value[$delta]['target_uuid']);
  79. }
  80. else {
  81. unset($default_value[$delta]);
  82. }
  83. }
  84. }
  85. // Ensure we return consecutive deltas, in case we removed unknown UUIDs.
  86. $default_value = array_values($default_value);
  87. }
  88. return $default_value;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function defaultValuesFormSubmit(array $element, array &$form, FormStateInterface $form_state) {
  94. $default_value = parent::defaultValuesFormSubmit($element, $form, $form_state);
  95. // Convert numeric IDs to UUIDs to ensure config deployability.
  96. $ids = [];
  97. foreach ($default_value as $delta => $properties) {
  98. if (isset($properties['entity']) && $properties['entity']->isNew()) {
  99. // This may be a newly created term.
  100. $properties['entity']->save();
  101. $default_value[$delta]['target_id'] = $properties['entity']->id();
  102. unset($default_value[$delta]['entity']);
  103. }
  104. $ids[] = $default_value[$delta]['target_id'];
  105. }
  106. $entities = \Drupal::entityManager()
  107. ->getStorage($this->getSetting('target_type'))
  108. ->loadMultiple($ids);
  109. foreach ($default_value as $delta => $properties) {
  110. unset($default_value[$delta]['target_id']);
  111. $default_value[$delta]['target_uuid'] = $entities[$properties['target_id']]->uuid();
  112. }
  113. return $default_value;
  114. }
  115. }