EntityReferenceItem.php 9.51 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem.
6 7
 */

8
namespace Drupal\Core\Field\Plugin\Field\FieldType;
9

10
use Drupal\Core\Entity\EntityInterface;
11
use Drupal\Core\Entity\EntityTypeInterface;
12
use Drupal\Core\Entity\TypedData\EntityDataDefinition;
13
use Drupal\Core\Field\FieldDefinitionInterface;
14
use Drupal\Core\Field\FieldItemBase;
15
use Drupal\Core\Field\FieldStorageDefinitionInterface;
16
use Drupal\Core\TypedData\DataDefinition;
17
use Drupal\Core\TypedData\DataReferenceDefinition;
18 19

/**
20
 * Defines the 'entity_reference' entity field type.
21
 *
22 23 24 25 26
 * Supported settings (below the definition's 'settings' key) are:
 * - target_type: The entity type to reference. Required.
 * - target_bundle: (optional): If set, restricts the entity bundles which may
 *   may be referenced. May be set to an single bundle, or to an array of
 *   allowed bundles.
27
 *
28 29 30
 * @FieldType(
 *   id = "entity_reference",
 *   label = @Translation("Entity reference"),
31
 *   description = @Translation("An entity field containing an entity reference."),
32
 *   no_ui = TRUE,
33
 *   list_class = "\Drupal\Core\Field\EntityReferenceFieldItemList",
34
 *   constraints = {"ValidReference" = {}}
35
 * )
36 37 38
 */
class EntityReferenceItem extends FieldItemBase {

39 40 41 42 43 44 45
  /**
   * Marker value to identify a newly created entity.
   *
   * @var int
   */
  protected static $NEW_ENTITY_MARKER = -1;

46 47 48
  /**
   * {@inheritdoc}
   */
49
  public static function defaultStorageSettings() {
50 51 52
    return array(
      'target_type' => \Drupal::moduleHandler()->moduleExists('node') ? 'node' : 'user',
      'target_bundle' => NULL,
53
    ) + parent::defaultStorageSettings();
54 55 56 57 58
  }

  /**
   * {@inheritdoc}
   */
59
  public static function defaultFieldSettings() {
60 61
    return array(
      'handler' => 'default',
62
    ) + parent::defaultFieldSettings();
63 64
  }

65
  /**
66
   * {@inheritdoc}
67
   */
68
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
69 70
    $settings = $field_definition->getSettings();
    $target_type_info = \Drupal::entityManager()->getDefinition($settings['target_type']);
71

72
    if ($target_type_info->isSubclassOf('\Drupal\Core\Entity\FieldableEntityInterface')) {
73 74 75
      // @todo: Lookup the entity type's ID data type and use it here.
      // https://drupal.org/node/2107249
      $target_id_definition = DataDefinition::create('integer')
76
        ->setLabel(t('@label ID', array($target_type_info->getLabel())))
77
        ->setSetting('unsigned', TRUE);
78
    }
79 80
    else {
      $target_id_definition = DataDefinition::create('string')
81
        ->setLabel(t('@label ID', array($target_type_info->getLabel())));
82
    }
83
    $target_id_definition->setRequired(TRUE);
84
    $properties['target_id'] = $target_id_definition;
85

86
    $properties['entity'] = DataReferenceDefinition::create('entity')
87
      ->setLabel($target_type_info->getLabel())
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
      ->setDescription(t('The referenced entity'))
      // The entity object is computed out of the entity ID.
      ->setComputed(TRUE)
      ->setReadOnly(FALSE)
      ->setTargetDefinition(EntityDataDefinition::create($settings['target_type']));

    if (isset($settings['target_bundle'])) {
      $properties['entity']->getTargetDefinition()->addConstraint('Bundle', $settings['target_bundle']);
    }

    return $properties;
  }

  /**
   * {@inheritdoc}
   */
  public static function mainPropertyName() {
    return 'target_id';
106 107
  }

108 109 110
  /**
   * {@inheritdoc}
   */
111
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
112 113 114
    $target_type = $field_definition->getSetting('target_type');
    $target_type_info = \Drupal::entityManager()->getDefinition($target_type);

115
    if ($target_type_info->isSubclassOf('\Drupal\Core\Entity\FieldableEntityInterface')) {
116 117 118 119 120 121 122 123 124 125 126 127 128
      $columns = array(
        'target_id' => array(
          'description' => 'The ID of the target entity.',
          'type' => 'int',
          'unsigned' => TRUE,
        ),
      );
    }
    else {
      $columns = array(
        'target_id' => array(
          'description' => 'The ID of the target entity.',
          'type' => 'varchar',
129 130 131
          // If the target entities act as bundles for another entity type,
          // their IDs should not exceed the maximum length for bundles.
          'length' => $target_type_info->getBundleOf() ? EntityTypeInterface::BUNDLE_MAX_LENGTH : 255,
132 133 134 135 136 137 138 139 140 141 142 143 144 145
        ),
      );
    }

    $schema = array(
      'columns' => $columns,
      'indexes' => array(
        'target_id' => array('target_id'),
      ),
    );

    return $schema;
  }

