ConfigurableEntityReferenceFieldItemList.php 2.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
<?php

/**
 * @file
 * Contains \Drupal\entity_reference\Plugin\Field\FieldType\ConfigurableEntityReferenceFieldItemList.
 */

namespace Drupal\entity_reference\Plugin\Field\FieldType;

use Drupal\Core\Field\ConfigFieldItemList;

/**
 * Represents a configurable entity_reference entity field.
 */
class ConfigurableEntityReferenceFieldItemList extends ConfigFieldItemList {

  /**
   * {@inheritdoc}
   */
  protected function getDefaultValue() {
    $default_value = parent::getDefaultValue();

    if ($default_value) {
      // Convert UUIDs to numeric IDs.
      $uuids = array();
      $fixed = array();
      foreach ($default_value as $delta => $properties) {
        if ($properties['target_uuid'] == 'anonymous' || $properties['target_uuid'] == 'administrator') {
          $fixed[$delta] = ($properties['target_uuid'] == 'anonymous') ? '0' : '1';
        }
        else {
          $uuids[$delta] = $properties['target_uuid'];
        }
      }
      if ($uuids) {
        $entities = \Drupal::entityManager()
          ->getStorageController($this->getFieldDefinition()->getFieldSetting('target_type'))
          ->loadByProperties(array('uuid' => $uuids));

        foreach ($entities as $id => $entity) {
          $entity_ids[$entity->uuid()] = $id;
        }
        foreach ($uuids as $delta => $uuid) {
          if ($entity_ids[$uuid]) {
            $default_value[$delta]['target_id'] = $entity_ids[$uuid];
            unset($default_value[$delta]['target_uuid']);
          }
          else {
            unset($default_value[$delta]);
          }
        }
      }

      if ($fixed) {
        foreach ($fixed as $delta => $id) {
          $default_value[$delta]['target_id'] = $id;
          unset($default_value[$delta]['target_uuid']);
        }
      }

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

  /**
   * {@inheritdoc}
   */
  public function defaultValuesFormSubmit(array $element, array &$form, array &$form_state) {
    $default_value = parent::defaultValuesFormSubmit($element, $form, $form_state);

    // Convert numeric IDs to UUIDs to ensure config deployability.
    $ids = array();
    foreach ($default_value as $delta => $properties) {
      $ids[] = $properties['target_id'];
    }
    $entities = \Drupal::entityManager()
      ->getStorageController($this->getFieldDefinition()->getFieldSetting('target_type'))
      ->loadMultiple($ids);

    foreach ($default_value as $delta => $properties) {
      $uuid = $entities[$properties['target_id']]->uuid();
      // @todo Some entities do not have uuid. IE: Anonymous and admin user.
      //   Remove this special case once http://drupal.org/node/2050843
      //   has been fixed.
      if (!$uuid) {
        $uuid = ($properties['target_id'] == '0') ? 'anonymous' : 'administrator';
      }
      unset($default_value[$delta]['target_id']);
      $default_value[$delta]['target_uuid'] = $uuid;
    }
    return $default_value;
  }

}