You are here

public static function DynamicEntityReferenceFieldItemList::processDefaultValue in Dynamic Entity Reference 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldType/DynamicEntityReferenceFieldItemList.php \Drupal\dynamic_entity_reference\Plugin\Field\FieldType\DynamicEntityReferenceFieldItemList::processDefaultValue()

Processes the default value before being applied.

Defined or configured default values of a field might need some processing in order to be a valid runtime value for the field type; e.g., a date field could process the defined value of 'NOW' to a valid date.

Parameters

array $default_value: The unprocessed default value defined for the field, as a numerically indexed array of items, each item being an array of property/value pairs.

\Drupal\Core\Entity\FieldableEntityInterface $entity: The entity for which the default value is generated.

\Drupal\Core\Field\FieldDefinitionInterface $definition: The definition of the field.

Return value

array The return default value for the field.

Overrides EntityReferenceFieldItemList::processDefaultValue

File

src/Plugin/Field/FieldType/DynamicEntityReferenceFieldItemList.php, line 79

Class

DynamicEntityReferenceFieldItemList
Defines a item list class for dynamic entity reference fields.

Namespace

Drupal\dynamic_entity_reference\Plugin\Field\FieldType

Code

public static function processDefaultValue($default_value, FieldableEntityInterface $entity, FieldDefinitionInterface $definition) {
  $manager = \Drupal::entityTypeManager();

  // We want to bypass the EntityReferenceItem::processDefaultValue() so
  // we duplicate FieldItemList::processDefaultValue() here which just returns
  // $default_values.
  if ($default_value) {

    // Convert UUIDs to numeric IDs.
    $all_uuids = [];
    foreach ($default_value as $delta => $properties) {
      if (isset($properties['target_uuid'])) {
        $target_type = $properties['target_type'];
        $all_uuids[$target_type][$delta] = $properties['target_uuid'];
      }
    }
    $entity_uuids = [];
    foreach ($all_uuids as $target_type => $uuids) {
      if ($uuids) {
        $entities = $manager
          ->getStorage($target_type)
          ->loadByProperties([
          'uuid' => $uuids,
        ]);
        $entity_uuids[$target_type] = [];
        foreach ($entities as $id => $entity) {
          $entity_uuids[$target_type][$entity
            ->uuid()] = $id;
        }
        foreach ($uuids as $delta => $uuid) {
          if (isset($entity_uuids[$target_type]) && isset($entity_uuids[$target_type][$uuid])) {
            $default_value[$delta]['target_id'] = $entity_uuids[$target_type][$uuid];
            unset($default_value[$delta]['target_uuid']);
          }
          else {
            unset($default_value[$delta]);
          }
        }
      }
    }

    // Ensure we return consecutive deltas, in case we removed unknown UUIDs.
    $default_value = array_values($default_value);
  }
  return $default_value;
}