Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
Alex Pott
committed
* Contains \Drupal\Core\Field\FieldItemBase.
Dries Buytaert
committed
*/
Alex Pott
committed
namespace Drupal\Core\Field;
Dries Buytaert
committed
catch
committed
use Drupal\Core\Entity\EntityInterface;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinitionInterface;
Alex Pott
committed
use Drupal\Core\TypedData\Plugin\DataType\Map;
Dries Buytaert
committed
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\user;
/**
* An entity field item.
*
* Entity field items making use of this base class have to implement
* the static method propertyDefinitions().
Dries Buytaert
committed
*
Alex Pott
committed
* @see \Drupal\Core\Field\FieldItemInterface
Jennifer Hodgdon
committed
* @ingroup field_types
Dries Buytaert
committed
*/
catch
committed
abstract class FieldItemBase extends Map implements FieldItemInterface {
Dries Buytaert
committed
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array();
}
/**
* {@inheritdoc}
*/
public static function defaultInstanceSettings() {
return array();
}
/**
* {@inheritdoc}
*/
public static function mainPropertyName() {
return 'value';
}
Dries Buytaert
committed
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
parent::__construct($definition, $name, $parent);
catch
committed
// Initialize computed properties by default, such that they get cloned
// with the whole item.
foreach ($this->definition->getPropertyDefinitions() as $name => $definition) {
if ($definition->isComputed()) {
$this->properties[$name] = \Drupal::typedDataManager()->getPropertyInstance($this, $name);
Dries Buytaert
committed
}
}
}
/**
* {@inheritdoc}
*/
public function getEntity() {
return $this->getParent()->getEntity();
}
/**
* {@inheritdoc}
*/
public function getLangcode() {
return $this->parent->getLangcode();
}
/**
* {@inheritdoc}
*/
public function getFieldDefinition() {
return $this->getParent()->getFieldDefinition();
}
Alex Pott
committed
/**
* Returns the array of field settings.
*
* @return array
* The array of settings.
*/
protected function getSettings() {
Alex Pott
committed
return $this->getFieldDefinition()->getSettings();
Alex Pott
committed
}
/**
* Returns the value of a field setting.
*
* @param string $setting_name
* The setting name.
*
* @return mixed
* The setting value.
*/
protected function getSetting($setting_name) {
Alex Pott
committed
return $this->getFieldDefinition()->getSetting($setting_name);
Alex Pott
committed
}
Dries Buytaert
committed
/**
* Overrides \Drupal\Core\TypedData\TypedData::setValue().
Dries Buytaert
committed
*
* @param array|null $values
Dries Buytaert
committed
* An array of property values.
*/
catch
committed
public function setValue($values, $notify = TRUE) {
Dries Buytaert
committed
// Treat the values as property value of the first property, if no array is
// given.
catch
committed
if (isset($values) && !is_array($values)) {
$keys = array_keys($this->definition->getPropertyDefinitions());
Dries Buytaert
committed
$values = array($keys[0] => $values);
}
catch
committed
$this->values = $values;
// Update any existing property objects.
Dries Buytaert
committed
foreach ($this->properties as $name => $property) {
catch
committed
$value = NULL;
if (isset($values[$name])) {
catch
committed
$value = $values[$name];
}
catch
committed
$property->setValue($value, FALSE);
unset($this->values[$name]);
Dries Buytaert
committed
}
// Notify the parent of any changes.
if ($notify && isset($this->parent)) {
$this->parent->onChange($this->name);
}
Dries Buytaert
committed
}
/**
Alex Pott
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
catch
committed
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];
Dries Buytaert
committed
}
catch
committed
elseif (isset($this->properties[$name])) {
return $this->properties[$name]->getValue();
Dries Buytaert
committed
}
}
/**
Alex Pott
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
catch
committed
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);
Alex Pott
committed
unset($this->values[$property_name]);
Angie Byron
committed
}
catch
committed
// Allow setting plain values for not-defined properties also.
else {
$this->values[$property_name] = $value;
Angie Byron
committed
}
// Directly notify ourselves.
if ($notify) {
$this->onChange($property_name);
}
Dries Buytaert
committed
}
/**
Alex Pott
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
public function __set($name, $value) {
catch
committed
// 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)) {
Dries Buytaert
committed
$value = $value->getValue();
}
catch
committed
$this->set($name, $value);
Dries Buytaert
committed
}
/**
Alex Pott
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
public function __isset($name) {
catch
committed
return isset($this->values[$name]) || (isset($this->properties[$name]) && $this->properties[$name]->getValue() !== NULL);
Dries Buytaert
committed
}
/**
Alex Pott
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
public function __unset($name) {
catch
committed
$this->set($name, NULL);
Dries Buytaert
committed
unset($this->values[$name]);
Dries Buytaert
committed
}
/**
catch
committed
* Overrides \Drupal\Core\TypedData\Map::onChange().
Dries Buytaert
committed
*/
catch
committed
public function onChange($property_name) {
// Notify the parent of changes.
if (isset($this->parent)) {
$this->parent->onChange($this->name);
Dries Buytaert
committed
}
catch
committed
// Remove the plain value, such that any further __get() calls go via the
// updated property object.
if (isset($this->properties[$property_name])) {
unset($this->values[$property_name]);
}
Dries Buytaert
committed
}
Alex Pott
committed
/**
* {@inheritdoc}
*/
public function view($display_options = array()) {
$view_builder = \Drupal::entityManager()->getViewBuilder($this->getEntity()->getEntityTypeId());
return $view_builder->viewFieldItem($this, $display_options);
}
/**
* {@inheritdoc}
*/
public function preSave() { }
/**
* {@inheritdoc}
*/
public function insert() { }
/**
* {@inheritdoc}
*/
public function update() { }
/**
* {@inheritdoc}
*/
public function delete() { }
/**
* {@inheritdoc}
*/
public function deleteRevision() { }
Alex Pott
committed
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function settingsForm(array &$form, FormStateInterface $form_state, $has_data) {
Alex Pott
committed
return array();
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function instanceSettingsForm(array $form, FormStateInterface $form_state) {
Alex Pott
committed
return array();
}
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**
* {@inheritdoc}
*/
public static function settingsToConfigData(array $settings) {
return $settings;
}
/**
* {@inheritdoc}
*/
public static function settingsFromConfigData(array $settings) {
return $settings;
}
/**
* {@inheritdoc}
*/
public static function instanceSettingsToConfigData(array $settings) {
return $settings;
}
/**
* {@inheritdoc}
*/
public static function instanceSettingsFromConfigData(array $settings) {
return $settings;
}