MediaElementAudioPlayer.php in AudioField 8
File
src/Plugin/AudioPlayer/MediaElementAudioPlayer.php
View source
<?php
namespace Drupal\audiofield\Plugin\AudioPlayer;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\audiofield\AudioFieldPluginBase;
use Drupal\Component\Serialization\Json;
class MediaElementAudioPlayer extends AudioFieldPluginBase {
public function renderPlayer(FieldItemListInterface $items, $langcode, array $settings) {
if (!$this
->checkInstalled()) {
$this
->showInstallError();
return $this
->renderDefaultPlayer($items, $settings);
}
$player_settings = [
'volume' => $settings['audio_player_initial_volume'] / 10,
'elements' => [],
];
$template_files = $this
->getItemRenderList($items);
foreach ($template_files as $renderInfo) {
$player_settings['elements'][] = '#mediaelement_player_' . $renderInfo->id;
}
return [
'audioplayer' => [
'#theme' => 'audioplayer',
'#plugin_id' => 'mediaelement',
'#settings' => $settings,
'#files' => $template_files,
],
'downloads' => $this
->createDownloadList($items, $settings),
'#attached' => [
'library' => [
'audiofield/audiofield.' . $this
->getPluginLibraryName(),
],
'drupalSettings' => [
'audiofieldmediaelement' => [
$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'];
}
}