You are here

trait FieldAPIHandlerTrait in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/FieldAPIHandlerTrait.php \Drupal\views\FieldAPIHandlerTrait

A trait containing helper methods for field definitions.

Hierarchy

5 files declare their use of FieldAPIHandlerTrait
Date.php in core/modules/datetime/src/Plugin/views/filter/Date.php
Contains \Drupal\datetime\Plugin\views\filter\Date.
Field.php in core/modules/views/src/Plugin/views/field/Field.php
Contains \Drupal\views\Plugin\views\field\Field.
ListField.php in core/modules/options/src/Plugin/views/filter/ListField.php
Contains \Drupal\options\Plugin\views\filter\ListField.
NumberListField.php in core/modules/options/src/Plugin/views/argument/NumberListField.php
Contains \Drupal\options\Plugin\views\argument\NumberListField.
StringListField.php in core/modules/options/src/Plugin/views/argument/StringListField.php
Contains \Drupal\options\Plugin\views\argument\StringListField.

File

core/modules/views/src/FieldAPIHandlerTrait.php, line 15
Contains \Drupal\views\FieldAPIHandlerTrait.

Namespace

Drupal\views
View source
trait FieldAPIHandlerTrait {

  /**
   * The field definition.
   *
   * @var \Drupal\Core\Field\FieldDefinitionInterface
   */
  protected $fieldDefinition;

  /**
   * The field storage definition.
   *
   * @var \Drupal\field\FieldStorageConfigInterface
   */
  protected $fieldStorageDefinition;

  /**
   * Gets the field definition.
   *
   * A View works on an entity type across bundles, and thus only has access to
   * field storage definitions. In order to be able to use widgets and
   * formatters, we create a generic field definition out of that storage
   * definition.
   *
   * @see BaseFieldDefinition::createFromFieldStorageDefinition()
   *
   * @return \Drupal\Core\Field\FieldDefinitionInterface
   *   The field definition used by this handler.
   */
  protected function getFieldDefinition() {
    if (!$this->fieldDefinition) {
      $field_storage_config = $this
        ->getFieldStorageDefinition();
      $this->fieldDefinition = BaseFieldDefinition::createFromFieldStorageDefinition($field_storage_config);
    }
    return $this->fieldDefinition;
  }

  /**
   * Gets the field storage configuration.
   *
   * @return \Drupal\field\FieldStorageConfigInterface
   *   The field storage definition used by this handler
   */
  protected function getFieldStorageDefinition() {
    if (!$this->fieldStorageDefinition) {
      $field_storage_definitions = $this
        ->getEntityManager()
        ->getFieldStorageDefinitions($this->definition['entity_type']);
      $this->fieldStorageDefinition = $field_storage_definitions[$this->definition['field_name']];
    }
    return $this->fieldStorageDefinition;
  }

  /**
   * Returns the entity manager.
   *
   * @return \Drupal\Core\Entity\EntityManagerInterface
   *   The entity manager service.
   */
  protected function getEntityManager() {
    if (!isset($this->entityManager)) {
      $this->entityManager = \Drupal::entityManager();
    }
    return $this->entityManager;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldAPIHandlerTrait::$fieldDefinition protected property The field definition.
FieldAPIHandlerTrait::$fieldStorageDefinition protected property The field storage definition.
FieldAPIHandlerTrait::getEntityManager protected function Returns the entity manager. 1
FieldAPIHandlerTrait::getFieldDefinition protected function Gets the field definition.
FieldAPIHandlerTrait::getFieldStorageDefinition protected function Gets the field storage configuration. 1