EntityReference.php 3.25 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
<?php

/**
 * @file
 * Contains \Drupal\Core\Entity\Plugin\DataType\EntityReference.
 */

namespace Drupal\Core\Entity\Plugin\DataType;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\TypedData\Annotation\DataType;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\TypedData\DataReferenceBase;

/**
 * Defines an 'entity_reference' data type.
 *
 * This serves as 'entity' property of entity reference field items and gets
 * its value set from the parent, i.e. LanguageItem.
 *
 * The plain value of this reference is the entity object, i.e. an instance of
 * \Drupal\Core\Entity\EntityInterface. For setting the value the entity object
 * or the entity ID may be passed, whereas passing the ID is only supported if
 * an 'entity type' constraint is specified.
 *
 * Some supported constraints (below the definition's 'constraints' key) are:
 *  - EntityType: The entity type. Required.
 *  - Bundle: (optional) The bundle or an array of possible bundles.
 *
 * @DataType(
 *   id = "entity_reference",
 *   label = @Translation("Entity reference")
 * )
 */
class EntityReference extends DataReferenceBase {

  /**
   * The entity ID.
   *
   * @var integer|string
   */
  protected $id;

  /**
   * {@inheritdoc}
   */
  public function getTargetDefinition() {
    $definition = array(
      'type' => 'entity',
    );
    if (isset($this->definition['constraints']['EntityType'])) {
      $definition['type'] .= ':' . $this->definition['constraints']['EntityType'];
    }
    if (isset($this->definition['constraints']['Bundle']) && is_string($this->definition['constraints']['Bundle'])) {
      $definition['type'] .= ':' . $this->definition['constraints']['Bundle'];
    }
    return $definition;
  }

  /**
   * {@inheritdoc}
   */
  public function getTarget() {
    if (!isset($this->target) && isset($this->id)) {
      // If we have a valid reference, return the entity object which is typed
      // data itself.
      $this->target = entity_load($this->definition['constraints']['EntityType'], $this->id);
    }
    return $this->target;
  }

  /**
   * {@inheritdoc}
   */
  public function getTargetIdentifier() {
    if (isset($this->id)) {
      return $this->id;
    }
    elseif ($entity = $this->getValue()) {
      return $entity->id();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getValue() {
    // Entities are already typed data, so just return that.
    return $this->getTarget();
  }

  /**
   * {@inheritdoc}
   */
  public function setValue($value, $notify = TRUE) {
    unset($this->target);
    unset($this->id);

    // Both the entity ID and the entity object may be passed as value. The
    // reference may also be unset by passing NULL as value.
    if (!isset($value) || $value instanceof EntityInterface) {
      $this->target = $value;
    }
    elseif (!is_scalar($value) || empty($this->definition['constraints']['EntityType'])) {
      throw new \InvalidArgumentException('Value is not a valid entity.');
    }
    else {
      $this->id = $value;
    }
    // Notify the parent of any changes.
    if ($notify && isset($this->parent)) {
      $this->parent->onChange($this->name);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getString() {
    if ($entity = $this->getValue()) {
      return $entity->label();
    }
    return '';
  }
}