Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity_reference_revisions / src / EntityReferenceRevisionsFieldItemList.php
1 <?php
2
3 namespace Drupal\entity_reference_revisions;
4
5 use Drupal\Core\Entity\FieldableEntityInterface;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Field\FieldItemListTranslationChangesInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Field\FieldDefinitionInterface;
10 use Drupal\Core\Field\EntityReferenceFieldItemList;
11 use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
12
13 /**
14  * Defines a item list class for entity reference fields.
15  */
16 class EntityReferenceRevisionsFieldItemList extends EntityReferenceFieldItemList implements EntityReferenceFieldItemListInterface {
17
18   /**
19    * {@inheritdoc}
20    */
21   public function referencedEntities() {
22     if (empty($this->list)) {
23       return array();
24     }
25
26     // Collect the IDs of existing entities to load, and directly grab the
27     // "autocreate" entities that are already populated in $item->entity.
28     $target_entities = $ids = array();
29     foreach ($this->list as $delta => $item) {
30       if ($item->hasNewEntity()) {
31         $target_entities[$delta] = $item->entity;
32       }
33       elseif ($item->target_revision_id !== NULL) {
34         $ids[$delta] = $item->target_revision_id;
35       }
36     }
37
38     // Load and add the existing entities.
39     if ($ids) {
40       $target_type = $this->getFieldDefinition()->getSetting('target_type');
41       foreach ($ids as $delta => $target_id) {
42         $entity = \Drupal::entityTypeManager()->getStorage($target_type)->loadRevision($target_id);
43         if ($entity) {
44           $target_entities[$delta] = $entity;
45         }
46       }
47       // Ensure the returned array is ordered by deltas.
48       ksort($target_entities);
49     }
50
51     return $target_entities;
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public static function processDefaultValue($default_value, FieldableEntityInterface $entity, FieldDefinitionInterface $definition) {
58     $default_value = parent::processDefaultValue($default_value, $entity, $definition);
59
60     if ($default_value) {
61       // Convert UUIDs to numeric IDs.
62       $uuids = array();
63       foreach ($default_value as $delta => $properties) {
64         if (isset($properties['target_uuid'])) {
65           $uuids[$delta] = $properties['target_uuid'];
66         }
67       }
68       if ($uuids) {
69         $target_type = $definition->getSetting('target_type');
70         $entity_ids = \Drupal::entityQuery($target_type)
71           ->condition('uuid', $uuids, 'IN')
72           ->execute();
73         $entities = \Drupal::entityTypeManager()
74           ->getStorage($target_type)
75           ->loadMultiple($entity_ids);
76
77         $entity_uuids = array();
78         foreach ($entities as $id => $entity) {
79           $entity_uuids[$entity->uuid()] = $id;
80         }
81         foreach ($uuids as $delta => $uuid) {
82           if (isset($entity_uuids[$uuid])) {
83             $default_value[$delta]['target_id'] = $entity_uuids[$uuid];
84             unset($default_value[$delta]['target_uuid']);
85           }
86           else {
87             unset($default_value[$delta]);
88           }
89         }
90       }
91
92       // Ensure we return consecutive deltas, in case we removed unknown UUIDs.
93       $default_value = array_values($default_value);
94     }
95
96     return $default_value;
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   public function defaultValuesFormSubmit(array $element, array &$form, FormStateInterface $form_state) {
103     $default_value = parent::defaultValuesFormSubmit($element, $form, $form_state);
104
105     // Convert numeric IDs to UUIDs to ensure config deployability.
106     $ids = array();
107     foreach ($default_value as $delta => $properties) {
108       $ids[] = $properties['target_revision_id'];
109     }
110
111     $entities = array();
112     foreach($ids as $id) {
113       $entities[$id] = \Drupal::entityTypeManager()
114         ->getStorage($this->getSetting('target_type'))
115         ->loadRevision($id);
116     }
117
118     foreach ($default_value as $delta => $properties) {
119       $default_value[$delta] = array(
120         'target_uuid' => $entities[$properties['target_revision_id']]->uuid(),
121         'target_revision_id' => $properties['target_revision_id'],
122       );
123     }
124     return $default_value;
125   }
126
127   /**
128    * {@inheritdoc}
129    */
130   public function hasAffectingChanges(FieldItemListInterface $original_items, $langcode) {
131     // If there are fewer items, then it is a change.
132     if (count($this) < count($original_items)) {
133       return TRUE;
134     }
135
136     foreach ($this as $delta => $item) {
137       // If this is a different entity, then it is an affecting change.
138       if (!$original_items->offsetExists($delta) || $item->target_id != $original_items[$delta]->target_id) {
139         return TRUE;
140       }
141       // If it is the same entity, only consider it as having affecting changes
142       // if the target entity itself has changes.
143       if ($item->entity && $item->entity->hasTranslation($langcode) && $item->entity->getTranslation($langcode)->hasTranslationChanges()) {
144         return TRUE;
145       }
146     }
147
148     return FALSE;
149   }
150
151 }