146
  /**
147
   * {@inheritdoc}
148 149 150
   */
  public function setValue($values, $notify = TRUE) {
    if (isset($values) && !is_array($values)) {
151 152 153
      // If either a scalar or an object was passed as the value for the item,
      // assign it to the 'entity' property since that works for both cases.
      $this->set('entity', $values, $notify);
154
    }
155
    else {
156 157 158
      parent::setValue($values, FALSE);
      // Support setting the field item with only one property, but make sure
      // values stay in sync if only property is passed.
159
      if (isset($values['target_id']) && !isset($values['entity'])) {
160 161 162 163 164 165
        $this->onChange('target_id', FALSE);
      }
      elseif (!isset($values['target_id']) && isset($values['entity'])) {
        $this->onChange('entity', FALSE);
      }
      elseif (isset($values['target_id']) && isset($values['entity'])) {
166 167 168 169 170 171
        // If both properties are passed, verify the passed values match. The
        // only exception we allow is when we have a new entity: in this case
        // its actual id and target_id will be different, due to the new entity
        // marker.
        $entity_id = $this->get('entity')->getTargetIdentifier();
        if ($entity_id != $values['target_id'] && ($values['target_id'] != static::$NEW_ENTITY_MARKER || !$this->entity->isNew())) {
172 173 174 175 176 177
          throw new \InvalidArgumentException('The target id and entity passed to the entity reference item do not match.');
        }
      }
      // Notify the parent if necessary.
      if ($notify && $this->parent) {
        $this->parent->onChange($this->getName());
178
      }
179
    }
180

181
  }
182

183 184 185
  /**
   * {@inheritdoc}
   */
186 187
  public function getValue() {
    $values = parent::getValue();
188 189 190

    // If there is an unsaved entity, return it as part of the field item values
    // to ensure idempotency of getValue() / setValue().
191
    if ($this->hasNewEntity()) {
192 193 194 195 196
      $values['entity'] = $this->entity;
    }
    return $values;
  }

197 198 199
  /**
   * {@inheritdoc}
   */
200
  public function onChange($property_name, $notify = TRUE) {
201
    // Make sure that the target ID and the target property stay in sync.
202 203 204 205
    if ($property_name == 'entity') {
      $property = $this->get('entity');
      $target_id = $property->isTargetNew() ? static::$NEW_ENTITY_MARKER : $property->getTargetIdentifier();
      $this->writePropertyValue('target_id', $target_id);
206
    }
207 208
    elseif ($property_name == 'target_id' && $this->target_id != static::$NEW_ENTITY_MARKER) {
      $this->writePropertyValue('entity', $this->target_id);
209
    }
210
    parent::onChange($property_name, $notify);
211
  }
212

213 214 215 216 217
  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
    // Avoid loading the entity by first checking the 'target_id'.
218
    if ($this->target_id !== NULL) {
219 220
      return FALSE;
    }
221
    if ($this->entity && $this->entity instanceof EntityInterface) {
222 223 224 225 226 227 228 229 230
      return FALSE;
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function preSave() {
231
    if ($this->hasNewEntity()) {
232 233 234 235 236 237
      // Save the entity if it has not already been saved by some other code.
      if ($this->entity->isNew()) {
        $this->entity->save();
      }
      // Make sure the parent knows we are updating this property so it can
      // react properly.
238 239 240 241
      $this->target_id = $this->entity->id();
    }
  }

242 243 244 245 246 247 248 249 250 251 252 253
  /**
   * {@inheritdoc}
   */
  public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
    $manager = \Drupal::service('plugin.manager.entity_reference.selection');
    if ($referenceable = $manager->getSelectionHandler($field_definition)->getReferenceableEntities()) {
      $group = array_rand($referenceable);
      $values['target_id'] = array_rand($referenceable[$group]);
      return $values;
    }
  }

254 255 256 257 258 259 260 261 262 263
  /**
   * Determines whether the item holds an unsaved entity.
   *
   * This is notably used for "autocreate" widgets, and more generally to
   * support referencing freshly created entities (they will get saved
   * automatically as the hosting entity gets saved).
   *
   * @return bool
   *   TRUE if the item holds an unsaved entity.
   */
264
  public function hasNewEntity() {
265
    return $this->target_id === static::$NEW_ENTITY_MARKER;
266
  }
267

268 269 270 271 272 273 274 275 276 277 278 279 280
  /**
   * {@inheritdoc}
   */
  public static function calculateDependencies(FieldDefinitionInterface $field_definition) {
    $dependencies = [];
    if (is_array($field_definition->default_value) && count($field_definition->default_value)) {
      $target_entity_type = \Drupal::entityManager()->getDefinition($field_definition->getFieldStorageDefinition()->getSetting('target_type'));
      foreach ($field_definition->default_value as $default_value) {
        if (is_array($default_value) && isset($default_value['target_uuid'])) {
          $entity = \Drupal::entityManager()->loadEntityByUuid($target_entity_type->id(), $default_value['target_uuid']);
          // If the entity does not exist do not create the dependency.
          // @see \Drupal\Core\Field\EntityReferenceFieldItemList::processDefaultValue()
          if ($entity) {
281
            $dependencies[$target_entity_type->getConfigDependencyKey()][] = $entity->getConfigDependencyName();
282 283 284 285 286 287 288
          }
        }
      }
    }
    return $dependencies;
  }

289
}