You are here

public function IconFieldDefault::viewElements in Icon API 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 FormatterInterface::viewElements

File

modules/icon_field/src/Plugin/Field/FieldFormatter/IconFieldDefault.php, line 30
Contains \Drupal\icon_field\Plugin\Field\FieldFormatter\IconFieldDefault.

Class

IconFieldDefault
Plugin implementation of the 'icon_field_default' formatter.

Namespace

Drupal\icon_field\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = array();
  $entity = $items
    ->getEntity();
  $uri = $entity->uri;
  foreach ($items as $delta => $item) {
    if ($this
      ->getSetting('link_to_content')) {
      $options = array(
        'html' => TRUE,
        'attributes' => array(),
      );
      if (isset($uri['options']) and !empty($uri['options'])) {
        $options = array_merge($uri['options'], $options);
      }
    }
    else {
      $elements[$delta] = array(
        '#type' => 'icon',
        '#icon' => $item['icon'],
        '#tag' => 'p',
      );
    }
  }
  return $elements;
}