Skip to content
FieldItemBase.php 6.64 KiB
Newer Older
 * Contains \Drupal\Core\Field\FieldItemBase.
use Drupal\Core\TypedData\DataDefinitionInterface;
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().
 * @see \Drupal\Core\Field\FieldItemInterface
abstract class FieldItemBase extends Map implements FieldItemInterface {
  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return array();
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultInstanceSettings() {
    return array();
  }

  /**
   * {@inheritdoc}
   */
  public static function mainPropertyName() {
    return 'value';
  }

  public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
    parent::__construct($definition, $name, $parent);
    // 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);
  /**
   * {@inheritdoc}
   */
  public function getEntity() {
    return $this->getParent()->getEntity();
  }

  /**
   * {@inheritdoc}
   */
  public function getLangcode() {
    return $this->parent->getLangcode();
  }

  /**
   * {@inheritdoc}
   */
  public function getFieldDefinition() {
    return $this->getParent()->getFieldDefinition();
  }

  /**
   * Returns the array of field settings.
   *
   * @return array
   *   The array of settings.
   */
  protected function getSettings() {
    return $this->getFieldDefinition()->getSettings();
  }

  /**
   * Returns the value of a field setting.
   *
   * @param string $setting_name
   *   The setting name.
   *
   * @return mixed
   *   The setting value.
   */
  protected function getSetting($setting_name) {
    return $this->getFieldDefinition()->getSetting($setting_name);
   * Overrides \Drupal\Core\TypedData\TypedData::setValue().
  public function setValue($values, $notify = TRUE) {
    // Treat the values as property value of the first property, if no array is
      $keys = array_keys($this->definition->getPropertyDefinitions());
    $this->values = $values;
    // Update any existing property objects.
    foreach ($this->properties as $name => $property) {
      $property->setValue($value, FALSE);
      unset($this->values[$name]);
    // Notify the parent of any changes.
    if ($notify && isset($this->parent)) {
      $this->parent->onChange($this->name);
    }
  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];
    elseif (isset($this->properties[$name])) {
      return $this->properties[$name]->getValue();
  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);
    // Allow setting plain values for not-defined properties also.
    else {
      $this->values[$property_name] = $value;
    // Directly notify ourselves.
    if ($notify) {
      $this->onChange($property_name);
    }
   */
  public function __set($name, $value) {
    // 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)) {
    return isset($this->values[$name]) || (isset($this->properties[$name]) && $this->properties[$name]->getValue() !== NULL);
   * Overrides \Drupal\Core\TypedData\Map::onChange().
  public function onChange($property_name) {
    // Notify the parent of changes.
    if (isset($this->parent)) {
      $this->parent->onChange($this->name);
    // 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]);
    }
  /**
   * {@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() { }

  public function settingsForm(array &$form, FormStateInterface $form_state, $has_data) {
  public function instanceSettingsForm(array $form, FormStateInterface $form_state) {
  /**
   * {@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;
  }