View source
<?php
function oembed_file_formatter_info() {
$formatters['oembed'] = array(
'label' => t('oEmbed'),
'default settings' => array(
'width' => '560',
'height' => '340',
'wmode' => '',
),
'view callback' => 'oembed_file_formatter_view',
'settings callback' => 'oembed_file_formatter_oembed_settings',
'description' => t('All-purpose oEmbed formatter.'),
);
$formatters['oembed_thumbnail'] = array(
'label' => t('oEmbed thumbnail'),
'default settings' => array(
'width' => '180',
'height' => '',
),
'view callback' => 'oembed_file_formatter_view',
'settings callback' => 'oembed_file_formatter_oembed_thumbnail_settings',
'description' => t('oEmbed thumbnail media.'),
);
return $formatters;
}
function oembed_file_formatter_info_alter(&$info) {
if (isset($info['file_image'])) {
$info['oembed_image'] = array(
'label' => t('oEmbed image'),
'view callback' => 'oembed_remote_file_formatter_view',
'description' => t('oEmbed photo or thumbnail is saved to local filesystem and transformed by image styles.'),
) + $info['file_image'];
}
}
function oembed_file_mimetype_mapping_alter(&$mapping) {
$mapping['mimetypes'][] = 'video/oembed';
$mapping['mimetypes'][] = 'image/oembed';
$mapping['mimetypes'][] = 'text/oembed';
$mapping['mimetypes'][] = 'audio/oembed';
}
function oembed_file_operation_info() {
$operations = array(
'refresh' => array(
'label' => t('Refresh from source'),
'callback' => 'oembed_cache_clear',
),
);
return $operations;
}
function oembed_file_presave(stdClass $file) {
if (empty($file->fid) && isset($file->oembed)) {
$embed = $file->oembed;
$file->filename = truncate_utf8(empty($embed['title']) ? oembed_alt_attr($embed) : $embed['title'], 255);
}
}
function oembed_file_load($files) {
foreach ($files as $file) {
$scheme = file_uri_scheme($file->uri);
if ($scheme == 'oembed') {
$wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
$file->oembed = oembed_get_data($wrapper
->getExternalUrl());
oembed_image_dimensions($file);
}
}
}
function oembed_file_insert($file) {
oembed_image_dimensions($file, TRUE);
}
function oembed_file_update($file) {
oembed_image_dimensions($file, TRUE);
}
function oembed_image_dimensions($file, $force = FALSE) {
$scheme = file_uri_scheme($file->uri);
if ($scheme != 'oembed') {
return;
}
if (strpos($file->filemime, 'image/') !== 0) {
return;
}
if (!$force && isset($file->image_dimensions)) {
return $file->image_dimensions;
}
$embed = $file->oembed;
if ($embed && $embed['type'] == 'photo' && !empty($embed['width']) && !empty($embed['height'])) {
$file->image_dimensions = array(
'width' => $embed['width'],
'height' => $embed['height'],
);
db_merge('image_dimensions')
->key(array(
'fid' => $file->fid,
))
->fields(array(
'width' => $file->image_dimensions['width'],
'height' => $file->image_dimensions['height'],
))
->execute();
}
else {
$file->image_dimensions = array(
'width' => NULL,
'height' => NULL,
);
}
}