Skip to content
Snippets Groups Projects
Select Git revision
  • 72c63698c815f2ac68b3c01c0177fe0bf08e6f82
  • 11.x default protected
  • 11.2.x protected
  • 10.5.x protected
  • 10.6.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 8.9.x protected
  • 9.0.x protected
  • 8.8.x protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
  • 11.1.7 protected
  • 10.4.6 protected
  • 11.1.6 protected
  • 10.3.14 protected
  • 10.4.5 protected
  • 11.0.13 protected
41 results

ConfigurableEntityReferenceFieldItemList.php

Blame
  • Nathaniel Catchpole's avatar
    Issue #2090541 by plopesc, yched: Avoid storing numeric target_id()'s for...
    catch authored
    Issue #2090541 by plopesc, yched: Avoid storing numeric target_id()'s for default values in field instance CMI files.
    72c63698
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    ConfigurableEntityReferenceFieldItemList.php 2.99 KiB
    <?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;
      }
    
    }