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