You are here

abstract class ExtraFieldPlusDisplayFormattedBase in Extra Field Settings Provider 8.2

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

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

Hierarchy

Expanded class hierarchy of ExtraFieldPlusDisplayFormattedBase

1 file declares its use of ExtraFieldPlusDisplayFormattedBase
ExampleNodeLabelFormatted.php in modules/extra_field_plus_example/src/Plugin/ExtraField/Display/ExampleNodeLabelFormatted.php

File

src/Plugin/ExtraFieldPlusDisplayFormattedBase.php, line 13

Namespace

Drupal\extra_field_plus\Plugin
View source
abstract class ExtraFieldPlusDisplayFormattedBase extends ExtraFieldPlusDisplayBase implements ExtraFieldPlusDisplayInterface, ExtraFieldDisplayFormattedInterface {

  /**
   * Flag to indicate that the extra field has no content.
   *
   * @var bool
   */
  protected $isEmpty = FALSE;

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

  /**
   * {@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(),
        '#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;

        // 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;
  }

  /**
   * {@inheritdoc}
   */
  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
ExtraFieldPlusDisplayBase::defaultFormValues protected function Provides field settings form default values. 2
ExtraFieldPlusDisplayBase::getDefaultFormValues public function Returns field settings form default values. Overrides ExtraFieldPlusDisplayInterface::getDefaultFormValues
ExtraFieldPlusDisplayBase::getSetting public function Returns field setting. Overrides ExtraFieldPlusDisplayInterface::getSetting
ExtraFieldPlusDisplayBase::getSettings public function Returns field settings. Overrides ExtraFieldPlusDisplayInterface::getSettings
ExtraFieldPlusDisplayBase::getSettingsForm public function Returns field settings form. Overrides ExtraFieldPlusDisplayInterface::getSettingsForm
ExtraFieldPlusDisplayBase::settingsForm protected function Provides field settings form. 2
ExtraFieldPlusDisplayFormattedBase::$isEmpty protected property Flag to indicate that the extra field has no content.
ExtraFieldPlusDisplayFormattedBase::$langcode protected property The langcode of the field values.
ExtraFieldPlusDisplayFormattedBase::getFieldName public function
ExtraFieldPlusDisplayFormattedBase::getFieldType public function
ExtraFieldPlusDisplayFormattedBase::getLabel public function 1
ExtraFieldPlusDisplayFormattedBase::getLabelDisplay public function 1
ExtraFieldPlusDisplayFormattedBase::getLangcode public function
ExtraFieldPlusDisplayFormattedBase::isEmpty public function
ExtraFieldPlusDisplayFormattedBase::isTranslatable public function
ExtraFieldPlusDisplayFormattedBase::setLangcode public function
ExtraFieldPlusDisplayFormattedBase::view public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.