feat: saut Drupal 10.6.22 -> 11.4.13
- core 11.4.13 (patch 2671162 OK), gin 5.0.16 + claro (adminimal/seven retires), ban desinstalle - audiofield + bulkdelete en takeover custom (abandonnes upstream, plugins convertis en attributs PHP8) - 9 modules montes pour D11 (extlink 3.0.1, imagemagick 5.0.2, js_cookie 2.0.2, sophron 3.1.1, persistent_login 2.3.0, email_registration 2.0.0-rc9, embed 1.12, file_mdm 3.2.1, path_alias_xt dev-2.x) - decoupled.info.yml widen ^11 - updb OK, cim OK, cex OK, no entity changes, front identique (diffs markup D11 only) Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\audiofield\Annotation;
|
||||
|
||||
use Drupal\Component\Plugin\Attribute\Plugin;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_CLASS)]
|
||||
class AudioPlayer extends Plugin {
|
||||
|
||||
public string|\Stringable $description = '';
|
||||
|
||||
public function __construct(
|
||||
string $id,
|
||||
string|\Stringable|null $title = '',
|
||||
public array $fileTypes = [],
|
||||
string|\Stringable|null $description = '',
|
||||
public string $libraryName = '',
|
||||
public string $website = '',
|
||||
) {
|
||||
parent::__construct($id, $title ?? '');
|
||||
$this->description = $description ?? '';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\audiofield;
|
||||
|
||||
use Drupal\Core\Plugin\DefaultPluginManager;
|
||||
use Drupal\Core\Cache\CacheBackendInterface;
|
||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||
|
||||
/**
|
||||
* Manages audio player plugins.
|
||||
*/
|
||||
class AudioFieldPlayerManager extends DefaultPluginManager {
|
||||
|
||||
/**
|
||||
* Constructs a new AudioFieldPlayerManager.
|
||||
*
|
||||
* @param \Traversable $namespaces
|
||||
* An object that implements \Traversable which contains the root paths
|
||||
* keyed by the corresponding namespace to look for plugin implementations.
|
||||
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
|
||||
* Cache backend instance to use.
|
||||
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
||||
* The module handler.
|
||||
*/
|
||||
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
|
||||
parent::__construct('Plugin/AudioPlayer', $namespaces, $module_handler, 'Drupal\audiofield\AudioFieldPluginBase', 'Drupal\audiofield\Annotation\AudioPlayer');
|
||||
|
||||
$this->alterInfo('audiofield');
|
||||
$this->setCacheBackend($cache_backend, 'audiofield_audioplayer');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,644 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\audiofield;
|
||||
|
||||
use Drupal\Core\Link;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\file\FileInterface;
|
||||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Component\Utility\UrlHelper;
|
||||
use Drupal\Component\Utility\Random;
|
||||
use Drupal\Component\Plugin\PluginBase;
|
||||
use Drupal\Core\Asset\LibraryDiscoveryInterface;
|
||||
use Drupal\file\Plugin\Field\FieldType\FileItem;
|
||||
use Drupal\link\LinkItemInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\Core\Messenger\MessengerInterface;
|
||||
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
|
||||
use Drupal\Core\File\FileSystemInterface;
|
||||
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
||||
use Drupal\Core\File\FileUrlGenerator;
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
|
||||
/**
|
||||
* Base class for audiofield plugins. Includes global functions.
|
||||
*/
|
||||
abstract class AudioFieldPluginBase extends PluginBase implements ContainerFactoryPluginInterface {
|
||||
|
||||
use StringTranslationTrait;
|
||||
|
||||
/**
|
||||
* Library discovery service.
|
||||
*
|
||||
* @var Drupal\Core\Asset\LibraryDiscoveryInterface
|
||||
*/
|
||||
protected $libraryDiscovery;
|
||||
|
||||
/**
|
||||
* Messenger service.
|
||||
*
|
||||
* @var Drupal\Core\Messenger\MessengerInterface
|
||||
*/
|
||||
protected $messenger;
|
||||
|
||||
/**
|
||||
* File System service.
|
||||
*
|
||||
* @var Drupal\Core\File\FileSystemInterface
|
||||
*/
|
||||
protected $fileSystem;
|
||||
|
||||
/**
|
||||
* Messenger service.
|
||||
*
|
||||
* @var Drupal\Core\Logger\LoggerChannelFactoryInterface
|
||||
*/
|
||||
protected $loggerFactory;
|
||||
|
||||
/**
|
||||
* File URL generator service.
|
||||
*
|
||||
* @var Drupal\Core\File\FileUrlGenerator
|
||||
*/
|
||||
protected $fileUrlGenerator;
|
||||
|
||||
/**
|
||||
* The file storage.
|
||||
*
|
||||
* @var \Drupal\file\FileStorage
|
||||
*/
|
||||
protected $fileStorage;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, LibraryDiscoveryInterface $library_discovery, MessengerInterface $messenger, LoggerChannelFactoryInterface $logger_factory, FileSystemInterface $file_system, FileUrlGenerator $file_url_generator, EntityTypeManagerInterface $entity) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
|
||||
$this->libraryDiscovery = $library_discovery;
|
||||
$this->messenger = $messenger;
|
||||
$this->loggerFactory = $logger_factory;
|
||||
$this->fileSystem = $file_system;
|
||||
$this->fileUrlGenerator = $file_url_generator;
|
||||
$this->fileStorage = $entity->getStorage('file');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
||||
return new static(
|
||||
$configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$container->get('library.discovery'),
|
||||
$container->get('messenger'),
|
||||
$container->get('logger.factory'),
|
||||
$container->get('file_system'),
|
||||
$container->get('file_url_generator'),
|
||||
$container->get('entity_type.manager'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the player.
|
||||
*
|
||||
* @param \Drupal\Core\Field\FieldItemListInterface $items
|
||||
* The uploaded item list.
|
||||
* @param string $langcode
|
||||
* The language code.
|
||||
* @param array $settings
|
||||
* An array of additional render settings.
|
||||
*
|
||||
* @return array
|
||||
* Returns the rendered array.
|
||||
*/
|
||||
abstract public function renderPlayer(FieldItemListInterface $items, $langcode, array $settings);
|
||||
|
||||
/**
|
||||
* Gets the plugin_id of the plugin instance.
|
||||
*
|
||||
* @return string
|
||||
* The plugin_id of the plugin instance.
|
||||
*/
|
||||
public function getPluginId() {
|
||||
return $this->pluginDefinition['id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the title of the plugin instance.
|
||||
*
|
||||
* @return string
|
||||
* The title of the plugin instance.
|
||||
*/
|
||||
public function getPluginTitle() {
|
||||
return $this->pluginDefinition['title'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of the main library of the plugin instance.
|
||||
*
|
||||
* @return string
|
||||
* The name of the main library of the plugin instance.
|
||||
*/
|
||||
public function getPluginLibraryName() {
|
||||
return $this->pluginDefinition['libraryName'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the main library instance of this plugin.
|
||||
*
|
||||
* @return array
|
||||
* The definition of the main library for this plugin.
|
||||
*/
|
||||
public function getPluginLibrary() {
|
||||
// Get the main library for this plugin.
|
||||
return $this->libraryDiscovery->getLibraryByName('audiofield', 'audiofield.' . $this->getPluginLibraryName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses library to get version number of installed library.
|
||||
*
|
||||
* @return string
|
||||
* The version number of the currently installed library.
|
||||
*/
|
||||
public function getPluginLibraryVersion() {
|
||||
// Default to 1. This is implemented in the plugins.
|
||||
return '1';
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the location of this plugin's library installation.
|
||||
*
|
||||
* @return string
|
||||
* The library path of the plugin instance.
|
||||
*/
|
||||
public function getPluginLibraryPath() {
|
||||
// Get the main library for this plugin.
|
||||
$library = $this->getPluginLibrary();
|
||||
|
||||
return preg_filter('%(^libraries/[^//]+).*%', '/$1', $library['js'][0]['data']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the remote download source from the plugin's main library.
|
||||
*
|
||||
* @return string
|
||||
* The remote download source of the plugin instance.
|
||||
*/
|
||||
public function getPluginRemoteSource() {
|
||||
// Get the main library for this plugin.
|
||||
$library = $this->getPluginLibrary();
|
||||
|
||||
return $library['remote'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the definition of the plugin implementation.
|
||||
*
|
||||
* @return array
|
||||
* The plugin definition, as returned by the discovery object used by the
|
||||
* plugin manager.
|
||||
*/
|
||||
public function getPluginDefinition() {
|
||||
return $this->t('@title: @description. Plugin library can be found at %librarySource.', [
|
||||
'@title' => $this->getPluginTitle(),
|
||||
'@description' => $this->pluginDefinition['description'],
|
||||
'%librarySource' => $this->getPluginRemoteSource(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if this audio plugin has been properly installed.
|
||||
*
|
||||
* @param bool $log_error
|
||||
* Flag to indicate whether or not alert should be logged/shown.
|
||||
*
|
||||
* @return bool
|
||||
* Returns a boolean indicating install state.
|
||||
*/
|
||||
public function checkInstalled($log_error = TRUE) {
|
||||
// Get the main library for this plugin.
|
||||
$library = $this->getPluginLibrary();
|
||||
|
||||
// Check if the library is installed.
|
||||
if (file_exists(DRUPAL_ROOT . '/' . $library['js'][0]['data'])) {
|
||||
// Check to make sure the installed version is up to date.
|
||||
$this->checkVersion($log_error);
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if this audio plugin version is up to date.
|
||||
*
|
||||
* @param bool $log_error
|
||||
* Flag to indicate whether or not alert should be logged/shown.
|
||||
*
|
||||
* @return bool
|
||||
* Returns a boolean indicating if version is up to date.
|
||||
*/
|
||||
public function checkVersion($log_error = TRUE) {
|
||||
// Get the main library for this plugin.
|
||||
$library = $this->getPluginLibrary();
|
||||
|
||||
// Check to make sure the installed version is up to date.
|
||||
if (version_compare($this->getPluginLibraryVersion(), $library['version'], '<')) {
|
||||
// Log the warning if necessary.
|
||||
if ($log_error) {
|
||||
$message_data = [
|
||||
'@plugin' => $this->getPluginTitle(),
|
||||
'@version' => $this->getPluginLibraryVersion(),
|
||||
'@newversion' => $library['version'],
|
||||
'@download-link' => Link::fromTextAndUrl($this->getPluginRemoteSource(), Url::fromUri($this->getPluginRemoteSource()))->toString(),
|
||||
'%command' => 'drush audiofield-update',
|
||||
'@status_report' => Link::createFromRoute('status report', 'system.status')->toString(),
|
||||
];
|
||||
$this->loggerFactory->get('audiofield')->warning('Warning: @plugin library is out of date. You should upgrade from version @version to version @newversion. You can manually download the required version here: @download-link or you can install automatically by running the command %command. See the @status_report for more information', $message_data);
|
||||
$this->messenger->addWarning($this->t('Warning: @plugin library is out of date. You should upgrade from version @version to version @newversion. You can manually download the required version here: @download-link or you can install automatically by running the command %command. See the @status_report for more information', $message_data));
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows library installation errors for in-use libraries.
|
||||
*/
|
||||
public function showInstallError() {
|
||||
$message_data = [
|
||||
'@library_name' => $this->getPluginLibraryName(),
|
||||
'@status_report' => Link::createFromRoute('status report', 'system.status')->toString(),
|
||||
];
|
||||
$this->loggerFactory->get('audiofield')->error('Error: @library_name library is not currently installed! See the @status_report for more information.', $message_data);
|
||||
$this->messenger->addWarning($this->t('Error: @library_name library is not currently installed! See the @status_report for more information.', $message_data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a file entity will work with this player.
|
||||
*
|
||||
* @param \Drupal\file\FileInterface $file
|
||||
* A file entity.
|
||||
*
|
||||
* @return bool
|
||||
* Indicates if the file is valid for this player or not.
|
||||
*/
|
||||
private function validateFileAgainstPlayer(FileInterface $file) {
|
||||
// Validate the file extension agains the list of valid extensions.
|
||||
$errors = file_validate_extensions($file, implode(' ', $this->pluginDefinition["fileTypes"]));
|
||||
if (count($errors) > 0) {
|
||||
$message_data = [
|
||||
'%filename' => $file->getFilename(),
|
||||
'@player' => $this->getPluginLibraryName(),
|
||||
'%extensions' => implode(', ', $this->pluginDefinition["fileTypes"]),
|
||||
];
|
||||
$this->loggerFactory->get('audiofield')->error('Error playing file %filename: currently selected audio player only supports the following extensions: %extensions', $message_data);
|
||||
$this->messenger->addWarning($this->t('Error playing file %filename: currently selected audio player only supports the following extensions: %extensions', $message_data));
|
||||
return FALSE;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a link entity will work with this player.
|
||||
*
|
||||
* @param \Drupal\Core\Url $link
|
||||
* Url to the link.
|
||||
*
|
||||
* @return bool
|
||||
* Indicates if the link is valid for this player or not.
|
||||
*/
|
||||
private function validateLinkAgainstPlayer(Url $link) {
|
||||
// Check for a valid link and a valid extension.
|
||||
$extension = pathinfo($link->toString(), PATHINFO_EXTENSION);
|
||||
if (!UrlHelper::isValid($link->toString(), FALSE) ||empty($extension)) {
|
||||
// We are currently not validating file types for links.
|
||||
$message_data = [
|
||||
'%link' => $link->toString(),
|
||||
];
|
||||
$this->loggerFactory->get('audiofield')->error('Error playing file: invalid link: %link', $message_data);
|
||||
$this->messenger->addWarning($this->t('Error playing file: invalid link: %link', $message_data));
|
||||
return FALSE;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the class type for an entity.
|
||||
*
|
||||
* @param \Drupal\file\Plugin\Field\FieldType\FileItem|\Drupal\link\Plugin\Field\FieldType\LinkItem $item
|
||||
* Item for which we are determining class type.
|
||||
*
|
||||
* @return string
|
||||
* The class type for the item.
|
||||
*/
|
||||
protected function getClassType($item) {
|
||||
// Handle File entity.
|
||||
if (is_a($item, FileItem::class)) {
|
||||
return 'FileItem';
|
||||
}
|
||||
// Handle Link entity.
|
||||
elseif ($item instanceof LinkItemInterface) {
|
||||
return 'LinkItem';
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that this entity will work with this player.
|
||||
*
|
||||
* @param \Drupal\file\Plugin\Field\FieldType\FileItem|\Drupal\link\Plugin\Field\FieldType\LinkItem $item
|
||||
* Item which we are validating works with the player.
|
||||
*
|
||||
* @return bool
|
||||
* Indicates if the entity is valid for this player or not.
|
||||
*/
|
||||
protected function validateEntityAgainstPlayer($item) {
|
||||
// Handle File entity.
|
||||
if ($this->getClassType($item) == 'FileItem') {
|
||||
// Load the associated file.
|
||||
$file = $this->loadFileFromItem($item);
|
||||
|
||||
// Be certain that the file itself actually exists.
|
||||
if (!$file instanceof FileInterface) {
|
||||
$this->loggerFactory->get('audiofield')->error('Error: file does not exist.');
|
||||
$this->messenger->addWarning($this->t('Error: file does not exist.'));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Validate that this file will work with this player.
|
||||
return $this->validateFileAgainstPlayer($file);
|
||||
}
|
||||
// Handle Link entity.
|
||||
elseif ($this->getClassType($item) == 'LinkItem') {
|
||||
// Get the source URL for this item.
|
||||
$source_url = $this->getAudioSource($item);
|
||||
|
||||
// Be certain that the URL itself actually exists.
|
||||
if (!$source_url instanceof Url) {
|
||||
$this->loggerFactory->get('audiofield')->error('Error: file does not exist.');
|
||||
$this->messenger->addWarning($this->t('Error: file does not exist.'));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Validate that this link will work with this player.
|
||||
return $this->validateLinkAgainstPlayer($source_url);
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a file from an audio file entity.
|
||||
*
|
||||
* @param \Drupal\file\Plugin\Field\FieldType\FileItem|\Drupal\link\Plugin\Field\FieldType\LinkItem $item
|
||||
* Item for which we are loading the file entity.
|
||||
*
|
||||
* @return \Drupal\file\FileInterface
|
||||
* FileInterface from the item.
|
||||
*/
|
||||
private function loadFileFromItem($item) {
|
||||
// Load the associated file.
|
||||
return $this->fileStorage->load($item->get('target_id')->getCastedValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a unique audofield ID.
|
||||
*
|
||||
* @return string
|
||||
* Unique ID for this audio player.
|
||||
*/
|
||||
protected function getUniqueRenderId() {
|
||||
return Html::getUniqueId('audiofield');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a unique ID for an item.
|
||||
*
|
||||
* @param \Drupal\file\Plugin\Field\FieldType\FileItem|\Drupal\link\Plugin\Field\FieldType\LinkItem $item
|
||||
* Item for which we are generating a unique ID.
|
||||
*
|
||||
* @return string
|
||||
* Unique ID for the item.
|
||||
*/
|
||||
private function getUniqueId($item) {
|
||||
// Used to generate unique container.
|
||||
$random_generator = new Random();
|
||||
// Handle File entity.
|
||||
if ($this->getClassType($item) == 'FileItem') {
|
||||
// Load the associated file.
|
||||
$file = $this->loadFileFromItem($item);
|
||||
|
||||
// Craft a unique ID.
|
||||
return Html::getUniqueId('file_' . $file->get('fid')->getValue()[0]['value'] . '_' . $random_generator->name(16, TRUE));
|
||||
}
|
||||
// Handle Link entity.
|
||||
elseif ($this->getClassType($item) == 'LinkItem') {
|
||||
// Craft a unique ID.
|
||||
return Html::getUniqueId('item_' . $random_generator->name(16, TRUE));
|
||||
}
|
||||
return Html::getUniqueId($random_generator->name(16, TRUE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the filetype for an item.
|
||||
*
|
||||
* @param \Drupal\file\Plugin\Field\FieldType\FileItem|\Drupal\link\Plugin\Field\FieldType\LinkItem $item
|
||||
* Item for which we are determining filetype.
|
||||
*
|
||||
* @return string
|
||||
* Filetype for the item.
|
||||
*/
|
||||
private function getFiletype($item) {
|
||||
// Handle File entity.
|
||||
if ($this->getClassType($item) == 'FileItem') {
|
||||
// Load the associated file.
|
||||
$file = $this->loadFileFromItem($item);
|
||||
|
||||
return pathinfo($file->getFilename(), PATHINFO_EXTENSION);
|
||||
}
|
||||
// Handle Link entity.
|
||||
elseif ($this->getClassType($item) == 'LinkItem') {
|
||||
return pathinfo($this->getAudioSource($item)->toString(), PATHINFO_EXTENSION);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get source URL from an audiofield entity.
|
||||
*
|
||||
* @param \Drupal\file\Plugin\Field\FieldType\FileItem|\Drupal\link\Plugin\Field\FieldType\LinkItem $item
|
||||
* Item for which we are determining source.
|
||||
*
|
||||
* @return \Drupal\Core\Url|string
|
||||
* The source URL of an entity.
|
||||
*/
|
||||
private function getAudioSource($item) {
|
||||
if ($this->getClassType($item) == 'FileItem') {
|
||||
// Load the associated file.
|
||||
$file = $this->loadFileFromItem($item);
|
||||
// Get the file URL.
|
||||
return $this->fileUrlGenerator->generate($file->getFileUri());
|
||||
}
|
||||
// Handle Link entity.
|
||||
elseif ($this->getClassType($item) == 'LinkItem') {
|
||||
// Get the file URL.
|
||||
return $item->getUrl();
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a title description from an audiofield entity.
|
||||
*
|
||||
* @param \Drupal\file\Plugin\Field\FieldType\FileItem|\Drupal\link\Plugin\Field\FieldType\LinkItem $item
|
||||
* Item for which a title is being generated.
|
||||
*
|
||||
* @return string
|
||||
* The description of an entity.
|
||||
*/
|
||||
private function getAudioDescription($item) {
|
||||
if ($this->getClassType($item) == 'FileItem') {
|
||||
// Get the file description - use the filename if it doesn't exist.
|
||||
$entity_description = $item->get('description')->getString();
|
||||
if (empty($entity_description)) {
|
||||
// Load the associated file.
|
||||
$file = $this->loadFileFromItem($item);
|
||||
|
||||
return $file->getFilename();
|
||||
}
|
||||
return $entity_description;
|
||||
}
|
||||
// Handle Link entity.
|
||||
elseif ($this->getClassType($item) == 'LinkItem') {
|
||||
// Get the file description - use the filename if it doesn't exist.
|
||||
$entity_description = $item->get('title')->getString();
|
||||
if (empty($entity_description)) {
|
||||
return $item->getUrl()->toString();
|
||||
}
|
||||
return $entity_description;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get required rendering information from an entity.
|
||||
*
|
||||
* @param \Drupal\file\Plugin\Field\FieldType\FileItem|\Drupal\link\Plugin\Field\FieldType\LinkItem $item
|
||||
* Item for which we are getting render information.
|
||||
*
|
||||
* @return object
|
||||
* Render information for an item.
|
||||
*/
|
||||
public function getAudioRenderInfo($item) {
|
||||
return (object) [
|
||||
'item' => $item,
|
||||
'description' => $this->getAudioDescription($item),
|
||||
'url' => $this->getAudioSource($item),
|
||||
'id' => $this->getUniqueId($item),
|
||||
'filetype' => $this->getFiletype($item),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to format file entities for use in the twig themes.
|
||||
*
|
||||
* @param object $items
|
||||
* A list of items for which we need to generate render information.
|
||||
* @param int $limit
|
||||
* An upper limit for the number of files to return. 0 indicates unlimited.
|
||||
*
|
||||
* @return array
|
||||
* A render array containing files in the proper format for rendering.
|
||||
*/
|
||||
public function getItemRenderList($items, $limit = 0) {
|
||||
$template_files = [];
|
||||
foreach ($items as $item) {
|
||||
// If this entity has passed validation, we render it.
|
||||
if ($this->validateEntityAgainstPlayer($item)) {
|
||||
// Get render information for this item.
|
||||
$renderInfo = $this->getAudioRenderInfo($item);
|
||||
// Add the file to the render array.
|
||||
$template_files[] = $renderInfo;
|
||||
|
||||
// Return list if we have hit the limit.
|
||||
if ($limit > 0 && count($template_files) == $limit) {
|
||||
return $template_files;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $template_files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to render list of downloads as an item list.
|
||||
*
|
||||
* @param object $items
|
||||
* A list of items for which we need to generate download links..
|
||||
* @param array $settings
|
||||
* An array of additional render settings.
|
||||
*
|
||||
* @return array
|
||||
* A render array containing download links.
|
||||
*/
|
||||
public function createDownloadList($items, array $settings) {
|
||||
// Check if download links are turned on and there are items.
|
||||
if (!$settings['download_link'] || count($items) == 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$links = [];
|
||||
// Loop over each item.
|
||||
foreach ($items as $item) {
|
||||
if ($this->validateEntityAgainstPlayer($item)) {
|
||||
// Get the source URL for this item.
|
||||
$source_url = $this->getAudioSource($item);
|
||||
|
||||
// Set the download attribute to the filename.
|
||||
$source_path = $source_url->getUri();
|
||||
$filename = basename($source_path);
|
||||
$attributes = $source_url->getOption('attributes');
|
||||
$attributes['download'] = urldecode($filename);
|
||||
$source_url->setOption('attributes', $attributes);
|
||||
|
||||
// Get the entity description for this item.
|
||||
$entity_description = $this->getAudioDescription($item);
|
||||
|
||||
// Add the link.
|
||||
$links[] = [
|
||||
'link' => Link::fromTextAndUrl($entity_description, $source_url)->toString(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'#theme' => 'audiofield_download_links',
|
||||
'#links' => $links,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to render a default player (for fallback).
|
||||
*
|
||||
* @param \Drupal\Core\Field\FieldItemListInterface $items
|
||||
* The uploaded item list.
|
||||
* @param array $settings
|
||||
* An array of additional render settings.
|
||||
*/
|
||||
public function renderDefaultPlayer(FieldItemListInterface $items, array $settings) {
|
||||
return [
|
||||
'audioplayer' => [
|
||||
'#theme' => 'audioplayer',
|
||||
'#plugin_id' => 'default',
|
||||
'#settings' => $settings,
|
||||
'#files' => $this->getItemRenderList($items),
|
||||
],
|
||||
'downloads' => $this->createDownloadList($items, $settings),
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,274 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\audiofield\Commands;
|
||||
|
||||
use Drush\Commands\DrushCommands;
|
||||
use Drush\Drush;
|
||||
use Drupal\Core\File\FileSystemInterface;
|
||||
use Drupal\audiofield\AudioFieldPlayerManager;
|
||||
use Drupal\Core\Archiver\ArchiverManager;
|
||||
|
||||
/**
|
||||
* A Drush commandfile for Audiofield module.
|
||||
*/
|
||||
class AudiofieldCommands extends DrushCommands {
|
||||
|
||||
/**
|
||||
* Library discovery service.
|
||||
*
|
||||
* @var Drupal\audiofield\AudioFieldPlayerManager
|
||||
*/
|
||||
protected $playerManager;
|
||||
|
||||
/**
|
||||
* File system service.
|
||||
*
|
||||
* @var Drupal\Component\FileSystem\FileSystemInterface
|
||||
*/
|
||||
protected $fileSystem;
|
||||
|
||||
/**
|
||||
* Archive manager service.
|
||||
*
|
||||
* @var Drupal\Core\Archiver\ArchiverManager
|
||||
*/
|
||||
protected $archiverManager;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(AudioFieldPlayerManager $player_manager, FileSystemInterface $file_system, ArchiverManager $archiver_manager) {
|
||||
parent::__construct();
|
||||
|
||||
$this->playerManager = $player_manager;
|
||||
$this->fileSystem = $file_system;
|
||||
$this->archiverManager = $archiver_manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads the suggested Audiofield libraries from their remote repos.
|
||||
*
|
||||
* @param string $installLibrary
|
||||
* The name of the library. If omitted, all libraries will be installed.
|
||||
* @param bool $print_messages
|
||||
* Flag indicating if messages should be displayed.
|
||||
*
|
||||
* @command audiofield:download
|
||||
* @aliases audiofield-download
|
||||
*/
|
||||
public function download($installLibrary = '', $print_messages = TRUE) {
|
||||
|
||||
// Get a list of the audiofield plugins.
|
||||
$pluginList = $this->playerManager->getDefinitions();
|
||||
|
||||
// If there is an argument, check to make sure its valid.
|
||||
if (!empty($installLibrary)) {
|
||||
if (!isset($pluginList[$installLibrary . '_audio_player'])) {
|
||||
$this->logger()->error(dt('Error: @library is not a valid Audiofield library.', [
|
||||
'@library' => $installLibrary,
|
||||
]));
|
||||
return;
|
||||
}
|
||||
// If the argument is valid, we only want to install that plugin.
|
||||
$pluginList = [$installLibrary . '_audio_player' => $pluginList[$installLibrary . '_audio_player']];
|
||||
}
|
||||
|
||||
// Loop over each plugin and make sure it's library is installed.
|
||||
foreach ($pluginList as $pluginName => $plugin) {
|
||||
// Create an instance of this plugin.
|
||||
$pluginInstance = $this->playerManager->createInstance($pluginName);
|
||||
|
||||
// Only check install if there is a library for the plugin.
|
||||
if (!$pluginInstance->getPluginLibrary()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip if the plugin is installed.
|
||||
if ($pluginInstance->checkInstalled()) {
|
||||
if ($print_messages) {
|
||||
$this->logger()->notice(dt('Audiofield library for @library is already installed at @location', [
|
||||
'@library' => $pluginInstance->getPluginTitle(),
|
||||
'@location' => $pluginInstance->getPluginLibraryPath(),
|
||||
]));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get the library install path.
|
||||
$path = DRUPAL_ROOT . $pluginInstance->getPluginLibraryPath();
|
||||
// Create the install directory if it does not exist.
|
||||
if (!is_dir($path)) {
|
||||
$this->fileSystem->mkdir($path);
|
||||
}
|
||||
|
||||
// Download the file.
|
||||
$destination = $this->fileSystem->tempnam($this->fileSystem->getTempDirectory(), 'file.') . "tar.gz";
|
||||
system_retrieve_file($pluginInstance->getPluginRemoteSource(), $destination, FALSE);
|
||||
if (!file_exists($destination)) {
|
||||
// Remove the directory.
|
||||
$this->fileSystem->rmdir($path);
|
||||
$this->logger()->error(dt('Error: unable to download @library.', [
|
||||
'@library' => $pluginInstance->getPluginTitle(),
|
||||
]));
|
||||
continue;
|
||||
}
|
||||
$this->fileSystem->move($destination, $path . '/audiofield-dl.zip');
|
||||
if (!file_exists($path . '/audiofield-dl.zip')) {
|
||||
// Remove the directory where we tried to install.
|
||||
$this->fileSystem->rmdir($path);
|
||||
if ($print_messages) {
|
||||
$this->logger()->error(dt('Error: unable to download Audiofield library @library', [
|
||||
'@library' => $pluginInstance->getPluginTitle(),
|
||||
]));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Unzip the file.
|
||||
$zipFile = $this->archiverManager->getInstance(['filepath' => $path . '/audiofield-dl.zip']);
|
||||
$zipFile->extract($path);
|
||||
|
||||
// Remove the downloaded zip file.
|
||||
$this->fileSystem->unlink($path . '/audiofield-dl.zip');
|
||||
|
||||
// If the library still is not installed, we need to move files.
|
||||
if (!$pluginInstance->checkInstalled()) {
|
||||
// Find all folders in this directory and move their
|
||||
// subdirectories up to the parent directory.
|
||||
$directories = $this->fileSystem->scanDirectory($path, '/.*?/', [
|
||||
'recurse' => FALSE,
|
||||
]);
|
||||
foreach ($directories as $dirName) {
|
||||
$this->fileSystem->move($path . '/' . $dirName->filename, $this->fileSystem->getTempDirectory() . '/temp_audiofield', FileSystemInterface::EXISTS_REPLACE);
|
||||
$this->fileSystem->rmdir($path);
|
||||
$this->fileSystem->move($this->fileSystem->getTempDirectory() . '/temp_audiofield', $path, FileSystemInterface::EXISTS_REPLACE);
|
||||
}
|
||||
|
||||
// Projekktor source files need to be installed.
|
||||
if ($pluginInstance->getPluginId() == 'projekktor_audio_player') {
|
||||
$process = Drush::process('npm install', $path);
|
||||
$process->run();
|
||||
$process = Drush::process('grunt --force', $path);
|
||||
$process->run();
|
||||
}
|
||||
// Wavesurfer source files need to be installed.
|
||||
if ($pluginInstance->getPluginId() == 'wavesurfer_audio_player') {
|
||||
$this->logger()->notice(dt('Installing @library', [
|
||||
'@library' => $pluginInstance->getPluginTitle(),
|
||||
]));
|
||||
$process = Drush::process('npm install', $path);
|
||||
$process->run();
|
||||
$this->logger()->notice(dt('Building @library', [
|
||||
'@library' => $pluginInstance->getPluginTitle(),
|
||||
]));
|
||||
$process = Drush::process('npm run build', $path);
|
||||
$process->run();
|
||||
}
|
||||
}
|
||||
if ($pluginInstance->checkInstalled()) {
|
||||
if ($print_messages) {
|
||||
$this->logger()->notice(dt('Audiofield library for @library has been successfully installed at @location', [
|
||||
'@library' => $pluginInstance->getPluginTitle(),
|
||||
'@location' => $pluginInstance->getPluginLibraryPath(),
|
||||
]));
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Remove the directory where we tried to install.
|
||||
$this->fileSystem->rmdir($path);
|
||||
if ($print_messages) {
|
||||
$this->logger()->error(dt('Error: unable to install Audiofield library @library', [
|
||||
'@library' => $pluginInstance->getPluginTitle(),
|
||||
]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates Audiofield libraries from their remote repos if out of date.
|
||||
*
|
||||
* @param string $updateLibrary
|
||||
* The name of the library. If omitted, all libraries will be updated.
|
||||
* @param bool $print_messages
|
||||
* Flag indicating if messages should be displayed.
|
||||
*
|
||||
* @command audiofield:update
|
||||
* @aliases audiofield-update
|
||||
*/
|
||||
public function update($updateLibrary = '', $print_messages = TRUE) {
|
||||
|
||||
// Get a list of the audiofield plugins.
|
||||
$pluginList = $this->playerManager->getDefinitions();
|
||||
|
||||
// If there is an argument, check to make sure its valid.
|
||||
if (!empty($updateLibrary)) {
|
||||
if (!isset($pluginList[$updateLibrary . '_audio_player'])) {
|
||||
$this->logger()->error(dt('Error: @library is not a valid Audiofield library.', [
|
||||
'@library' => $updateLibrary,
|
||||
]));
|
||||
return;
|
||||
}
|
||||
// If the argument is valid, we only want to install that plugin.
|
||||
$pluginList = [$updateLibrary . '_audio_player' => $pluginList[$updateLibrary . '_audio_player']];
|
||||
}
|
||||
|
||||
// Loop over each plugin and make sure it's library is installed.
|
||||
foreach ($pluginList as $pluginName => $plugin) {
|
||||
// Create an instance of this plugin.
|
||||
$pluginInstance = $this->playerManager->createInstance($pluginName);
|
||||
|
||||
// Only check install if there is a library for the plugin.
|
||||
if (!$pluginInstance->getPluginLibrary()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get the library install path.
|
||||
$path = DRUPAL_ROOT . $pluginInstance->getPluginLibraryPath();
|
||||
|
||||
// If the library isn't installed at all we just run the install.
|
||||
if (!$pluginInstance->checkInstalled(FALSE)) {
|
||||
$this->download($pluginInstance->getPluginLibraryName());
|
||||
continue;
|
||||
}
|
||||
|
||||
// Don't updating the library if its up to date.
|
||||
if ($pluginInstance->checkVersion(FALSE)) {
|
||||
$this->logger()->notice(dt('Audiofield library for @library is already up to date', [
|
||||
'@library' => $pluginInstance->getPluginTitle(),
|
||||
]));
|
||||
continue;
|
||||
}
|
||||
|
||||
// Move the current installation to the temp directory.
|
||||
$this->fileSystem->move($path, $this->fileSystem->getTempDirectory() . '/temp_audiofield', TRUE);
|
||||
// If the directory failed to move, just delete it.
|
||||
if (is_dir($path)) {
|
||||
$this->fileSystem->rmdir($path);
|
||||
}
|
||||
|
||||
// Run the install command now to get the latest version.
|
||||
$this->download($updateLibrary, FALSE);
|
||||
|
||||
// Check if library has been properly installed.
|
||||
if ($pluginInstance->checkInstalled()) {
|
||||
// Remove the temporary directory.
|
||||
$this->fileSystem->rmdir($this->fileSystem->getTempDirectory() . '/temp_audiofield');
|
||||
$this->logger()->notice(dt('Audiofield library for @library has been successfully updated at @location', [
|
||||
'@library' => $pluginInstance->getPluginTitle(),
|
||||
'@location' => $pluginInstance->getPluginLibraryPath(),
|
||||
]));
|
||||
}
|
||||
else {
|
||||
// Remove the directory where we tried to install.
|
||||
$this->fileSystem->rmdir($path);
|
||||
$this->logger()->error(dt('Error: unable to update Audiofield library @library', [
|
||||
'@library' => $pluginInstance->getPluginTitle(),
|
||||
]));
|
||||
// Restore the original install since we failed to update.
|
||||
$this->fileSystem->move($this->fileSystem->getTempDirectory() . '/temp_audiofield', $path, TRUE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\audiofield\Plugin\AudioPlayer;
|
||||
|
||||
use Drupal\audiofield\Annotation\AudioPlayer;
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\audiofield\AudioFieldPluginBase;
|
||||
|
||||
#[AudioPlayer(
|
||||
id: 'audiojs_audio_player',
|
||||
title: new TranslatableMarkup('audio.js audio player'),
|
||||
description: new TranslatableMarkup('Drop-in javascript library using native <audio> tag.'),
|
||||
fileTypes: ['mp3'],
|
||||
libraryName: 'audiojs',
|
||||
website: 'https://kolber.github.io/audiojs/',
|
||||
)]
|
||||
class AudioJsAudioPlayer extends AudioFieldPluginBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
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 audio.js builder.
|
||||
$player_settings = [
|
||||
// Audio.js expects this as a 0 - 1 range.
|
||||
'volume' => ($settings['audio_player_initial_volume'] / 10),
|
||||
'element' => '',
|
||||
];
|
||||
|
||||
// Create arrays to pass to the twig theme..
|
||||
$template_settings = $settings;
|
||||
// Format files for output.
|
||||
$template_files = $this->getItemRenderList($items);
|
||||
foreach ($template_files as $renderInfo) {
|
||||
// Used to generate unique container.
|
||||
$player_settings['element'] = $template_settings['id'] = 'audiofield_audiojs_' . $renderInfo->id;
|
||||
}
|
||||
|
||||
// If we have at least one audio file, we render.
|
||||
return [
|
||||
'audioplayer' => [
|
||||
'#theme' => 'audioplayer',
|
||||
'#plugin_id' => 'audiojs',
|
||||
'#settings' => $template_settings,
|
||||
'#files' => $template_files,
|
||||
],
|
||||
'downloads' => $this->createDownloadList($items, $settings),
|
||||
'#attached' => [
|
||||
'library' => [
|
||||
// Attach the audio.js library.
|
||||
'audiofield/audiofield.' . $this->getPluginLibraryName(),
|
||||
],
|
||||
'drupalSettings' => [
|
||||
'audiofieldaudiojs' => [
|
||||
$this->getUniqueRenderId() => $player_settings,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\audiofield\Plugin\AudioPlayer;
|
||||
|
||||
use Drupal\audiofield\Annotation\AudioPlayer;
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\audiofield\AudioFieldPluginBase;
|
||||
|
||||
#[AudioPlayer(
|
||||
id: 'default_mp3_player',
|
||||
title: new TranslatableMarkup('default HTML5 audio player'),
|
||||
description: new TranslatableMarkup('Default html5 player - built into HTML specification.'),
|
||||
fileTypes: ['mp3', 'mp4', 'm4a', '3gp', 'aac', 'wav', 'ogg', 'oga', 'flac', 'webm'],
|
||||
libraryName: 'default',
|
||||
)]
|
||||
class DefaultMp3Player extends AudioFieldPluginBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function renderPlayer(FieldItemListInterface $items, $langcode, array $settings) {
|
||||
// Simply return the default constructor.
|
||||
return $this->renderDefaultPlayer($items, $settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function checkInstalled($log_error = FALSE) {
|
||||
// This is built in to HTML5, so it is always "installed".
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\audiofield\Plugin\AudioPlayer;
|
||||
|
||||
use Drupal\audiofield\Annotation\AudioPlayer;
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\audiofield\AudioFieldPluginBase;
|
||||
use Drupal\Component\Serialization\Json;
|
||||
|
||||
#[AudioPlayer(
|
||||
id: 'jplayer_audio_player',
|
||||
title: new TranslatableMarkup('jPlayer audio player'),
|
||||
description: new TranslatableMarkup('Free and open source media library.'),
|
||||
fileTypes: ['mp3', 'mp4', 'wav', 'ogg', 'oga', 'webm'],
|
||||
libraryName: 'jplayer',
|
||||
website: 'http://jplayer.org/',
|
||||
)]
|
||||
class JPlayerAudioPlayer extends AudioFieldPluginBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
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'],
|
||||
];
|
||||
}
|
||||
}
|
||||
// This is a normal jPlayer skin, so we render normally.
|
||||
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;
|
||||
}
|
||||
}
|
||||
// If we have multiple files, we need to render this as a playlist.
|
||||
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,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPluginLibraryVersion() {
|
||||
// Parse the JSON file for version info.
|
||||
$library_path = $this->fileSystem->realpath(DRUPAL_ROOT . $this->getPluginLibraryPath() . '/package.json');
|
||||
$library_data = Json::decode(file_get_contents($library_path));
|
||||
return $library_data['version'];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\audiofield\Plugin\AudioPlayer;
|
||||
|
||||
use Drupal\audiofield\Annotation\AudioPlayer;
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\audiofield\AudioFieldPluginBase;
|
||||
use Drupal\Component\Serialization\Json;
|
||||
|
||||
#[AudioPlayer(
|
||||
id: 'mediaelement_audio_player',
|
||||
title: new TranslatableMarkup('MediaElement audio player'),
|
||||
description: new TranslatableMarkup('A dependable HTML media framework.'),
|
||||
fileTypes: ['mp3', 'oga', 'ogg', 'wav'],
|
||||
libraryName: 'mediaelement',
|
||||
website: 'http://mediaelementjs.com/',
|
||||
)]
|
||||
class MediaElementAudioPlayer extends AudioFieldPluginBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
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 MediaElement builder.
|
||||
$player_settings = [
|
||||
// MediaElement expects this as a 0 - 1 range.
|
||||
'volume' => ($settings['audio_player_initial_volume'] / 10),
|
||||
'elements' => [],
|
||||
];
|
||||
|
||||
// Format files for output.
|
||||
$template_files = $this->getItemRenderList($items);
|
||||
foreach ($template_files as $renderInfo) {
|
||||
// Pass the element name for the player so we know what to render.
|
||||
$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' => [
|
||||
// Attach the MediaElement library.
|
||||
'audiofield/audiofield.' . $this->getPluginLibraryName(),
|
||||
],
|
||||
'drupalSettings' => [
|
||||
'audiofieldmediaelement' => [
|
||||
$this->getUniqueRenderId() => $player_settings,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPluginLibraryVersion() {
|
||||
// Parse the JSON file for version info.
|
||||
$library_path = $this->fileSystem->realpath(DRUPAL_ROOT . $this->getPluginLibraryPath() . '/package.json');
|
||||
$library_data = Json::decode(file_get_contents($library_path));
|
||||
return $library_data['version'];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\audiofield\Plugin\AudioPlayer;
|
||||
|
||||
use Drupal\audiofield\Annotation\AudioPlayer;
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\audiofield\AudioFieldPluginBase;
|
||||
use Drupal\Component\Serialization\Json;
|
||||
|
||||
#[AudioPlayer(
|
||||
id: 'projekktor_audio_player',
|
||||
title: new TranslatableMarkup('Projekktor audio player'),
|
||||
description: new TranslatableMarkup('Free Web Video Player (converted for audio)'),
|
||||
fileTypes: ['mp3', 'mp4', 'ogg', 'oga', 'wav'],
|
||||
libraryName: 'projekktor',
|
||||
website: 'http://www.projekktor.com/',
|
||||
)]
|
||||
class ProjekktorAudioPlayer extends AudioFieldPluginBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
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,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPluginLibraryVersion() {
|
||||
// Parse the JSON file for version info.
|
||||
$library_path = $this->fileSystem->realpath(DRUPAL_ROOT . $this->getPluginLibraryPath() . '/package.json');
|
||||
$library_data = Json::decode(file_get_contents($library_path));
|
||||
return $library_data['version'];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\audiofield\Plugin\AudioPlayer;
|
||||
|
||||
use Drupal\audiofield\Annotation\AudioPlayer;
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\audiofield\AudioFieldPluginBase;
|
||||
use Drupal\Component\Serialization\Json;
|
||||
|
||||
#[AudioPlayer(
|
||||
id: 'soundmanager_audio_player',
|
||||
title: new TranslatableMarkup('SoundManager audio player'),
|
||||
description: new TranslatableMarkup('Simple, reliable cross-platform audio.'),
|
||||
fileTypes: ['mp3', 'mp4', 'ogg', 'oga', 'wav', 'flac'],
|
||||
libraryName: 'soundmanager',
|
||||
website: 'http://www.schillmania.com/projects/soundmanager2',
|
||||
)]
|
||||
class SoundManagerAudioPlayer extends AudioFieldPluginBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
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_settings['count'] = count($items);
|
||||
|
||||
// Start building settings to pass to the javascript SoundManager builder.
|
||||
$player_settings = [
|
||||
// SoundManager expects this as a 0 - 100 range.
|
||||
'volume' => ($settings['audio_player_initial_volume'] * 10),
|
||||
'swfpath' => $this->getPluginLibraryPath() . '/swf/',
|
||||
];
|
||||
|
||||
return [
|
||||
'audioplayer' => [
|
||||
'#theme' => 'audioplayer',
|
||||
'#plugin_id' => 'soundmanager',
|
||||
'#plugin_theme' => $settings['audio_player_soundmanager_theme'],
|
||||
'#settings' => $template_settings,
|
||||
'#files' => $this->getItemRenderList($items),
|
||||
],
|
||||
'downloads' => $this->createDownloadList($items, $settings),
|
||||
'#attached' => [
|
||||
'library' => [
|
||||
// Attach the SoundManager library.
|
||||
'audiofield/audiofield.' . $this->getPluginLibraryName(),
|
||||
// Attach the skin library.
|
||||
'audiofield/audiofield.' . $this->getPluginLibraryName() . '.' . $settings['audio_player_soundmanager_theme'],
|
||||
],
|
||||
'drupalSettings' => [
|
||||
'audiofieldsoundmanager' => [
|
||||
$this->getUniqueRenderId() => $player_settings,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPluginLibraryVersion() {
|
||||
// Parse the JSON file for version info.
|
||||
$library_path = $this->fileSystem->realpath(DRUPAL_ROOT . $this->getPluginLibraryPath() . '/package.json');
|
||||
$library_data = Json::decode(file_get_contents($library_path));
|
||||
return $library_data['version'];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\audiofield\Plugin\AudioPlayer;
|
||||
|
||||
use Drupal\audiofield\Annotation\AudioPlayer;
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
|
||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\audiofield\AudioFieldPluginBase;
|
||||
use Drupal\Component\Serialization\Json;
|
||||
use Drupal\file\Entity\File;
|
||||
use Drupal\Core\Link;
|
||||
use Drupal\Core\Asset\LibraryDiscoveryInterface;
|
||||
use Drupal\Core\Messenger\MessengerInterface;
|
||||
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
|
||||
use Drupal\Core\File\FileSystemInterface;
|
||||
use Drupal\Core\File\FileUrlGenerator;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
|
||||
#[AudioPlayer(
|
||||
id: 'wavesurfer_audio_player',
|
||||
title: new TranslatableMarkup('Wavesurfer audio player'),
|
||||
description: new TranslatableMarkup('A customizable audio waveform visualization, built on top of Web Audio API and HTML5 Canvas.'),
|
||||
fileTypes: ['mp3', 'ogg', 'oga', 'wav'],
|
||||
libraryName: 'wavesurfer',
|
||||
website: 'https://github.com/katspaugh/wavesurfer.js',
|
||||
)]
|
||||
class WavesurferAudioPlayer extends AudioFieldPluginBase {
|
||||
|
||||
/**
|
||||
* Module handler service.
|
||||
*
|
||||
* @var \Drupal\Core\Extension\ModuleHandlerInterface
|
||||
*/
|
||||
protected $moduleHandler;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, LibraryDiscoveryInterface $library_discovery, MessengerInterface $messenger, LoggerChannelFactoryInterface $logger_factory, FileSystemInterface $file_system, ModuleHandlerInterface $module_handler, FileUrlGenerator $file_url_generator, EntityTypeManagerInterface $entity) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition, $library_discovery, $messenger, $logger_factory, $file_system, $file_url_generator, $entity);
|
||||
|
||||
$this->moduleHandler = $module_handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
||||
return new static(
|
||||
$configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$container->get('library.discovery'),
|
||||
$container->get('messenger'),
|
||||
$container->get('logger.factory'),
|
||||
$container->get('file_system'),
|
||||
$container->get('module_handler'),
|
||||
$container->get('file_url_generator'),
|
||||
$container->get('entity_type.manager'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
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_files = [];
|
||||
|
||||
// Get a unique render Id.
|
||||
$settings['unique_id'] = $this->getUniqueRenderId();
|
||||
|
||||
// Start building settings to pass to the javascript wavesurfer builder.
|
||||
$player_settings = [
|
||||
// Wavesurfer expects this as a 0 - 1 range.
|
||||
'volume' => ($settings['audio_player_initial_volume'] / 10),
|
||||
'playertype' => ($settings['audio_player_wavesurfer_combine_files'] ? 'playlist' : 'default'),
|
||||
'files' => [],
|
||||
'audioRate' => $settings['audio_player_wavesurfer_audiorate'],
|
||||
'autoCenter' => $settings['audio_player_wavesurfer_autocenter'],
|
||||
'backend' => $settings['audio_player_wavesurfer_backend'],
|
||||
'barGap' => $settings['audio_player_wavesurfer_bargap'],
|
||||
'barHeight' => $settings['audio_player_wavesurfer_barheight'],
|
||||
'barWidth' => $settings['audio_player_wavesurfer_barwidth'],
|
||||
'cursorColor' => $settings['audio_player_wavesurfer_cursorcolor'],
|
||||
'cursorWidth' => $settings['audio_player_wavesurfer_cursorwidth'],
|
||||
'forceDecode' => $settings['audio_player_wavesurfer_forcedecode'],
|
||||
'normalize' => $settings['audio_player_wavesurfer_normalize'],
|
||||
'progressColor' => $settings['audio_player_wavesurfer_progresscolor'],
|
||||
'responsive' => $settings['audio_player_wavesurfer_responsive'],
|
||||
'waveColor' => $settings['audio_player_wavesurfer_wavecolor'],
|
||||
'autoplayNextTrack' => $settings['audio_player_wavesurfer_playnexttrack'],
|
||||
'autoplay' => $settings['audio_player_autoplay'],
|
||||
'unique_id' => $settings['unique_id'],
|
||||
];
|
||||
|
||||
// Format files for output.
|
||||
$template_files = $this->getItemRenderList($items);
|
||||
foreach ($template_files as &$renderInfo) {
|
||||
// Generate settings for this file.
|
||||
$fileSettings = [
|
||||
'id' => $renderInfo->id,
|
||||
'path' => $renderInfo->url->toString(),
|
||||
];
|
||||
|
||||
// Check for Peak files.
|
||||
if ($settings['audio_player_wavesurfer_use_peakfile'] && $this->getClassType($renderInfo->item) == 'FileItem') {
|
||||
|
||||
// Load the associated file.
|
||||
$file = File::load($renderInfo->item->get('target_id')->getCastedValue());
|
||||
// Get the file URL.
|
||||
$deliveryUrl = $file->getFileUri();
|
||||
// Get the file information so we can determin extension.
|
||||
$deliveryFileInfo = pathinfo($this->fileUrlGenerator->generateAbsoluteString($deliveryUrl));
|
||||
// Generate the URL for finding the peak file.
|
||||
$peakData = [
|
||||
'url' => dirname($deliveryUrl) . '/' . $deliveryFileInfo['filename'] . '.json',
|
||||
'arguments' => '--pixels-per-second 20 --bits 8',
|
||||
];
|
||||
// Allow other modules to alter path data.
|
||||
$this->moduleHandler->alter('audiofield_wavesurfer_peak', $peakData);
|
||||
|
||||
// Get the real path.
|
||||
$peakPath = $this->fileSystem->realpath($peakData['url']);
|
||||
|
||||
// If the file is missing and Audiowaveform is installed.
|
||||
if (!file_exists($peakPath) && audiofield_check_audiowaveform_installed()) {
|
||||
|
||||
$deliveryPath = escapeshellarg($this->fileSystem->realpath($deliveryUrl));
|
||||
$peakPath = escapeshellarg($peakPath);
|
||||
$peakArguments = $peakData['arguments'];
|
||||
|
||||
// Generate the data file.
|
||||
shell_exec("audiowaveform -i $deliveryPath -o $peakPath $peakArguments");
|
||||
// If the file didn't generate, log/report the error.
|
||||
if (!file_exists($peakData['url'])) {
|
||||
$message_data = [
|
||||
'@status_report' => Link::createFromRoute('status report', 'system.status')->toString(),
|
||||
];
|
||||
$this->loggerFactory->get('audiofield')->warning('Warning: Unable to generate Waveform peak file. Please check your installation of audiowaveform. More information available in the @status_report.', $message_data);
|
||||
$this->messenger->addWarning($this->t('Warning: Unable to generate Waveform peak file. Please check your installation of audiowaveform. More information available in the @status_report.', $message_data));
|
||||
}
|
||||
}
|
||||
|
||||
// If the file exists, add it to the jQuery and template settings.
|
||||
if (file_exists($peakData['url'])) {
|
||||
$renderInfo->peakpath = $fileSettings['peakpath'] = $this->fileUrlGenerator->generateAbsoluteString($peakData['url']);
|
||||
}
|
||||
}
|
||||
|
||||
// Add this file to the render settings.
|
||||
$player_settings['files'][] = $fileSettings;
|
||||
}
|
||||
|
||||
return [
|
||||
'audioplayer' => [
|
||||
'#theme' => 'audioplayer',
|
||||
'#plugin_id' => 'wavesurfer',
|
||||
'#plugin_theme' => $player_settings['playertype'],
|
||||
'#settings' => $settings,
|
||||
'#files' => $template_files,
|
||||
],
|
||||
'downloads' => $this->createDownloadList($items, $settings),
|
||||
'#attached' => [
|
||||
'library' => [
|
||||
// Attach the wavesurfer library.
|
||||
'audiofield/audiofield.' . $this->getPluginLibraryName(),
|
||||
],
|
||||
'drupalSettings' => [
|
||||
'audiofieldwavesurfer' => [
|
||||
$settings['unique_id'] => $player_settings,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPluginLibraryVersion() {
|
||||
// Parse the JSON file for version info.
|
||||
$library_path = $this->fileSystem->realpath(DRUPAL_ROOT . $this->getPluginLibraryPath() . '/package.json');
|
||||
$library_data = Json::decode(file_get_contents($library_path));
|
||||
return $library_data['version'];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\audiofield\Plugin\AudioPlayer;
|
||||
|
||||
use Drupal\audiofield\Annotation\AudioPlayer;
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\audiofield\AudioFieldPluginBase;
|
||||
|
||||
#[AudioPlayer(
|
||||
id: 'wordpress_audio_player',
|
||||
title: new TranslatableMarkup('WordPress audio player'),
|
||||
description: new TranslatableMarkup('Standalone audio player originally built for WordPress'),
|
||||
fileTypes: ['mp3'],
|
||||
libraryName: 'wordpress',
|
||||
website: 'http://wpaudioplayer.com',
|
||||
)]
|
||||
class WordPressAudioPlayer extends AudioFieldPluginBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
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 WordPress builder.
|
||||
$player_settings = [
|
||||
// WordPress expects this as a 0 - 100 range.
|
||||
'volume' => ($settings['audio_player_initial_volume'] * 10),
|
||||
'animate' => ($settings['audio_player_wordpress_animation'] ? 'yes' : 'no'),
|
||||
'files' => [],
|
||||
'autoplay' => $settings['audio_player_autoplay'],
|
||||
];
|
||||
|
||||
// Format files for output.
|
||||
$template_files = $this->getItemRenderList($items, ($settings['audio_player_wordpress_combine_files'] ? 1 : 0));
|
||||
foreach ($template_files as $renderInfo) {
|
||||
// Pass settings for the file.
|
||||
$player_settings['files'][] = [
|
||||
'file' => $renderInfo->url->toString(),
|
||||
'title' => $renderInfo->description,
|
||||
'unique_id' => $renderInfo->id,
|
||||
];
|
||||
}
|
||||
|
||||
// If we are combining into a single player, make some modifications.
|
||||
if ($settings['audio_player_wordpress_combine_files']) {
|
||||
// Wordpress expects comma-deliminated lists
|
||||
// when using multiple files in a single player.
|
||||
$wp_files = [];
|
||||
$wp_titles = [];
|
||||
foreach ($player_settings['files'] as $wp_file) {
|
||||
$wp_files[] = $wp_file['file'];
|
||||
$wp_titles[] = $wp_file['title'];
|
||||
}
|
||||
|
||||
// Redeclare settings with only a single (combined) file.
|
||||
$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' => [
|
||||
// Attach the WordPress library.
|
||||
'audiofield/audiofield.' . $this->getPluginLibraryName(),
|
||||
],
|
||||
'drupalSettings' => [
|
||||
'audiofieldwordpress' => [
|
||||
$this->getUniqueRenderId() => $player_settings,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPluginLibraryVersion() {
|
||||
// Parse the audio-player.js file for version info.
|
||||
$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];
|
||||
}
|
||||
|
||||
}
|
||||
+654
@@ -0,0 +1,654 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\audiofield\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\Core\Field\Attribute\FieldFormatter;
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
|
||||
use Drupal\audiofield\AudioFieldPlayerManager;
|
||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Field\FormatterBase;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
#[FieldFormatter(
|
||||
id: 'audiofield_audioplayer',
|
||||
label: new TranslatableMarkup('Audiofield Audio Player'),
|
||||
field_types: ['file', 'link'],
|
||||
)]
|
||||
class AudioFieldFieldFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* Audio player management service.
|
||||
*
|
||||
* @var \Drupal\audiofield\AudioFieldPlayerManager
|
||||
*/
|
||||
protected $audioPlayerManager;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AudioFieldPlayerManager $audio_player_manager) {
|
||||
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
|
||||
|
||||
$this->audioPlayerManager = $audio_player_manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
||||
return new static(
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$configuration['field_definition'],
|
||||
$configuration['settings'],
|
||||
$configuration['label'],
|
||||
$configuration['view_mode'],
|
||||
$configuration['third_party_settings'],
|
||||
$container->get('plugin.manager.audiofield')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
// Get the fieldname in a format that works for all forms.
|
||||
$fieldname = $this->fieldDefinition->getItemDefinition()->getFieldDefinition()->getName();
|
||||
|
||||
// Loop over each plugin type and create an entry for it.
|
||||
$plugin_definitions = $this->audioPlayerManager->getDefinitions();
|
||||
$plugins = [
|
||||
'available' => [],
|
||||
'unavailable' => [],
|
||||
];
|
||||
foreach ($plugin_definitions as $plugin_id => $plugin) {
|
||||
// Create an instance of the player.
|
||||
$player = $this->audioPlayerManager->createInstance($plugin_id);
|
||||
if ($player->checkInstalled()) {
|
||||
$plugins['available'][$plugin_id] = $plugin['title'];
|
||||
}
|
||||
else {
|
||||
$plugins['unavailable'][$plugin_id] = $plugin['title'];
|
||||
}
|
||||
}
|
||||
ksort($plugins['available']);
|
||||
|
||||
// Build settings form for display on the structure page.
|
||||
$elements = parent::settingsForm($form, $form_state);
|
||||
$default_player = $this->getSetting('audio_player');
|
||||
if (isset($plugins['unavailable'][$default_player])) {
|
||||
$default_player = 'default_mp3_player';
|
||||
}
|
||||
// Let user select the audio player.
|
||||
$elements['audio_player'] = [
|
||||
'#type' => 'select',
|
||||
'#title' => $this->t('Select Player'),
|
||||
'#default_value' => $default_player,
|
||||
'#options' => $plugins['available'],
|
||||
];
|
||||
if (count($plugins['unavailable']) > 0) {
|
||||
ksort($plugins['unavailable']);
|
||||
$elements['unavailable'] = [
|
||||
'#type' => 'details',
|
||||
'#title' => $this->t('Disabled Players:'),
|
||||
'#open' => FALSE,
|
||||
'#disabled' => TRUE,
|
||||
[
|
||||
'#type' => '#container',
|
||||
'#attributes' => [],
|
||||
'#children' => implode('<br/>', $plugins['unavailable']),
|
||||
],
|
||||
];
|
||||
}
|
||||
// Settings for jPlayer.
|
||||
// Only show when jPlayer is the selected audio player.
|
||||
$jplayer_options = [
|
||||
'none' => 'None (for styling manually with CSS)',
|
||||
// Add the circle skin in (special non-standard custom skin for jPlayer).
|
||||
'audiofield.jplayer.theme_jplayer_circle' => 'jPlayer circle player',
|
||||
];
|
||||
// Build the list of jPlayer available skins.
|
||||
foreach (_audiofield_list_skins('jplayer_audio_player') as $skin) {
|
||||
$jplayer_options[$skin['library_name']] = $skin['name'];
|
||||
}
|
||||
$elements['audio_player_jplayer_theme'] = [
|
||||
'#type' => 'radios',
|
||||
'#title' => $this->t('Select jPlayer Skin'),
|
||||
'#description' => $this->t('jPlayer comes bundled with multiple skins by default. You can install additional skins by placing them in /libraries/jplayer/dist/skin/'),
|
||||
'#default_value' => $this->getSetting('audio_player_jplayer_theme'),
|
||||
'#options' => $jplayer_options,
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'jplayer_audio_player'],
|
||||
],
|
||||
],
|
||||
];
|
||||
// Settings for WaveSurfer.
|
||||
// Only show when WaveSurfer is the selected audio player.
|
||||
$elements['audio_player_wavesurfer_combine_files'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Combine audio files into a single audio player'),
|
||||
'#description' => $this->t('By default Wavesurfer displays files individually. This option combines the files into a playlist so only one file shows at a time.'),
|
||||
'#default_value' => $this->getSetting('audio_player_wavesurfer_combine_files'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wavesurfer_audio_player'],
|
||||
],
|
||||
],
|
||||
];
|
||||
// Settings for WaveSurfer.
|
||||
$elements['audio_player_wavesurfer_audiorate'] = [
|
||||
'#type' => 'number',
|
||||
'#title' => $this->t('Audio Rate'),
|
||||
'#description' => $this->t("Speed at which to play audio. Lower number is slower."),
|
||||
'#default_value' => $this->getSetting('audio_player_wavesurfer_audiorate'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wavesurfer_audio_player']],
|
||||
],
|
||||
],
|
||||
];
|
||||
$elements['audio_player_wavesurfer_autocenter'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Auto Center'),
|
||||
'#description' => $this->t("If a scrollbar is present, center the waveform around the progress."),
|
||||
'#default_value' => $this->getSetting('audio_player_wavesurfer_autocenter'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wavesurfer_audio_player']],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
// Let user select the wavesurfer backend.
|
||||
// @see https://github.com/katspaugh/wavesurfer.js/issues/1382
|
||||
$elements['audio_player_wavesurfer_backend'] = [
|
||||
'#type' => 'select',
|
||||
'#title' => $this->t('Select Backend'),
|
||||
'#description' => $this->t("WebAudio is the default backend for Wavesurfer. Choose MediaElement if you need to play long audio files. See https://github.com/katspaugh/wavesurfer.js/issues/1382."),
|
||||
'#options' => [
|
||||
'WebAudio' => $this->t('WebAudio'),
|
||||
'MediaElement' => $this->t('MediaElement'),
|
||||
],
|
||||
'#default_value' => $this->getSetting('audio_player_wavesurfer_backend'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wavesurfer_audio_player']],
|
||||
],
|
||||
],
|
||||
];
|
||||
$elements['audio_player_wavesurfer_bargap'] = [
|
||||
'#type' => 'number',
|
||||
'#title' => $this->t('Bar Gap'),
|
||||
'#description' => $this->t("The optional spacing between bars of the wave."),
|
||||
'#default_value' => $this->getSetting('audio_player_wavesurfer_bargap'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wavesurfer_audio_player']],
|
||||
],
|
||||
],
|
||||
];
|
||||
$elements['audio_player_wavesurfer_barheight'] = [
|
||||
'#type' => 'number',
|
||||
'#title' => $this->t('Bar Height'),
|
||||
'#description' => $this->t("Height of the waveform bars. Higher number than 1 will increase the waveform bar heights."),
|
||||
'#default_value' => $this->getSetting('audio_player_wavesurfer_barheight'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wavesurfer_audio_player']],
|
||||
],
|
||||
],
|
||||
];
|
||||
$elements['audio_player_wavesurfer_barwidth'] = [
|
||||
'#type' => 'number',
|
||||
'#title' => $this->t('Bar Width'),
|
||||
'#description' => $this->t("If specified, the waveform will be drawn like this: ▁ ▂ ▇ ▃ ▅ ▂"),
|
||||
'#default_value' => $this->getSetting('audio_player_wavesurfer_barwidth'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wavesurfer_audio_player']],
|
||||
],
|
||||
],
|
||||
];
|
||||
$elements['audio_player_wavesurfer_cursorcolor'] = [
|
||||
'#type' => 'color',
|
||||
'#title' => $this->t('Cursor Color'),
|
||||
'#description' => $this->t("The fill color of the cursor indicating the playhead position."),
|
||||
'#default_value' => $this->getSetting('audio_player_wavesurfer_cursorcolor'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wavesurfer_audio_player']],
|
||||
],
|
||||
],
|
||||
];
|
||||
$elements['audio_player_wavesurfer_cursorwidth'] = [
|
||||
'#type' => 'number',
|
||||
'#title' => $this->t('Cursor Width'),
|
||||
'#description' => $this->t("Width of the cursor indicating the playhead position. Measured in pixels."),
|
||||
'#default_value' => $this->getSetting('audio_player_wavesurfer_cursorwidth'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wavesurfer_audio_player']],
|
||||
],
|
||||
],
|
||||
];
|
||||
$elements['audio_player_wavesurfer_forcedecode'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Force Decode'),
|
||||
'#description' => $this->t("Force decoding of audio using web audio when zooming to get a more detailed waveform."),
|
||||
'#default_value' => $this->getSetting('audio_player_wavesurfer_forcedecode'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wavesurfer_audio_player']],
|
||||
],
|
||||
],
|
||||
];
|
||||
$elements['audio_player_wavesurfer_normalize'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Normalize'),
|
||||
'#description' => $this->t("If checked, normalize by the maximum peak instead of 1.0."),
|
||||
'#default_value' => $this->getSetting('audio_player_wavesurfer_normalize'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wavesurfer_audio_player']],
|
||||
],
|
||||
],
|
||||
];
|
||||
$elements['audio_player_wavesurfer_progresscolor'] = [
|
||||
'#type' => 'color',
|
||||
'#title' => $this->t('Progress Color'),
|
||||
'#description' => $this->t("The fill color of the part of the waveform behind the cursor."),
|
||||
'#default_value' => $this->getSetting('audio_player_wavesurfer_progresscolor'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wavesurfer_audio_player']],
|
||||
],
|
||||
],
|
||||
];
|
||||
$elements['audio_player_wavesurfer_responsive'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Responsive'),
|
||||
'#description' => $this->t("If checked, resize the waveform, when the window is resized. This is debounced with a 100ms timeout by default."),
|
||||
'#default_value' => $this->getSetting('audio_player_wavesurfer_responsive'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wavesurfer_audio_player']],
|
||||
],
|
||||
],
|
||||
];
|
||||
$elements['audio_player_wavesurfer_wavecolor'] = [
|
||||
'#type' => 'color',
|
||||
'#title' => $this->t('Wave Color'),
|
||||
'#description' => $this->t("The fill color of the waveform after the cursor."),
|
||||
'#default_value' => $this->getSetting('audio_player_wavesurfer_wavecolor'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wavesurfer_audio_player']],
|
||||
],
|
||||
],
|
||||
];
|
||||
$elements['audio_player_wavesurfer_use_peakfile'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Use Peak File'),
|
||||
'#description' => $this->t("Peak files are used to speed up waveform display on the client and reduce the load on the server by pre-rendering the waveform. These are stored alongside your audio files and are automatically generated."),
|
||||
'#default_value' => $this->getSetting('audio_player_wavesurfer_use_peakfile'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wavesurfer_audio_player']],
|
||||
],
|
||||
],
|
||||
];
|
||||
$elements['audio_player_wavesurfer_playnexttrack'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Automatically skip to next track'),
|
||||
'#description' => $this->t("If checked, next track in playlist will auto-play"),
|
||||
'#default_value' => $this->getSetting('audio_player_wavesurfer_playnexttrack'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wavesurfer_audio_player']],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
// Settings for WordPress.
|
||||
// Only show when WordPress is the selected audio player.
|
||||
$elements['audio_player_wordpress_combine_files'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Combine audio files into a single audio player'),
|
||||
'#description' => $this->t('This can be more difficult to see for the WordPress plugin. Multiple files are represented only by small "next" and "previous" arrows. Unchecking this box causes each file to be rendered as its own player.'),
|
||||
'#default_value' => $this->getSetting('audio_player_wordpress_combine_files'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wordpress_audio_player'],
|
||||
],
|
||||
],
|
||||
];
|
||||
$elements['audio_player_wordpress_animation'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Animate player?'),
|
||||
'#description' => $this->t('If unchecked, the player will always remain open with the title visible.'),
|
||||
'#default_value' => $this->getSetting('audio_player_wordpress_animation'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wordpress_audio_player'],
|
||||
],
|
||||
],
|
||||
];
|
||||
// Settings for SoundManager.
|
||||
// Only show when SoundManager is the selected audio player.
|
||||
$elements['audio_player_soundmanager_theme'] = [
|
||||
'#type' => 'radios',
|
||||
'#title' => $this->t('Select SoundManager Skin'),
|
||||
'#default_value' => $this->getSetting('audio_player_soundmanager_theme'),
|
||||
'#options' => [
|
||||
'default' => $this->t('Default theme'),
|
||||
'player360' => $this->t('360 degree player'),
|
||||
'barui' => $this->t('Bar UI'),
|
||||
'inlineplayer' => $this->t('Inline Player'),
|
||||
'pageplayer' => $this->t('Page Player'),
|
||||
],
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'soundmanager_audio_player'],
|
||||
],
|
||||
],
|
||||
];
|
||||
// Settings for multiple players.
|
||||
$elements['audio_player_initial_volume'] = [
|
||||
'#type' => 'range',
|
||||
'#title' => $this->t('Set Initial Volume'),
|
||||
'#default_value' => $this->getSetting('audio_player_initial_volume'),
|
||||
'#min' => 0,
|
||||
'#max' => 10,
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'jplayer_audio_player']],
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'mediaelement_audio_player']],
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'projekktor_audio_player']],
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'soundmanager_audio_player']],
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wavesurfer_audio_player']],
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wordpress_audio_player']],
|
||||
],
|
||||
],
|
||||
];
|
||||
// Settings for autoplay.
|
||||
$elements['audio_player_autoplay'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Autoplay on page load'),
|
||||
'#default_value' => $this->getSetting('audio_player_autoplay'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'audiojs_audio_player']],
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'default_mp3_player']],
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'jplayer_audio_player']],
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'mediaelement_audio_player']],
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'projekktor_audio_player']],
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wavesurfer_audio_player']],
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'wordpress_audio_player']],
|
||||
],
|
||||
],
|
||||
];
|
||||
// Settings for autoplay.
|
||||
$elements['audio_player_lazyload'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Lazy Load audio'),
|
||||
'#description' => $this->t("This setting causes audio not to be loaded until it is played."),
|
||||
'#default_value' => $this->getSetting('audio_player_lazyload'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'audiojs_audio_player']],
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'default_mp3_player']],
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'jplayer_audio_player']],
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'mediaelement_audio_player']],
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'projekktor_audio_player']],
|
||||
],
|
||||
],
|
||||
];
|
||||
// Settings for download button.
|
||||
$elements['download_button'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Display download button on player'),
|
||||
'#default_value' => $this->getSetting('download_button'),
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
[':input[name="fields[' . $fieldname . '][settings_edit_form][settings][audio_player]"]' => ['value' => 'default_mp3_player']],
|
||||
],
|
||||
],
|
||||
];
|
||||
// Setting for optional download link.
|
||||
$elements['download_link'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Display download link below player'),
|
||||
'#default_value' => $this->getSetting('download_link'),
|
||||
];
|
||||
|
||||
return $elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsSummary() {
|
||||
$plugin_definitions = $this->audioPlayerManager->getDefinitions();
|
||||
|
||||
$settings = $this->getSettings();
|
||||
|
||||
// Show which player we are currently using for the field.
|
||||
$summary = [
|
||||
$this->t('Selected player: <strong>@player</strong>', [
|
||||
'@player' => $plugin_definitions[$settings['audio_player']]['title'],
|
||||
]),
|
||||
];
|
||||
// If this is jPlayer, add those settings.
|
||||
if ($settings['audio_player'] == 'jplayer_audio_player') {
|
||||
// Display theme.
|
||||
$theme = 'None (for styling manually with CSS)';
|
||||
// If this is the custom jplayer circle theme.
|
||||
if ($settings['audio_player_jplayer_theme'] == 'audiofield.jplayer.theme_jplayer_circle') {
|
||||
$theme = 'jPlayer circle player';
|
||||
}
|
||||
// Search for the theme we're using.
|
||||
else {
|
||||
foreach (_audiofield_list_skins('jplayer_audio_player') as $skin) {
|
||||
if ($skin['library_name'] == $settings['audio_player_jplayer_theme']) {
|
||||
$theme = $skin['name'];
|
||||
}
|
||||
}
|
||||
}
|
||||
$summary[] = $this->t('Skin: <strong>@theme</strong>', [
|
||||
'@theme' => $theme,
|
||||
]);
|
||||
}
|
||||
// If this is wavesurfer, add those settings.
|
||||
elseif ($settings['audio_player'] == 'wavesurfer_audio_player') {
|
||||
$summary[] = $this->t('Combine files into single player? <strong>@combine</strong>', [
|
||||
'@combine' => ($settings['audio_player_wavesurfer_combine_files'] ? 'Yes' : 'No'),
|
||||
]);
|
||||
$summary[] = $this->t('Audio Rate: <strong>@value</strong>', [
|
||||
'@value' => $settings['audio_player_wavesurfer_audiorate'],
|
||||
]);
|
||||
$summary[] = $this->t('Auto Center? <strong>@value</strong>', [
|
||||
'@value' => ($settings['audio_player_wavesurfer_autocenter'] ? 'Yes' : 'No'),
|
||||
]);
|
||||
$summary[] = $this->t('Autoplay next track? <strong>@value</strong>', [
|
||||
'@value' => ($settings['audio_player_wavesurfer_playnexttrack'] ? 'Yes' : 'No'),
|
||||
]);
|
||||
$summary[] = $this->t('Backend: <strong>@value</strong>', [
|
||||
'@value' => $settings['audio_player_wavesurfer_backend'],
|
||||
]);
|
||||
$summary[] = $this->t('Bar Gap: <strong>@value</strong>', [
|
||||
'@value' => $settings['audio_player_wavesurfer_bargap'],
|
||||
]);
|
||||
$summary[] = $this->t('Bar Height: <strong>@value</strong>', [
|
||||
'@value' => $settings['audio_player_wavesurfer_barheight'],
|
||||
]);
|
||||
$summary[] = $this->t('Bar Width: <strong>@value</strong>', [
|
||||
'@value' => $settings['audio_player_wavesurfer_barwidth'],
|
||||
]);
|
||||
$summary[] = $this->t('Cursor Color: <span style="border:1px solid black;height:10px;width:10px;display:inline-block;background:@value;"></span>', [
|
||||
'@value' => $settings['audio_player_wavesurfer_cursorcolor'],
|
||||
]);
|
||||
$summary[] = $this->t('Cursor Width: <strong>@value</strong>', [
|
||||
'@value' => $settings['audio_player_wavesurfer_cursorwidth'],
|
||||
]);
|
||||
$summary[] = $this->t('Force Decode? <strong>@value</strong>', [
|
||||
'@value' => ($settings['audio_player_wavesurfer_forcedecode'] ? 'Yes' : 'No'),
|
||||
]);
|
||||
$summary[] = $this->t('Normalize? <strong>@value</strong>', [
|
||||
'@value' => ($settings['audio_player_wavesurfer_normalize'] ? 'Yes' : 'No'),
|
||||
]);
|
||||
$summary[] = $this->t('Progress Color: <span style="border:1px solid black;height:10px;width:10px;display:inline-block;background:@value;"></span>', [
|
||||
'@value' => $settings['audio_player_wavesurfer_progresscolor'],
|
||||
]);
|
||||
$summary[] = $this->t('Responsive? <strong>@value</strong>', [
|
||||
'@value' => ($settings['audio_player_wavesurfer_responsive'] ? 'Yes' : 'No'),
|
||||
]);
|
||||
$summary[] = $this->t('Wave Color: <span style="border:1px solid black;height:10px;width:10px;display:inline-block;background:@value;"></span>', [
|
||||
'@value' => $settings['audio_player_wavesurfer_wavecolor'],
|
||||
]);
|
||||
$summary[] = $this->t('Use Peak File? <strong>@value</strong>', [
|
||||
'@value' => ($settings['audio_player_wavesurfer_use_peakfile'] ? 'Yes' : 'No'),
|
||||
]);
|
||||
}
|
||||
// If this is wordpress, add those settings.
|
||||
elseif ($settings['audio_player'] == 'wordpress_audio_player') {
|
||||
$summary[] = $this->t('Combine files into single player? <strong>@combine</strong>', [
|
||||
'@combine' => ($settings['audio_player_wordpress_combine_files'] ? 'Yes' : 'No'),
|
||||
]);
|
||||
$summary[] = $this->t('Animate player? <strong>@animate</strong>', [
|
||||
'@animate' => ($settings['audio_player_wordpress_animation'] ? 'Yes' : 'No'),
|
||||
]);
|
||||
}
|
||||
// If this is soundmanager, add those settings.
|
||||
elseif ($settings['audio_player'] == 'soundmanager_audio_player') {
|
||||
$skins = [
|
||||
'default' => 'Default theme',
|
||||
'player360' => '360 degree player',
|
||||
'barui' => 'Bar UI',
|
||||
'inlineplayer' => 'Inline Player',
|
||||
'pageplayer' => 'Page Player',
|
||||
];
|
||||
$summary[] = $this->t('Skin: <strong>@skin</strong>', [
|
||||
'@skin' => $skins[$settings['audio_player_soundmanager_theme']],
|
||||
]);
|
||||
}
|
||||
|
||||
// Show combined settings for multiple players.
|
||||
if (in_array($settings['audio_player'], [
|
||||
'jplayer_audio_player',
|
||||
'mediaelement_audio_player',
|
||||
'projekktor_audio_player',
|
||||
'soundmanager_audio_player',
|
||||
'wavesurfer_audio_player',
|
||||
'wordpress_audio_player',
|
||||
])) {
|
||||
// Display volume.
|
||||
$summary[] = $this->t('Initial volume: <strong>@volume out of 10</strong>', [
|
||||
'@volume' => $settings['audio_player_initial_volume'],
|
||||
]);
|
||||
}
|
||||
|
||||
// Display autoplay.
|
||||
if (in_array($settings['audio_player'], [
|
||||
'audiojs_audio_player',
|
||||
'default_mp3_player',
|
||||
'jplayer_audio_player',
|
||||
'mediaelement_audio_player',
|
||||
'projekktor_audio_player',
|
||||
'wavesurfer_audio_player',
|
||||
'wordpress_audio_player',
|
||||
])) {
|
||||
$summary[] = $this->t('Autoplay: <strong>@autoplay</strong>', [
|
||||
'@autoplay' => ($settings['audio_player_autoplay'] ? 'Yes' : 'No'),
|
||||
]);
|
||||
}
|
||||
|
||||
// Display lazy load.
|
||||
if (in_array($settings['audio_player'], [
|
||||
'audiojs_audio_player',
|
||||
'default_mp3_player',
|
||||
'jplayer_audio_player',
|
||||
'mediaelement_audio_player',
|
||||
'projekktor_audio_player',
|
||||
])) {
|
||||
$summary[] = $this->t('Lazy Load: <strong>@lazyload</strong>', [
|
||||
'@lazyload' => ($settings['audio_player_lazyload'] ? 'Yes' : 'No'),
|
||||
]);
|
||||
}
|
||||
|
||||
// Check to make sure the library is installed.
|
||||
$player = $this->audioPlayerManager->createInstance($settings['audio_player']);
|
||||
if (!$player->checkInstalled()) {
|
||||
$summary[] = $this->t('<strong style="color:red;">Error: this player library is currently not installed. Please select another player or reinstall the library.</strong>');
|
||||
}
|
||||
|
||||
// Show whether or not we are displaying download buttons.
|
||||
$summary[] = $this->t('Display download button: <strong>@display_link</strong>', [
|
||||
'@display_link' => ($settings['download_button'] ? 'Yes' : 'No'),
|
||||
]);
|
||||
|
||||
// Show whether or not we are displaying direct downloads.
|
||||
$summary[] = $this->t('Display download link: <strong>@display_link</strong>', [
|
||||
'@display_link' => ($settings['download_link'] ? 'Yes' : 'No'),
|
||||
]);
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return [
|
||||
'audio_player' => 'default_mp3_player',
|
||||
'audio_player_jplayer_theme' => 'none',
|
||||
'audio_player_wavesurfer_combine_files' => FALSE,
|
||||
'audio_player_wavesurfer_audiorate' => 1,
|
||||
'audio_player_wavesurfer_autocenter' => TRUE,
|
||||
'audio_player_wavesurfer_backend' => 'WebAudio',
|
||||
'audio_player_wavesurfer_bargap' => 0,
|
||||
'audio_player_wavesurfer_barheight' => 1,
|
||||
'audio_player_wavesurfer_barwidth' => NULL,
|
||||
'audio_player_wavesurfer_cursorcolor' => '#333',
|
||||
'audio_player_wavesurfer_cursorwidth' => 1,
|
||||
'audio_player_wavesurfer_forcedecode' => FALSE,
|
||||
'audio_player_wavesurfer_normalize' => FALSE,
|
||||
'audio_player_wavesurfer_playnexttrack' => TRUE,
|
||||
'audio_player_wavesurfer_progresscolor' => '#555',
|
||||
'audio_player_wavesurfer_responsive' => FALSE,
|
||||
'audio_player_wavesurfer_wavecolor' => '#999',
|
||||
'audio_player_wavesurfer_use_peakfile' => FALSE,
|
||||
'audio_player_wordpress_combine_files' => FALSE,
|
||||
'audio_player_wordpress_animation' => TRUE,
|
||||
'audio_player_soundmanager_theme' => 'default',
|
||||
'audio_player_initial_volume' => 8,
|
||||
'audio_player_autoplay' => FALSE,
|
||||
'audio_player_lazyload' => FALSE,
|
||||
'download_button' => FALSE,
|
||||
'download_link' => FALSE,
|
||||
] + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items, $langcode) {
|
||||
$elements = [];
|
||||
|
||||
// Early opt-out if the field is empty.
|
||||
if (count($items) <= 0) {
|
||||
return $elements;
|
||||
}
|
||||
|
||||
$plugin_id = $this->getSetting('audio_player');
|
||||
$player = $this->audioPlayerManager->createInstance($plugin_id);
|
||||
|
||||
$elements[] = $player->renderPlayer($items, $langcode, $this->getSettings());
|
||||
return $elements;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user