You are here

public function FixedTextLink::viewElements in Fixed text link formatter 8

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 LinkFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/FixedTextLink.php, line 94

Class

FixedTextLink
Plugin implementation of the 'link' formatter.

Namespace

Drupal\fixed_text_link_formatter\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {

  /** @var \Drupal\link\Plugin\Field\FieldType\LinkItem $item */
  foreach ($items as $item) {
    $allowOverride = (bool) $this
      ->getSetting('allow_override');
    $shouldOverride = $allowOverride && $item
      ->get('title')
      ->getString() === '';
    if (!$allowOverride || $shouldOverride) {
      $item
        ->set('title', $this
        ->getLinkText());
    }
  }
  $elements = parent::viewElements($items, $langcode);
  foreach ($elements as &$element) {
    $element['#options']['attributes']['class'][] = $this
      ->getLinkClass();
  }
  return $elements;
}