You are here

public function TitleStyleTextFieldFormatter::viewElements in Title Field for Manage Display 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/TitleStyleTextFieldFormatter.php \Drupal\title_field_for_manage_display\Plugin\Field\FieldFormatter\TitleStyleTextFieldFormatter::viewElements()

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides BasicStringFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/TitleStyleTextFieldFormatter.php, line 30

Class

TitleStyleTextFieldFormatter
Plugin implementation of the 'title_style_text_field_formatter' formatter.

Namespace

Drupal\title_field_for_manage_display\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode = NULL) {
  $attributes = new Attribute();
  $classes = $this
    ->getSetting('classes');
  if (!empty($classes)) {
    $attributes
      ->addClass($classes);
  }
  $parent = $items
    ->getParent()
    ->getValue();
  $text = $parent
    ->label();
  if ($this
    ->getSetting('linked')) {
    $text = Link::fromTextAndUrl($text, $parent
      ->toUrl())
      ->toString();
  }
  $output[] = [
    '#type' => 'html_tag',
    '#tag' => $this
      ->getSetting('tag'),
    '#attributes' => $attributes
      ->toArray(),
    '#value' => $text,
  ];
  return $output;
}