You are here

public function SocialMediaLinksFieldDefaultFormatter::viewElements in Social Media Links Block and Field 8.2

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

File

modules/social_media_links_field/src/Plugin/Field/FieldFormatter/SocialMediaLinksFieldDefaultFormatter.php, line 30

Class

SocialMediaLinksFieldDefaultFormatter
Plugin implementation of the 'social_media_links_field_default' formatter.

Namespace

Drupal\social_media_links_field\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $platforms = $this
    ->getPlatformsWithValues($items);
  if (count($platforms) < 1) {
    return [];
  }
  $iconset_style = IconsetBase::explodeStyle($items
    ->getSetting('iconset'));
  $iconset = $this
    ->getIconset($iconset_style['iconset']);
  $link_attributes = $this
    ->getSetting('link_attributes');
  foreach ($link_attributes as $key => $value) {
    if ($value === '<none>') {
      unset($link_attributes[$key]);
    }
  }
  foreach ($platforms as $platform_id => $platform) {
    $platforms[$platform_id]['element'] = (array) $iconset['instance']
      ->getIconElement($platform['instance'], $iconset_style['style']);
    $platforms[$platform_id]['attributes'] = new Attribute($link_attributes);
    if (!empty($platform['instance']
      ->getDescription())) {
      $platforms[$platform_id]['attributes']
        ->setAttribute('aria-label', $this
        ->t($platform['instance']
        ->getDescription()));
      $platforms[$platform_id]['attributes']
        ->setAttribute('title', $this
        ->t($platform['instance']
        ->getDescription()));
    }
  }
  $output = [
    '#theme' => 'social_media_links_platforms',
    '#platforms' => $platforms,
    '#appearance' => $this
      ->getSetting('appearance'),
    '#attached' => [
      'library' => [
        'social_media_links/social_media_links.theme',
      ],
    ],
  ];
  if ($iconset['instance']
    ->getPath() === 'library' && (array) ($library = $iconset['instance']
    ->getLibrary())) {
    $output['#attached']['library'] = array_merge_recursive($output['#attached']['library'], $library);
  }
  return $output;
}