FieldItemBase.php 5.85 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Field\FieldItemBase.
6 7
 */

8
namespace Drupal\Core\Field;
9

10
use Drupal\Core\Entity\EntityInterface;
11
use Drupal\Core\TypedData\DataDefinitionInterface;
12
use Drupal\Core\TypedData\Plugin\DataType\Map;
13 14 15 16 17 18 19
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\user;

/**
 * An entity field item.
 *
 * Entity field items making use of this base class have to implement
20
 * the static method propertyDefinitions().
21
 *
22
 * @see \Drupal\Core\Field\FieldItemInterface
23
 */
24
abstract class FieldItemBase extends Map implements FieldItemInterface {
25

26 27 28 29 30 31 32
  /**
   * {@inheritdoc}
   */
  public static function mainPropertyName() {
    return 'value';
  }

33
  /**
34
   * {@inheritdoc}
35
   */
36
  public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
37
    parent::__construct($definition, $name, $parent);
38 39
    // Initialize computed properties by default, such that they get cloned
    // with the whole item.
40
    foreach ($this->definition->getPropertyDefinitions() as $name => $definition) {
41
      if ($definition->isComputed()) {
42
        $this->properties[$name] = \Drupal::typedDataManager()->getPropertyInstance($this, $name);
43 44 45 46
      }
    }
  }

47 48 49 50 51 52 53
  /**
   * {@inheritdoc}
   */
  public function getEntity() {
    return $this->getParent()->getEntity();
  }

54 55 56 57 58 59 60
  /**
   * {@inheritdoc}
   */
  public function getLangcode() {
    return $this->parent->getLangcode();
  }

61 62 63 64 65 66 67
  /**
   * {@inheritdoc}
   */
  public function getFieldDefinition() {
    return $this->getParent()->getFieldDefinition();
  }

68 69 70 71 72 73
  /**
   * Returns the array of field settings.
   *
   * @return array
   *   The array of settings.
   */
74
  protected function getSettings() {
75
    return $this->getFieldDefinition()->getSettings();
76 77 78 79 80 81 82 83 84 85 86
  }

  /**
   * Returns the value of a field setting.
   *
   * @param string $setting_name
   *   The setting name.
   *
   * @return mixed
   *   The setting value.
   */
87
  protected function getSetting($setting_name) {
88
    return $this->getFieldDefinition()->getSetting($setting_name);
89 90
  }

91
  /**
92
   * Overrides \Drupal\Core\TypedData\TypedData::setValue().
93
   *
94
   * @param array|null $values
95 96
   *   An array of property values.
   */
97
  public function setValue($values, $notify = TRUE) {
98
    // Treat the values as property value of the first property, if no array is
99
    // given.
100
    if (isset($values) && !is_array($values)) {
101
      $keys = array_keys($this->definition->getPropertyDefinitions());
102 103
      $values = array($keys[0] => $values);
    }
104 105
    $this->values = $values;
    // Update any existing property objects.
106
    foreach ($this->properties as $name => $property) {
107
      $value = NULL;
108
      if (isset($values[$name])) {
109
        $value = $values[$name];
110
      }
111 112
      $property->setValue($value, FALSE);
      unset($this->values[$name]);
113
    }
114 115 116 117
    // Notify the parent of any changes.
    if ($notify && isset($this->parent)) {
      $this->parent->onChange($this->name);
    }
118 119 120
  }

  /**
121
   * {@inheritdoc}
122
   */
123 124 125 126 127
  public function __get($name) {
    // There is either a property object or a plain value - possibly for a
    // not-defined property. If we have a plain value, directly return it.
    if (isset($this->values[$name])) {
      return $this->values[$name];
128
    }
129 130
    elseif (isset($this->properties[$name])) {
      return $this->properties[$name]->getValue();
131 132 133 134
    }
  }

  /**
135
   * {@inheritdoc}
136
   */
137 138 139 140 141
  public function set($property_name, $value, $notify = TRUE) {
    // For defined properties there is either a property object or a plain
    // value that needs to be updated.
    if (isset($this->properties[$property_name])) {
      $this->properties[$property_name]->setValue($value, FALSE);
142
      unset($this->values[$property_name]);
143
    }
144 145 146
    // Allow setting plain values for not-defined properties also.
    else {
      $this->values[$property_name] = $value;
147
    }
148 149 150 151
    // Directly notify ourselves.
    if ($notify) {
      $this->onChange($property_name);
    }
152 153 154
  }

  /**
155
   * {@inheritdoc}
156 157
   */
  public function __set($name, $value) {
158 159 160
    // Support setting values via property objects, but take care in as the
    // value of the 'entity' property is typed data also.
    if ($value instanceof TypedDataInterface && !($value instanceof EntityInterface)) {
161 162
      $value = $value->getValue();
    }
163
    $this->set($name, $value);
164 165 166
  }

  /**
167
   * {@inheritdoc}
168 169
   */
  public function __isset($name) {
170
    return isset($this->values[$name]) || (isset($this->properties[$name]) && $this->properties[$name]->getValue() !== NULL);
171 172 173
  }

  /**
174
   * {@inheritdoc}
175 176
   */
  public function __unset($name) {
177
    $this->set($name, NULL);
178
    unset($this->values[$name]);
179 180 181
  }

  /**
182
   * Overrides \Drupal\Core\TypedData\Map::onChange().
183
   */
184 185 186 187
  public function onChange($property_name) {
    // Notify the parent of changes.
    if (isset($this->parent)) {
      $this->parent->onChange($this->name);
188
    }
189 190
    // Remove the plain value, such that any further __get() calls go via the
    // updated property object.
191 192 193
    if (isset($this->properties[$property_name])) {
      unset($this->values[$property_name]);
    }
194
  }
195

196 197 198 199 200 201 202 203
  /**
   * {@inheritdoc}
   */
  public function view($display_options = array()) {
    $view_builder = \Drupal::entityManager()->getViewBuilder($this->getEntity()->getEntityTypeId());
    return $view_builder->viewFieldItem($this, $display_options);
  }

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  /**
   * {@inheritdoc}
   */
  public function preSave() { }

  /**
   * {@inheritdoc}
   */
  public function insert() { }

  /**
   * {@inheritdoc}
   */
  public function update() { }

  /**
   * {@inheritdoc}
   */
  public function delete() { }

  /**
   * {@inheritdoc}
   */
  public function deleteRevision() { }

229 230 231 232 233 234 235 236 237 238 239 240 241 242
  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, array &$form_state, $has_data) {
    return array();
  }

  /**
   * {@inheritdoc}
   */
  public function instanceSettingsForm(array $form, array &$form_state) {
    return array();
  }

243
}