You are here

protected function LanguagefieldFlagFormatter::viewValue in Flags 8

Generate the output appropriate for one field item.

Parameters

\Drupal\Core\Field\FieldItemInterface $item: One field item.

Return value

array The textual output generated as a render array.

Overrides LanguageFormatter::viewValue

File

flags_languagefield/src/Plugin/Field/FieldFormatter/LanguagefieldFlagFormatter.php, line 78

Class

LanguagefieldFlagFormatter
Plugin implementation of the 'languagefield_flag' formatter.

Namespace

Drupal\flags_languagefield\Plugin\Field\FieldFormatter

Code

protected function viewValue(FieldItemInterface $item) {
  $languagefield_view = parent::viewValue($item);

  // Base formatter uses #plain_text output but we're adding markup,
  // se we need to alter that.
  $code = $languagefield_view['#context']['value'];
  $text = $languagefield_view['#text'];
  $format = $this
    ->getSetting('flag_display');
  $attributes = new Attribute(array(
    'class' => array(
      $format,
    ),
  ));
  $view = array();
  if ('flag-instead' != $format) {
    $view['language'] = array(
      '#plain_text' => $text,
    );
  }
  $view['flag'] = array(
    '#theme' => 'flags',
    '#code' => strtolower($code),
    '#attributes' => $attributes,
    '#source' => 'language',
  );
  $view['#prefix'] = '<div class="field__flags__item">';
  $view['#suffix'] = '</div>';
  return $view;
}