You are here

protected function OpignoTftFormatter::viewValue in Opigno module 3.x

Same name and namespace in other branches
  1. 8 ActivityTypes/opigno_video/src/Plugin/Field/FieldFormatter/OpignoTftFormatter.php \Drupal\opigno_video\Plugin\Field\FieldFormatter\OpignoTftFormatter::viewValue()

Generate the output appropriate for one field item.

Parameters

\Drupal\Core\Field\FieldItemInterface $item: One field item.

Return value

string The textual output generated.

1 call to OpignoTftFormatter::viewValue()
OpignoTftFormatter::viewElements in ActivityTypes/opigno_video/src/Plugin/Field/FieldFormatter/OpignoTftFormatter.php
Builds a renderable array for a field value.

File

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

Class

OpignoTftFormatter
Plugin implementation of the 'opigno_tft_formatter' formatter.

Namespace

Drupal\opigno_video\Plugin\Field\FieldFormatter

Code

protected function viewValue(FieldItemInterface $item) {
  $fid = $item
    ->getValue();
  $file = File::load($fid['target_id']);
  $ext = explode('/', $file
    ->getMimeType());
  $entity = $item
    ->getEntity();
  $output[] = [
    '#type' => 'link',
    '#title' => $entity
      ->label(),
    '#url' => Url::fromUri("internal:/tft/download/file/{$entity->id()}"),
    '#attributes' => [
      'target' => '_blank',
    ],
    '#prefix' => '<div>',
    '#suffix' => '</div>',
  ];
  switch ($ext[0]) {
    case 'video':
      $source_attributes = new Attribute();
      $source_attributes
        ->setAttribute('src', file_url_transform_relative(file_create_url($file
        ->getFileUri())))
        ->setAttribute('type', $file
        ->getMimeType());
      $source_file = [
        'file' => $file,
        'source_attributes' => $source_attributes,
      ];
      $video_attributes = new Attribute();
      $video_attributes
        ->setAttribute('controls', 'controls')
        ->setAttribute('width', $this
        ->getSetting('width'))
        ->setAttribute('height', $this
        ->getSetting('height'))
        ->setAttribute('controlsList', 'nodownload');
      array_unshift($output, [
        '#theme' => 'file_video',
        '#attributes' => $video_attributes,
        '#files' => [
          $source_file,
        ],
      ]);
      break;
    case 'image':
      $image = [
        '#theme' => 'image_style',
        '#style_name' => 'medium',
        '#uri' => $file
          ->getFileUri(),
        '#width' => $this
          ->getSetting('width'),
        '#height' => $this
          ->getSetting('height'),
      ];
      array_unshift($output, [
        '#type' => 'link',
        '#title' => $image,
        '#url' => URL::fromRoute('opigno_video.image_popup_render', [
          'fid' => $fid['target_id'],
        ]),
        '#options' => [
          'attributes' => [
            'class' => [
              'use-ajax',
              'ops-link',
            ],
            'data-dialog-type' => 'modal',
            'data-dialog-options' => Json::encode([
              'width' => 500,
            ]),
          ],
        ],
      ]);
      break;
  }
  return $output;
}