You are here

class JPlayerAudioPlayer in AudioField 8

Implements the jPlayer Audio Player plugin.

@AudioPlayer ( id = "jplayer_audio_player", title =

Plugin annotation


@Translation("jPlayer audio player"),
  description = @Translation("Free and open source media library."),
  fileTypes = {
    "mp3", "mp4", "wav", "ogg", "oga", "webm",
  },
  libraryName = "jplayer",
  website = "http://jplayer.org/",
)

Hierarchy

Expanded class hierarchy of JPlayerAudioPlayer

File

src/Plugin/AudioPlayer/JPlayerAudioPlayer.php, line 23

Namespace

Drupal\audiofield\Plugin\AudioPlayer
View source
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'],
        ];
      }
    }
    else {

      // Need to determine quantity of valid items.
      $template_settings['item_count'] = 0;
      foreach ($items as $item) {
        if ($this
          ->validateEntityAgainstPlayer($item)) {
          $template_settings['item_count']++;
        }
      }

      // If there is only a single file, we render as a standard player.
      if ($template_settings['item_count'] == 1) {

        // Only require the default library.
        $library = 'audiofield/audiofield.' . $this
          ->getPluginLibraryName();

        // Set the template theme name.
        $template_theme = 'default_single';

        // Format files for output.
        $template_files = $this
          ->getItemRenderList($items, 1);
        foreach ($template_files as $renderInfo) {

          // Start building settings to pass to the javascript jplayer builder.
          $player_settings = [
            'playertype' => 'default',
            'file' => $renderInfo->url
              ->toString(),
            'description' => $renderInfo->description,
            'unique_id' => $renderInfo->id,
            'filetype' => $renderInfo->filetype,
            // JPlayer expects this as a 0 - 1 value.
            'volume' => $settings['audio_player_initial_volume'] / 10,
            'autoplay' => $settings['audio_player_autoplay'],
            'lazyload' => $settings['audio_player_lazyload'],
          ];

          // Store the unique id for the template.
          $template_settings['id'] = $renderInfo->id;
        }
      }
      else {

        // Requires the playlist library.
        $library = 'audiofield/audiofield.' . $this
          ->getPluginLibraryName() . '.playlist';

        // Set the template theme name.
        $template_theme = 'default_multiple';

        // Start building settings to pass to the javascript jplayer builder.
        $player_settings = [
          'playertype' => 'playlist',
          // JPlayer expects this as a 0 - 1 value.
          'volume' => $settings['audio_player_initial_volume'] / 10,
          'files' => [],
          'filetypes' => [],
          'autoplay' => $settings['audio_player_autoplay'],
          'lazyload' => $settings['audio_player_lazyload'],
        ];

        // Format files for output.
        $template_files = $this
          ->getItemRenderList($items);
        foreach ($template_files as $renderInfo) {

          // Add entry to player settings for this file.
          $player_settings['files'][] = [
            'file' => $renderInfo->url
              ->toString(),
            'description' => $renderInfo->description,
            'filetype' => $renderInfo->filetype,
          ];
          $player_settings['filetypes'][] = $renderInfo->filetype;

          // Used to generate unique container.
          $player_settings['unique_id'] = $template_settings['id'] = $renderInfo->id;
        }

        // Use only unique values in the filetypes.
        $player_settings['filetypes'] = array_unique($player_settings['filetypes']);
      }
    }
    return [
      'audioplayer' => [
        '#theme' => 'audioplayer',
        '#plugin_id' => 'jplayer',
        '#plugin_theme' => $template_theme,
        '#settings' => $template_settings,
        '#files' => $template_files,
      ],
      'downloads' => $this
        ->createDownloadList($items, $settings),
      '#attached' => [
        'library' => [
          // Attach the jPlayer library.
          $library,
          // Attach the jPlayer theme.
          'audiofield/' . $settings['audio_player_jplayer_theme'],
        ],
        'drupalSettings' => [
          'audiofieldjplayer' => [
            $this
              ->getUniqueRenderId() => $player_settings,
          ],
        ],
      ],
    ];
  }

  /**
   * {@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'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AudioFieldPluginBase::$fileSystem protected property File System service.
AudioFieldPluginBase::$libraryDiscovery protected property Library discovery service.
AudioFieldPluginBase::$loggerFactory protected property Messenger service.
AudioFieldPluginBase::$messenger protected property Messenger service.
AudioFieldPluginBase::checkInstalled public function Checks to see if this audio plugin has been properly installed. 1
AudioFieldPluginBase::checkVersion public function Checks to see if this audio plugin version is up to date.
AudioFieldPluginBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
AudioFieldPluginBase::createDownloadList public function Used to render list of downloads as an item list.
AudioFieldPluginBase::getAudioDescription private function Get a title description from an audiofield entity.
AudioFieldPluginBase::getAudioRenderInfo public function Get required rendering information from an entity.
AudioFieldPluginBase::getAudioSource private function Get source URL from an audiofield entity.
AudioFieldPluginBase::getClassType protected function Get the class type for an entity.
AudioFieldPluginBase::getFiletype private function Get the filetype for an item.
AudioFieldPluginBase::getItemRenderList public function Used to format file entities for use in the twig themes.
AudioFieldPluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginBase::getPluginDefinition
AudioFieldPluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginBase::getPluginId
AudioFieldPluginBase::getPluginLibrary public function Gets the main library instance of this plugin.
AudioFieldPluginBase::getPluginLibraryName public function Gets the name of the main library of the plugin instance.
AudioFieldPluginBase::getPluginLibraryPath public function Gets the location of this plugin's library installation.
AudioFieldPluginBase::getPluginRemoteSource public function Gets the remote download source from the plugin's main library.
AudioFieldPluginBase::getPluginTitle public function Gets the title of the plugin instance.
AudioFieldPluginBase::getUniqueId private function Get a unique ID for an item.
AudioFieldPluginBase::getUniqueRenderId protected function Get a unique audofield ID.
AudioFieldPluginBase::loadFileFromItem private function Load a file from an audio file entity.
AudioFieldPluginBase::renderDefaultPlayer public function Used to render a default player (for fallback).
AudioFieldPluginBase::showInstallError public function Shows library installation errors for in-use libraries.
AudioFieldPluginBase::validateEntityAgainstPlayer protected function Validate that this entity will work with this player.
AudioFieldPluginBase::validateFileAgainstPlayer private function Validate that a file entity will work with this player.
AudioFieldPluginBase::validateLinkAgainstPlayer private function Validate that a link entity will work with this player.
AudioFieldPluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 1
JPlayerAudioPlayer::getPluginLibraryVersion public function Parses library to get version number of installed library. Overrides AudioFieldPluginBase::getPluginLibraryVersion
JPlayerAudioPlayer::renderPlayer public function Renders the player. Overrides AudioFieldPluginBase::renderPlayer
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.