You are here

abstract class ExtraFieldDisplayFormattedBase in Extra Field 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/ExtraFieldDisplayFormattedBase.php \Drupal\extra_field\Plugin\ExtraFieldDisplayFormattedBase

Base class for Extra field Display plugins with field wrapper output.

Hierarchy

Expanded class hierarchy of ExtraFieldDisplayFormattedBase

6 files declare their use of ExtraFieldDisplayFormattedBase
EmptyFormattedFieldTest.php in tests/extra_field_test/src/Plugin/ExtraField/Display/EmptyFormattedFieldTest.php
ExampleFormattedField.php in modules/extra_field_example/src/Plugin/ExtraField/Display/ExampleFormattedField.php
ExampleMultilingualField.php in modules/extra_field_example/src/Plugin/ExtraField/Display/ExampleMultilingualField.php
MultipleItemsFieldTest.php in tests/extra_field_test/src/Plugin/ExtraField/Display/MultipleItemsFieldTest.php
MultipleItemsFieldWithCacheDependencyTest.php in tests/extra_field_test/src/Plugin/ExtraField/Display/MultipleItemsFieldWithCacheDependencyTest.php

... See full list

File

src/Plugin/ExtraFieldDisplayFormattedBase.php, line 12

Namespace

Drupal\extra_field\Plugin
View source
abstract class ExtraFieldDisplayFormattedBase extends ExtraFieldDisplayBase implements ExtraFieldDisplayFormattedInterface {

  /**
   * Flag to indicate that the extra field has no content.
   *
   * Set this flag when the render elements returned by ::viewElements only
   * contains non-visible render data such as #cache or #attached but does not
   * contain actual renderable data such as #markup, #theme or #item.
   *
   * When this flag is set, the render elements will not be wrapped in a field
   * wrapper.
   *
   * @var bool
   */
  protected $isEmpty = FALSE;

  /**
   * The langcode of the field values.
   *
   * @var string
   */
  protected $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * {@inheritdoc}
   */
  public function view(ContentEntityInterface $entity) {
    $elements = $this
      ->viewElements($entity);
    if (!empty($elements) && !$this
      ->isEmpty()) {

      // Construct a render array for the extra field elements.
      // @see \Drupal\Core\Field\FormatterBase::view
      $build = [
        '#theme' => 'field',
        '#title' => $this
          ->getLabel(),
        '#label_display' => $this
          ->getLabelDisplay(),
        // Prevent quickedit from editing this field by using a special view
        // mode.
        // @see quickedit_preprocess_field()
        '#view_mode' => '_custom',
        '#language' => $this
          ->getLangcode(),
        '#field_name' => $this
          ->getFieldName(),
        '#field_type' => $this
          ->getFieldType(),
        '#field_translatable' => $this
          ->isTranslatable(),
        '#entity_type' => $entity
          ->getEntityTypeId(),
        '#bundle' => $entity
          ->bundle(),
        '#object' => $entity,
        '#formatter' => $this
          ->getPluginId(),
      ];
      if ($children = Element::children($elements, TRUE)) {
        $build['#is_multiple'] = TRUE;
        $build['#cache'] = !empty($elements['#cache']) ? $elements['#cache'] : [];

        // Without #children the field will not show up.
        $build['#children'] = '';
        foreach ($children as $key) {

          // Only keys in "#items" property are required in
          // template_preprocess_field().
          $build['#items'][$key] = new \stdClass();
          $build[$key] = $elements[$key];
        }
      }
      else {
        $build['#is_multiple'] = FALSE;

        // Only keys in "#items" property are required in
        // template_preprocess_field().
        $build['#items'][] = new \stdClass();
        $build[] = $elements;
      }
    }
    else {
      $build = $elements;
    }
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function getLabel() {
    return '';
  }

  /**
   * {@inheritdoc}
   */
  public function getLabelDisplay() {
    return 'hidden';
  }

  /**
   * {@inheritdoc}
   */
  public function getFieldType() {
    return 'extra_field';
  }

  /**
   * {@inheritdoc}
   */
  public function getFieldName() {
    return 'extra_field_' . $this->pluginId;
  }

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

  /**
   * Set the language code.
   *
   * TODO Add this to the interface in the 3.0.0 release.
   */
  public function setLangcode($langcode) {
    $this->langcode = $langcode;
  }

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

  /**
   * {@inheritdoc}
   */
  public function isTranslatable() {
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ExtraFieldDisplayBase::$entity protected property The field's parent entity.
ExtraFieldDisplayBase::$entityViewDisplay protected property The entity view display.
ExtraFieldDisplayBase::$viewMode protected property The view mode the entity is rendered in.
ExtraFieldDisplayBase::getEntity public function Returns the field's parent entity. Overrides ExtraFieldDisplayInterface::getEntity
ExtraFieldDisplayBase::getEntityViewDisplay public function Returns the entity view display object of the field's host entity. Overrides ExtraFieldDisplayInterface::getEntityViewDisplay
ExtraFieldDisplayBase::getViewMode public function Returns the entity view mode object of the field's host entity. Overrides ExtraFieldDisplayInterface::getViewMode
ExtraFieldDisplayBase::setEntity public function Stores the field's parent entity. Overrides ExtraFieldDisplayInterface::setEntity
ExtraFieldDisplayBase::setEntityViewDisplay public function Stores the entity view display. Overrides ExtraFieldDisplayInterface::setEntityViewDisplay
ExtraFieldDisplayBase::setViewMode public function Stores the entity view mode. Overrides ExtraFieldDisplayInterface::setViewMode
ExtraFieldDisplayFormattedBase::$isEmpty protected property Flag to indicate that the extra field has no content.
ExtraFieldDisplayFormattedBase::$langcode protected property The langcode of the field values.
ExtraFieldDisplayFormattedBase::$languageManager protected property The language manager.
ExtraFieldDisplayFormattedBase::getFieldName public function The machine name of the field. Overrides ExtraFieldDisplayFormattedInterface::getFieldName 1
ExtraFieldDisplayFormattedBase::getFieldType public function The type of field. Overrides ExtraFieldDisplayFormattedInterface::getFieldType 1
ExtraFieldDisplayFormattedBase::getLabel public function The label of the field. Overrides ExtraFieldDisplayFormattedInterface::getLabel 5
ExtraFieldDisplayFormattedBase::getLabelDisplay public function How to display the field label will be displayed. Overrides ExtraFieldDisplayFormattedInterface::getLabelDisplay 5
ExtraFieldDisplayFormattedBase::getLangcode public function Gets the langcode of the field values. Overrides ExtraFieldDisplayFormattedInterface::getLangcode 1
ExtraFieldDisplayFormattedBase::isEmpty public function Check if the extra field has data. Overrides ExtraFieldDisplayFormattedInterface::isEmpty
ExtraFieldDisplayFormattedBase::isTranslatable public function The field is translatable. Overrides ExtraFieldDisplayFormattedInterface::isTranslatable 1
ExtraFieldDisplayFormattedBase::setLangcode public function Set the language code.
ExtraFieldDisplayFormattedBase::view public function Builds a renderable array for the field. Overrides ExtraFieldDisplayInterface::view
ExtraFieldDisplayFormattedInterface::viewElements public function Returns the renderable array of the field item(s). 6
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92