You are here

protected function AudioFieldPluginBase::validateEntityAgainstPlayer in AudioField 8

Validate that this entity will work with this player.

Parameters

\Drupal\file\Plugin\Field\FieldType\FileItem|\Drupal\link\Plugin\Field\FieldType\LinkItem $item: Item which we are validating works with the player.

Return value

bool Indicates if the entity is valid for this player or not.

3 calls to AudioFieldPluginBase::validateEntityAgainstPlayer()
AudioFieldPluginBase::createDownloadList in src/AudioFieldPluginBase.php
Used to render list of downloads as an item list.
AudioFieldPluginBase::getItemRenderList in src/AudioFieldPluginBase.php
Used to format file entities for use in the twig themes.
JPlayerAudioPlayer::renderPlayer in src/Plugin/AudioPlayer/JPlayerAudioPlayer.php
Renders the player.

File

src/AudioFieldPluginBase.php, line 338

Class

AudioFieldPluginBase
Base class for audiofield plugins. Includes global functions.

Namespace

Drupal\audiofield

Code

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);
  }
  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;
}