You are here

public function OpignoTftFormatter::viewElements in Opigno module 8

Same name and namespace in other branches
  1. 3.x ActivityTypes/opigno_video/src/Plugin/Field/FieldFormatter/OpignoTftFormatter.php \Drupal\opigno_video\Plugin\Field\FieldFormatter\OpignoTftFormatter::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

ActivityTypes/opigno_video/src/Plugin/Field/FieldFormatter/OpignoTftFormatter.php, line 68

Class

OpignoTftFormatter
Plugin implementation of the 'opigno_tft_formatter' formatter.

Namespace

Drupal\opigno_video\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements['#attached']['library'][] = 'core/drupal.dialog.ajax';
  foreach ($items as $delta => $item) {
    $elements[$delta] = [
      $this
        ->viewValue($item),
    ];
  }
  if (empty($items->list)) {
    $media = $items
      ->getEntity();
    if (!empty($media) && $media
      ->hasField('opigno_moxtra_recording_link') && !empty($link = $media
      ->get('opigno_moxtra_recording_link')
      ->getValue())) {

      // Moxtra recording file.
      // Get the filefield icon.
      $icon_class = file_icon_class('video/mp4');
      $elements[][] = [
        '#type' => 'link',
        '#title' => $media
          ->getName(),
        '#url' => Url::fromUri("internal:/tft/download/file/{$media->id()}"),
        '#attributes' => [
          'class' => "file {$icon_class}",
          'target' => '_blank',
        ],
        '#prefix' => '<div>',
        '#suffix' => '</div>',
      ];
    }
  }
  return $elements;
}