You are here

function oembed_file_load in oEmbed 7

Same name and namespace in other branches
  1. 8 oembed.file.inc \oembed_file_load()
  2. 7.0 oembed.file.inc \oembed_file_load()

Implements hook_file_load().

File

./oembed.file.inc, line 85

Code

function oembed_file_load($files) {
  foreach ($files as $file) {
    $scheme = file_uri_scheme($file->uri);
    if ($scheme == 'oembed' && !isset($file->metadata['oembed'])) {

      // Load plain oEmbed response onto file entity.

      /** @var \DrupalStreamWrapperInterface $wrapper */
      $wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
      $file->metadata['oembed'] = oembed_get_data($wrapper
        ->getExternalUrl());

      // Retrieve any missing images dimensions.
      oembed_metadata_fetch_image_dimensions($file);
      foreach (array(
        'oembed',
        'width',
        'height',
      ) as $name) {
        if (!empty($file->metadata[$name])) {
          $value = $file->metadata[$name];
          db_merge('file_metadata')
            ->fields(array(
            'value' => serialize($value),
          ))
            ->key(array(
            'fid' => $file->fid,
            'name' => $name,
          ))
            ->execute();
        }
      }
    }
  }
}