You are here

class MediaFeedsInternetProvider in Media Feeds 7

Same name and namespace in other branches
  1. 7.2 includes/MediaFeedsInternetProvider.inc \MediaFeedsInternetProvider

Wraps a media_internet provider to implement the interface of MediaFeedsProvider and provide workarounds for some issues. As the issues get fixed the workarounds can be removed.

Hierarchy

Expanded class hierarchy of MediaFeedsInternetProvider

2 string references to 'MediaFeedsInternetProvider'
media_feeds_file_presave in ./media_feeds.module
Implements hook_file_presave().
media_feeds_set_target in ./media_feeds.module
The actual mapping happens here.

File

includes/MediaFeedsInternetProvider.inc, line 13
Provides a wrapper class for media_internet providers.

View source
class MediaFeedsInternetProvider extends MediaFeedsProvider {
  protected $provider;

  /**
   * Construcor.
   *
   * @param $provider
   *   The media_internet provider to wrap for workarounds.
   */
  public function __construct($value, $config = array()) {
    parent::__construct($value, $config);
    $this->provider = media_internet_get_provider($this->value);
  }
  private $existingFile = NULL;

  /**
   * Looks for an existing file.
   *
   * @return
   *   The fid, if an existing file is in the database. FALSE if no existing
   *   file is found.
   */
  public function getExistingFile() {
    if ($this->existingFile === NULL) {
      $this->existingFile = FALSE;
      $file = $this->provider
        ->getFileObject();
      if ($file) {
        $query = new EntityFieldQuery();
        $existing = $query
          ->entityCondition('entity_type', 'file')
          ->propertyCondition('uri', $file->uri)
          ->execute();
        if ($existing && isset($existing['file']) && is_array($existing['file'])) {
          $this->existingFile = reset($existing['file'])->fid;
        }
      }
    }
    return $this->existingFile;
  }

  /**
   * If required, providers can validate the embedCode.
   *
   * However some providers fail validation, if the file already exists in the
   * media library. That means that validation has to be skipped for existing
   * files. Currently known providers don't do others things in validate(), so
   * skipping should be OK.
   *
   * Related Issue: When pasting the URL of a file which already exists in the
   * media library, media_internet throws a validation error
   * (http://drupal.org/node/1121808).
   */
  public function validate() {
    if (!$this
      ->getExistingFile()) {
      return $this->provider
        ->validate();
    }
  }

  /**
   * Get the file object that can be validated and saved.
   *
   * When saving an existing file $file->fid must be this fid of the existing
   * file and $file->is_new must be FALSE. Otherwise a PDOException will be
   * thrown, because the file uri is a unique key.
   *
   * See for example: Trying to add the same YouTube URL twice causes a primary
   * key violation (http://drupal.org/node/952422).
   *
   * Instead of skipping those files (by failing validation) we must work
   * around that issue.
   */
  public function getFileObject() {
    $file = $this->provider
      ->getFileObject();
    $fid = $this
      ->getExistingFile();
    if ($fid) {
      $file->fid = $fid;
      $file->is_new = FALSE;
    }
    return $file;
  }
  public static $currentExistingFile = FALSE;

  /**
   * Let the provider save the file. Since providers don't use the workaround
   * at MediaFeedsProviderWrapper::getFileObject() internally, hook into that
   * process using hook_file_presave().
   *
   * MediaFeedsProviderWrapper::$currentExistingFile will be the fid of an
   * existing file, if a workaround is required. Otherwise it is FALSE.
   */
  public function save() {
    self::$currentExistingFile = $this
      ->getExistingFile();
    $file = $this->provider
      ->save();
    self::$currentExistingFile = FALSE;
    return $file;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MediaFeedsInternetProvider::$currentExistingFile public static property
MediaFeedsInternetProvider::$existingFile private property
MediaFeedsInternetProvider::$provider protected property
MediaFeedsInternetProvider::getExistingFile public function Looks for an existing file.
MediaFeedsInternetProvider::getFileObject public function Get the file object that can be validated and saved. Overrides MediaFeedsProvider::getFileObject
MediaFeedsInternetProvider::save public function Let the provider save the file. Since providers don't use the workaround at MediaFeedsProviderWrapper::getFileObject() internally, hook into that process using hook_file_presave(). Overrides MediaFeedsProvider::save
MediaFeedsInternetProvider::validate public function If required, providers can validate the embedCode. Overrides MediaFeedsProvider::validate
MediaFeedsInternetProvider::__construct public function Construcor. Overrides MediaFeedsProvider::__construct
MediaFeedsProvider::$config protected property An associative array of configuration options.
MediaFeedsProvider::$entity protected property The target entity.
MediaFeedsProvider::$source protected property The FeedsSource.
MediaFeedsProvider::$target protected property The target name.
MediaFeedsProvider::$value protected property The values to save.