View source
<?php
namespace Drupal\audiofield\Plugin\AudioPlayer;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\audiofield\AudioFieldPluginBase;
class WordPressAudioPlayer 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,
'animate' => $settings['audio_player_wordpress_animation'] ? 'yes' : 'no',
'files' => [],
'autoplay' => $settings['audio_player_autoplay'],
];
$template_files = $this
->getItemRenderList($items, $settings['audio_player_wordpress_combine_files'] ? 1 : 0);
foreach ($template_files as $renderInfo) {
$player_settings['files'][] = [
'file' => $renderInfo->url
->toString(),
'title' => $renderInfo->description,
'unique_id' => $renderInfo->id,
];
}
if ($settings['audio_player_wordpress_combine_files']) {
$wp_files = [];
$wp_titles = [];
foreach ($player_settings['files'] as $wp_file) {
$wp_files[] = $wp_file['file'];
$wp_titles[] = $wp_file['title'];
}
$player_settings['files'] = [
[
'file' => implode(',', $wp_files),
'title' => implode(',', $wp_titles),
'unique_id' => $player_settings['files'][0]['unique_id'],
],
];
}
return [
'audioplayer' => [
'#theme' => 'audioplayer',
'#plugin_id' => 'wordpress',
'#settings' => $settings,
'#files' => $template_files,
],
'downloads' => $this
->createDownloadList($items, $settings),
'#attached' => [
'library' => [
'audiofield/audiofield.' . $this
->getPluginLibraryName(),
],
'drupalSettings' => [
'audiofieldwordpress' => [
$this
->getUniqueRenderId() => $player_settings,
],
],
],
];
}
public function getPluginLibraryVersion() {
$library_path = $this->fileSystem
->realpath(DRUPAL_ROOT . $this
->getPluginLibraryPath() . '/audio-player.js');
$library_data = file_get_contents($library_path);
$matches = [];
preg_match('%SWFObject v([0-9\\.]+).*%', $library_data, $matches);
return $matches[1];
}
}