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

/**
 * @file
5
 * Contains \Drupal\Core\Entity\Field\Type\EntityReferenceItem.
6
7
8
9
10
11
12
 */

namespace Drupal\Core\Entity\Field\Type;

use Drupal\Core\Entity\Field\FieldItemBase;

/**
13
 * Defines the 'entity_reference' entity field item.
14
 *
15
16
 * Required settings (below the definition's 'settings' key) are:
 *  - target_type: The entity type to reference.
17
18
19
20
 */
class EntityReferenceItem extends FieldItemBase {

  /**
21
   * Definitions of the contained properties.
22
   *
23
   * @see EntityReferenceItem::getPropertyDefinitions()
24
25
26
27
28
29
   *
   * @var array
   */
  static $propertyDefinitions;

  /**
30
   * Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions().
31
32
33
   */
  public function getPropertyDefinitions() {
    // Definitions vary by entity type, so key them by entity type.
34
    $target_type = $this->definition['settings']['target_type'];
35

36
37
    if (!isset(self::$propertyDefinitions[$target_type])) {
      static::$propertyDefinitions[$target_type]['target_id'] = array(
38
39
40
        // @todo: Lookup the entity type's ID data type and use it here.
        'type' => 'integer',
        'label' => t('Entity ID'),
41
42
43
        'constraints' => array(
          'Range' => array('min' => 0),
        ),
44
      );
45
      static::$propertyDefinitions[$target_type]['entity'] = array(
46
47
        'type' => 'entity',
        'constraints' => array(
48
          'EntityType' => $target_type,
49
50
51
        ),
        'label' => t('Entity'),
        'description' => t('The referenced entity'),
52
        // The entity object is computed out of the entity ID.
53
54
        'computed' => TRUE,
        'read-only' => FALSE,
55
        'settings' => array('id source' => 'target_id'),
56
57
      );
    }
58
    return static::$propertyDefinitions[$target_type];
59
60
61
  }

  /**
62
   * Overrides \Drupal\Core\Entity\Field\FieldItemBase::setValue().
63
64
65
   */
  public function setValue($values) {
    // Treat the values as property value of the entity field, if no array
66
    // is given. That way we support setting the field by entity ID or object.
67
68
69
70
71
72
    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.
73
74
    if (isset($values['target_id'])) {
      $this->properties['target_id']->setValue($values['target_id']);
75
    }
76
77
78
    elseif (isset($values['entity'])) {
      $this->properties['entity']->setValue($values['entity']);
    }
79
    else {
80
      $this->properties['entity']->setValue(NULL);
81
    }
82
    unset($values['entity'], $values['target_id']);
83
    if ($values) {
84
      throw new \InvalidArgumentException('Property ' . key($values) . ' is unknown.');
85
86
    }
  }
87

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  /**
   * Overrides \Drupal\Core\Entity\Field\FieldItemBase::__set().
   */
  public function __set($name, $value) {
    parent::__set($name, $value);
  }

  /**
   * Overrides \Drupal\Core\Entity\Field\FieldItemBase::__get().
   */
  public function __get($name) {
    $name = ($name == 'value') ? 'target_id' : $name;
    return parent::__get($name);
  }

103
104
105
106
107
108
109
  /**
   * Overrides \Drupal\Core\Entity\Field\FieldItemBase::get().
   */
  public function get($property_name) {
    $property_name = ($property_name == 'value') ? 'target_id' : $property_name;
    return parent::get($property_name);
  }
110
111
112
113
114
115
116
117
118

  /**
   * Implements \Drupal\Core\Entity\Field\FieldItemInterface::__isset().
   */
  public function __isset($property_name) {
    $property_name = ($property_name == 'value') ? 'target_id' : $property_name;
    return parent::__isset($property_name);
  }

119
}