public function JPlayerAudioPlayer::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/ JPlayerAudioPlayer.php, line 28
Class
- JPlayerAudioPlayer
- Implements the jPlayer Audio Player plugin.
Namespace
Drupal\audiofield\Plugin\AudioPlayerCode
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);
}
// Create arrays to pass to the twig template.
$template_settings = $settings;
$template_theme = str_replace('audiofield.jplayer.theme_', '', $settings['audio_player_jplayer_theme']);
// JPlayer circle has to render differently - no playlist support, etc.
$player_settings = [];
if ($settings['audio_player_jplayer_theme'] == 'audiofield.jplayer.theme_jplayer_circle') {
// @todo circle player broken for some reason.
// Only require the default library.
$library = 'audiofield/audiofield.' . $this
->getPluginLibraryName();
// Start building settings to pass to the javascript jplayer builder.
$player_settings = [
'playertype' => 'circle',
// JPlayer expects this as a 0 - 1 value.
'volume' => $settings['audio_player_initial_volume'] / 10,
'files' => [],
];
// Format files for output.
$template_files = $this
->getItemRenderList($items);
foreach ($template_files as $renderInfo) {
// Add entry to player settings for this file.
$player_settings['files'][] = [
'file' => $renderInfo->url
->toString(),
'description' => $renderInfo->description,
'filetype' => $renderInfo->filetype,
'fid' => $renderInfo->id,
'autoplay' => $settings['audio_player_autoplay'],
'lazyload' => $settings['audio_player_lazyload'],
];
}
}
else {
// Need to determine quantity of valid items.
$template_settings['item_count'] = 0;
foreach ($items as $item) {
if ($this
->validateEntityAgainstPlayer($item)) {
$template_settings['item_count']++;
}
}
// If there is only a single file, we render as a standard player.
if ($template_settings['item_count'] == 1) {
// Only require the default library.
$library = 'audiofield/audiofield.' . $this
->getPluginLibraryName();
// Set the template theme name.
$template_theme = 'default_single';
// Format files for output.
$template_files = $this
->getItemRenderList($items, 1);
foreach ($template_files as $renderInfo) {
// Start building settings to pass to the javascript jplayer builder.
$player_settings = [
'playertype' => 'default',
'file' => $renderInfo->url
->toString(),
'description' => $renderInfo->description,
'unique_id' => $renderInfo->id,
'filetype' => $renderInfo->filetype,
// JPlayer expects this as a 0 - 1 value.
'volume' => $settings['audio_player_initial_volume'] / 10,
'autoplay' => $settings['audio_player_autoplay'],
'lazyload' => $settings['audio_player_lazyload'],
];
// Store the unique id for the template.
$template_settings['id'] = $renderInfo->id;
}
}
else {
// Requires the playlist library.
$library = 'audiofield/audiofield.' . $this
->getPluginLibraryName() . '.playlist';
// Set the template theme name.
$template_theme = 'default_multiple';
// Start building settings to pass to the javascript jplayer builder.
$player_settings = [
'playertype' => 'playlist',
// JPlayer expects this as a 0 - 1 value.
'volume' => $settings['audio_player_initial_volume'] / 10,
'files' => [],
'filetypes' => [],
'autoplay' => $settings['audio_player_autoplay'],
'lazyload' => $settings['audio_player_lazyload'],
];
// Format files for output.
$template_files = $this
->getItemRenderList($items);
foreach ($template_files as $renderInfo) {
// Add entry to player settings for this file.
$player_settings['files'][] = [
'file' => $renderInfo->url
->toString(),
'description' => $renderInfo->description,
'filetype' => $renderInfo->filetype,
];
$player_settings['filetypes'][] = $renderInfo->filetype;
// Used to generate unique container.
$player_settings['unique_id'] = $template_settings['id'] = $renderInfo->id;
}
// Use only unique values in the filetypes.
$player_settings['filetypes'] = array_unique($player_settings['filetypes']);
}
}
return [
'audioplayer' => [
'#theme' => 'audioplayer',
'#plugin_id' => 'jplayer',
'#plugin_theme' => $template_theme,
'#settings' => $template_settings,
'#files' => $template_files,
],
'downloads' => $this
->createDownloadList($items, $settings),
'#attached' => [
'library' => [
// Attach the jPlayer library.
$library,
// Attach the jPlayer theme.
'audiofield/' . $settings['audio_player_jplayer_theme'],
],
'drupalSettings' => [
'audiofieldjplayer' => [
$this
->getUniqueRenderId() => $player_settings,
],
],
],
];
}