class Field

Same name in this branch

An entity field, i.e. a list of field items.

An entity field is a list of field items, which contain only primitive properties or entity references. Note that even single-valued entity fields are represented as list of items, however for easy access to the contained item the entity field delegates __get() and __set() calls directly to the first item.

Hierarchy

Expanded class hierarchy of Field

See also

\Drupal\Core\Entity\Field\FieldInterface

4 string references to 'Field'
DisplayOverview::form in drupal/core/modules/field_ui/lib/Drupal/field_ui/DisplayOverview.php
Overrides Drupal\field_ui\OverviewBase::form().
field_views_field_default_views_data in drupal/core/modules/field/field.views.inc
Default views data implementation for a field.
theme_views_ui_style_plugin_table in drupal/core/modules/views/views_ui/theme/theme.inc
Theme the form for the table style plugin
ViewExecutable::viewsHandlerTypes in drupal/core/modules/views/lib/Drupal/views/ViewExecutable.php
Provide a list of views handler types used in a view, with some information about them.

File

drupal/core/lib/Drupal/Core/Entity/Field/Type/Field.php, line 29
Definition of Drupal\Core\Entity\Field\Type\Field.

Namespace

Drupal\Core\Entity\Field\Type
View source
class Field extends TypedData implements IteratorAggregate, FieldInterface {

  /**
   * The entity field name.
   *
   * @var string
   */
  protected $name;

  /**
   * The parent entity.
   *
   * @var \Drupal\Core\Entity\EntityInterface
   */
  protected $parent;

  /**
   * Numerically indexed array of field items, implementing the
   * FieldItemInterface.
   *
   * @var array
   */
  protected $list = array();

  /**
   * Implements TypedDataInterface::getValue().
   */
  public function getValue() {
    $values = array();
    foreach ($this->list as $delta => $item) {
      $values[$delta] = !$item
        ->isEmpty() ? $item
        ->getValue() : NULL;
    }
    return $values;
  }

  /**
   * Implements TypedDataInterface::setValue().
   *
   * @param array $values
   *   An array of values of the field items.
   */
  public function setValue($values) {
    if (isset($values) && $values !== array()) {

      // Support passing in only the value of the first item.
      if (!is_array($values) || !is_numeric(current(array_keys($values)))) {
        $values = array(
          0 => $values,
        );
      }
      if (!is_array($values)) {
        throw new InvalidArgumentException("An entity field requires a numerically indexed array of items as value.");
      }

      // Clear the values of properties for which no value has been passed.
      foreach (array_diff_key($this->list, $values) as $delta => $item) {
        unset($this->list[$delta]);
      }

      // Set the values.
      foreach ($values as $delta => $value) {
        if (!is_numeric($delta)) {
          throw new InvalidArgumentException('Unable to set a value with a non-numeric delta in a list.');
        }
        elseif (!isset($this->list[$delta])) {
          $this->list[$delta] = $this
            ->createItem($value);
        }
        else {
          $this->list[$delta]
            ->setValue($value);
        }
      }
    }
    else {
      $this->list = array();
    }
  }

  /**
   * Returns a string representation of the field.
   *
   * @return string
   */
  public function getString() {
    $strings = array();
    foreach ($this
      ->list() as $item) {
      $strings[] = $item
        ->getString();
    }
    return implode(', ', array_filter($strings));
  }

  /**
   * Implements TypedDataInterface::validate().
   */
  public function validate() {

    // @todo implement
  }

  /**
   * Implements ArrayAccess::offsetExists().
   */
  public function offsetExists($offset) {
    return array_key_exists($offset, $this->list);
  }

  /**
   * Implements ArrayAccess::offsetUnset().
   */
  public function offsetUnset($offset) {
    unset($this->list[$offset]);
  }

  /**
   * Implements ArrayAccess::offsetGet().
   */
  public function offsetGet($offset) {
    if (!is_numeric($offset)) {
      throw new InvalidArgumentException('Unable to get a value with a non-numeric delta in a list.');
    }
    elseif (!isset($this->list[$offset])) {
      $this->list[$offset] = $this
        ->createItem();
    }
    return $this->list[$offset];
  }

  /**
   * Helper for creating a list item object.
   *
   * @return \Drupal\Core\TypedData\TypedDataInterface
   */
  protected function createItem($value = NULL) {
    $context = array(
      'parent' => $this,
    );
    return typed_data()
      ->create(array(
      'list' => FALSE,
    ) + $this->definition, $value, $context);
  }

  /**
   * Implements ArrayAccess::offsetSet().
   */
  public function offsetSet($offset, $value) {
    if (!isset($offset)) {

      // The [] operator has been used so point at a new entry.
      $offset = $this->list ? max(array_keys($this->list)) + 1 : 0;
    }
    if (is_numeric($offset)) {

      // Support setting values via typed data objects.
      if ($value instanceof TypedDataInterface) {
        $value = $value
          ->getValue();
      }
      $this
        ->offsetGet($offset)
        ->setValue($value);
    }
    else {
      throw new InvalidArgumentException('Unable to set a value with a non-numeric delta in a list.');
    }
  }

