You are here

public function AudioPlayerHTML5::viewElements in Media entity audio 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/AudioPlayerHTML5.php \Drupal\media_entity_audio\Plugin\Field\FieldFormatter\AudioPlayerHTML5::viewElements()

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

src/Plugin/Field/FieldFormatter/AudioPlayerHTML5.php, line 77

Class

AudioPlayerHTML5
Plugin implementation of the 'Audio Player (HTML5)' formatter.

Namespace

Drupal\media_entity_audio\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = array();
  $provide_download_link = $this
    ->getSetting('provide_download_link');
  $audio_attributes = $this
    ->getSetting('audio_attributes');
  foreach ($this
    ->getEntitiesToView($items, $langcode) as $delta => $file) {
    $item = $file->_referringItem;
    $elements[$delta] = array(
      '#theme' => 'media_audio_file_formatter',
      '#file' => $file,
      '#description' => $item->description,
      '#value' => $provide_download_link,
      '#extravalue' => $audio_attributes,
      '#cache' => array(
        'tags' => $file
          ->getCacheTags(),
      ),
    );

    // Pass field item attributes to the theme function.
    if (isset($item->_attributes)) {
      $elements[$delta] += array(
        '#attributes' => array(),
      );
      $elements[$delta]['#attributes'] += $item->_attributes;

      // Unset field item attributes since they have been included in the
      // formatter output and should not be rendered in the field template.
      unset($item->_attributes);
    }
  }
  return $elements;
}