View source
<?php
namespace Drupal\audiofield\Plugin\AudioPlayer;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\audiofield\AudioFieldPluginBase;
use Drupal\Component\Serialization\Json;
class JPlayerAudioPlayer extends AudioFieldPluginBase {
public function renderPlayer(FieldItemListInterface $items, $langcode, array $settings) {
if (!$this
->checkInstalled()) {
$this
->showInstallError();
return $this
->renderDefaultPlayer($items, $settings);
}
$template_settings = $settings;
$template_theme = str_replace('audiofield.jplayer.theme_', '', $settings['audio_player_jplayer_theme']);
$player_settings = [];
if ($settings['audio_player_jplayer_theme'] == 'audiofield.jplayer.theme_jplayer_circle') {
$library = 'audiofield/audiofield.' . $this
->getPluginLibraryName();
$player_settings = [
'playertype' => 'circle',
'volume' => $settings['audio_player_initial_volume'] / 10,
'files' => [],
];
$template_files = $this
->getItemRenderList($items);
foreach ($template_files as $renderInfo) {
$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 {
$template_settings['item_count'] = 0;
foreach ($items as $item) {
if ($this
->validateEntityAgainstPlayer($item)) {
$template_settings['item_count']++;
}
}
if ($template_settings['item_count'] == 1) {
$library = 'audiofield/audiofield.' . $this
->getPluginLibraryName();
$template_theme = 'default_single';
$template_files = $this
->getItemRenderList($items, 1);
foreach ($template_files as $renderInfo) {
$player_settings = [
'playertype' => 'default',
'file' => $renderInfo->url
->toString(),
'description' => $renderInfo->description,
'unique_id' => $renderInfo->id,
'filetype' => $renderInfo->filetype,
'volume' => $settings['audio_player_initial_volume'] / 10,
'autoplay' => $settings['audio_player_autoplay'],
'lazyload' => $settings['audio_player_lazyload'],
];
$template_settings['id'] = $renderInfo->id;
}
}
else {
$library = 'audiofield/audiofield.' . $this
->getPluginLibraryName() . '.playlist';
$template_theme = 'default_multiple';
$player_settings = [
'playertype' => 'playlist',
'volume' => $settings['audio_player_initial_volume'] / 10,
'files' => [],
'filetypes' => [],
'autoplay' => $settings['audio_player_autoplay'],
'lazyload' => $settings['audio_player_lazyload'],
];
$template_files = $this
->getItemRenderList($items);
foreach ($template_files as $renderInfo) {
$player_settings['files'][] = [
'file' => $renderInfo->url
->toString(),
'description' => $renderInfo->description,
'filetype' => $renderInfo->filetype,
];
$player_settings['filetypes'][] = $renderInfo->filetype;
$player_settings['unique_id'] = $template_settings['id'] = $renderInfo->id;
}
$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' => [
$library,
'audiofield/' . $settings['audio_player_jplayer_theme'],
],
'drupalSettings' => [
'audiofieldjplayer' => [
$this
->getUniqueRenderId() => $player_settings,
],
],
],
];
}
public function getPluginLibraryVersion() {
$library_path = $this->fileSystem
->realpath(DRUPAL_ROOT . $this
->getPluginLibraryPath() . '/package.json');
$library_data = Json::decode(file_get_contents($library_path));
return $library_data['version'];
}
}