  /**
   * Implements IteratorAggregate::getIterator().
   */
  public function getIterator() {
    return new ArrayIterator($this->list);
  }

  /**
   * Implements Countable::count().
   */
  public function count() {
    return count($this->list);
  }

  /**
   * Implements ContextAwareInterface::getName().
   */
  public function getName() {
    return $this->name;
  }

  /**
   * Implements ContextAwareInterface::setName().
   */
  public function setName($name) {
    $this->name = $name;
  }

  /**
   * Implements ContextAwareInterface::getParent().
   *
   * @return \Drupal\Core\Entity\EntityInterface
   */
  public function getParent() {
    return $this->parent;
  }

  /**
   * Implements ContextAwareInterface::setParent().
   */
  public function setParent($parent) {
    $this->parent = $parent;
  }

  /**
   * Delegate.
   */
  public function getPropertyDefinition($name) {
    return $this
      ->offsetGet(0)
      ->getPropertyDefinition($name);
  }

  /**
   * Delegate.
   */
  public function getPropertyDefinitions() {
    return $this
      ->offsetGet(0)
      ->getPropertyDefinitions();
  }

  /**
   * Delegate.
   */
  public function __get($property_name) {
    return $this
      ->offsetGet(0)
      ->__get($property_name);
  }

  /**
   * Delegate.
   */
  public function get($property_name) {
    return $this
      ->offsetGet(0)
      ->get($property_name);
  }

  /**
   * Delegate.
   */
  public function __set($property_name, $value) {
    $this
      ->offsetGet(0)
      ->__set($property_name, $value);
  }

  /**
   * Delegate.
   */
  public function __isset($property_name) {
    return $this
      ->offsetGet(0)
      ->__isset($property_name);
  }

  /**
   * Delegate.
   */
  public function __unset($property_name) {
    return $this
      ->offsetGet(0)
      ->__unset($property_name);
  }

  /**
   * Implements ListInterface::isEmpty().
   */
  public function isEmpty() {
    foreach ($this->list as $item) {
      if (!$item
        ->isEmpty()) {
        return FALSE;
      }
    }
    return TRUE;
  }

  /**
   * Implements a deep clone.
   */
  public function __clone() {
    foreach ($this->list as $delta => $property) {
      $this->list[$delta] = clone $property;
    }
  }

  /**
   * Implements AccessibleInterface::access().
   */
  public function access($operation = 'view', User $account = NULL) {

    // TODO: Implement access() method. Use item access.
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Field::$list protected property Numerically indexed array of field items, implementing the FieldItemInterface.
Field::$name protected property The entity field name.
Field::$parent protected property The parent entity.
Field::access public function Implements AccessibleInterface::access(). Overrides AccessibleInterface::access
Field::count public function Implements Countable::count().
Field::createItem protected function Helper for creating a list item object.
Field::get public function Delegate. Overrides FieldInterface::get
Field::getIterator public function Implements IteratorAggregate::getIterator().
Field::getName public function Implements ContextAwareInterface::getName(). Overrides ContextAwareInterface::getName
Field::getParent public function Implements ContextAwareInterface::getParent(). Overrides ContextAwareInterface::getParent
Field::getPropertyDefinition public function Delegate. Overrides FieldInterface::getPropertyDefinition
Field::getPropertyDefinitions public function Delegate. Overrides FieldInterface::getPropertyDefinitions
Field::getString public function Returns a string representation of the field. Overrides TypedData::getString
Field::getValue public function Implements TypedDataInterface::getValue(). Overrides TypedData::getValue
Field::isEmpty public function Implements ListInterface::isEmpty(). Overrides ListInterface::isEmpty
Field::offsetExists public function Implements ArrayAccess::offsetExists().
Field::offsetGet public function Implements ArrayAccess::offsetGet().
Field::offsetSet public function Implements ArrayAccess::offsetSet().
Field::offsetUnset public function Implements ArrayAccess::offsetUnset().
Field::setName public function Implements ContextAwareInterface::setName(). Overrides ContextAwareInterface::setName
Field::setParent public function Implements ContextAwareInterface::setParent(). Overrides ContextAwareInterface::setParent
Field::setValue public function Implements TypedDataInterface::setValue(). Overrides TypedData::setValue
Field::validate public function Implements TypedDataInterface::validate(). Overrides TypedDataInterface::validate
Field::__clone public function Implements a deep clone.
Field::__get public function Delegate. Overrides FieldInterface::__get
Field::__isset public function Delegate. Overrides FieldInterface::__isset
Field::__set public function Delegate. Overrides FieldInterface::__set
Field::__unset public function Delegate. Overrides FieldInterface::__unset
TypedData::$definition protected property The data definition.
TypedData::getDefinition public function Implements TypedDataInterface::getDefinition(). Overrides TypedDataInterface::getDefinition
TypedData::getType public function Implements TypedDataInterface::getType(). Overrides TypedDataInterface::getType
TypedData::__construct public function Constructs a TypedData object given its definition. 3