You are here

public function ProjekktorAudioPlayer::renderPlayer in AudioField 8

Renders the player.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The uploaded item list.

string $langcode: The language code.

array $settings: An array of additional render settings.

Return value

array Returns the rendered array.

Overrides AudioFieldPluginBase::renderPlayer

File

src/Plugin/AudioPlayer/ProjekktorAudioPlayer.php, line 28

Class

ProjekktorAudioPlayer
Implements the Projekktor Audio Player plugin.

Namespace

Drupal\audiofield\Plugin\AudioPlayer

Code

public function renderPlayer(FieldItemListInterface $items, $langcode, array $settings) {

  // Check to make sure we're installed.
  if (!$this
    ->checkInstalled()) {

    // Show the error.
    $this
      ->showInstallError();

    // Simply return the default rendering so the files are still displayed.
    return $this
      ->renderDefaultPlayer($items, $settings);
  }

  // Start building settings to pass to the javascript projekktor builder.
  $player_settings = [
    // Projekktor expects this as a 0 - 1 range.
    'volume' => $settings['audio_player_initial_volume'] / 10,
    'swfpath' => $this
      ->getPluginLibraryPath() . '/swf/Jarisplayer/jarisplayer.swf',
    'files' => [],
    'autoplay' => $settings['audio_player_autoplay'],
  ];

  // Format files for output.
  $template_files = $this
    ->getItemRenderList($items);
  foreach ($template_files as $renderInfo) {

    // Add this file to the render settings.
    $player_settings['files'][] = $renderInfo->id;
  }
  return [
    'audioplayer' => [
      '#theme' => 'audioplayer',
      '#plugin_id' => 'projekktor',
      '#settings' => $settings,
      '#files' => $template_files,
    ],
    'downloads' => $this
      ->createDownloadList($items, $settings),
    '#attached' => [
      'library' => [
        // Attach the projekktor library.
        'audiofield/audiofield.' . $this
          ->getPluginLibraryName(),
      ],
      'drupalSettings' => [
        'audiofieldprojekktor' => [
          $this
            ->getUniqueRenderId() => $player_settings,
        ],
      ],
    ],
  ];
}