Skip to content
Snippets Groups Projects
Select Git revision
  • e112c456a75dc2f5a730053b7f37184ef6f8e77b
  • 11.x default protected
  • 11.2.x protected
  • 10.6.x protected
  • 10.5.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

EntityReferenceItem.php

Blame
  • webchick's avatar
    Issue #1798382 by Berdir: Fixed Random Test failures in Entity translation tests.
    Angie Byron authored
    e112c456
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    EntityReferenceItem.php 2.36 KiB
    <?php
    
    /**
     * @file
     * Definition of Drupal\Core\Entity\Field\Type\EntityReferenceItem.
     */
    
    namespace Drupal\Core\Entity\Field\Type;
    
    use Drupal\Core\Entity\Field\FieldItemBase;
    use InvalidArgumentException;
    
    /**
     * Defines the 'entityreference_field' entity field item.
     *
     * Required settings (below the definition's 'settings' key) are:
     *  - entity type: The entity type to reference.
     */
    class EntityReferenceItem extends FieldItemBase {
    
      /**
       * Field definitions of the contained properties.
       *
       * @see self::getPropertyDefinitions()
       *
       * @var array
       */
      static $propertyDefinitions;
    
      /**
       * Implements ComplexDataInterface::getPropertyDefinitions().
       */
      public function getPropertyDefinitions() {
        // Definitions vary by entity type, so key them by entity type.
        $entity_type = $this->definition['settings']['entity type'];
    
        if (!isset(self::$propertyDefinitions[$entity_type])) {
          self::$propertyDefinitions[$entity_type]['value'] = array(
            // @todo: Lookup the entity type's ID data type and use it here.
            'type' => 'integer',
            'label' => t('Entity ID'),
          );
          self::$propertyDefinitions[$entity_type]['entity'] = array(
            'type' => 'entity',
            'constraints' => array(
              'entity type' => $entity_type,
            ),
            'label' => t('Entity'),
            'description' => t('The referenced entity'),
            // The entity object is computed out of the entity id.
            'computed' => TRUE,
            'read-only' => FALSE,
            'settings' => array('id source' => 'value'),
          );
        }
        return self::$propertyDefinitions[$entity_type];
      }
    
      /**
       * Overrides FieldItemBase::setValue().
       */
      public function setValue($values) {
        // Treat the values as property value of the entity field, if no array
        // is given.
        if (!is_array($values)) {
          $values = array('entity' => $values);
        }
    
        // Entity is computed out of the ID, so we only need to update the ID. Only
        // set the entity field if no ID is given.
        if (isset($values['value'])) {
          $this->properties['value']->setValue($values['value']);
        }
        else {
          $this->properties['entity']->setValue(isset($values['entity']) ? $values['entity'] : NULL);
        }
        unset($values['entity'], $values['value']);
        if ($values) {
          throw new InvalidArgumentException('Property ' . key($values) . ' is unknown.');
        }
      }
    }