You are here

public function SocialLinkFontAwesomeFormatter::viewElements in Social Link Field 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 SocialLinkBaseFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/SocialLinkFontAwesomeFormatter.php, line 63

Class

SocialLinkFontAwesomeFormatter
Plugin implementation of the 'social_link_field' formatter.

Namespace

Drupal\social_link_field\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $element = parent::viewElements($items, $langcode);
  $configs = $this->configFactory
    ->get('social_link_field.settings');
  $icon_type = $this
    ->getSetting('icon_type');
  $element['#appearance'] = [
    'orientation' => $this
      ->getSetting('orientation'),
  ];
  foreach ($items as $delta => $item) {
    $values = $item
      ->getValue();
    $social = $values['social'];
    $link = $values['link'];
    if ($icon_type == 'square' && isset($this->platforms[$social]['iconSquare'])) {
      $icon = $this->platforms[$social]['iconSquare'];
    }
    else {
      $icon = $this->platforms[$social]['icon'];
    }
    $element['#links'][$delta] = [
      'text' => [
        '#type' => 'html_tag',
        '#tag' => 'i',
        '#attributes' => [
          'class' => [
            'fa',
            $icon,
          ],
        ],
      ],
      'url' => $this->platforms[$social]['urlPrefix'] . $link,
      'title' => $this->platforms[$social]['name'],
    ];
  }

  // Attach formatter library.
  $element['#attached']['library'][] = 'social_link_field/social_link_field.font_awesome_formatter';

  // Attach FontAwesome external library.
  if ($configs
    ->get('attached_fa')) {
    $element['#attached']['library'][] = 'social_link_field/fontawesome.component';
  }
  return $element;
}