View source
<?php
function oembed_file_formatter_info() {
$formatters['oembed'] = array(
'label' => t('oEmbed'),
'file types' => array(
'audio, document, image, video',
),
'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.'),
'mime types' => array(
'audio/oembed',
'image/oembed',
'text/oembed',
'video/oembed',
),
);
$formatters['oembed_thumbnail'] = array(
'label' => t('oEmbed thumbnail'),
'file types' => array(
'audio, document, image, video',
),
'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.'),
'mime types' => array(
'audio/oembed',
'image/oembed',
'text/oembed',
'video/oembed',
),
);
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.'),
'hidden' => FALSE,
'mime types' => array(
'audio/oembed',
'image/oembed',
'text/oembed',
'video/oembed',
),
) + $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($file) {
if (empty($file->fid) && isset($file->metadata['oembed'])) {
$embed = $file->metadata['oembed'];
$file->filename = truncate_utf8(empty($embed['title']) ? oembed_alt_attr($embed) : $embed['title'], 255);
}
oembed_metadata_fetch_image_dimensions($file);
}
function oembed_file_load($files) {
foreach ($files as $file) {
$scheme = file_uri_scheme($file->uri);
if ($scheme == 'oembed' && !isset($file->metadata['oembed'])) {
$wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
$file->metadata['oembed'] = oembed_get_data($wrapper
->getExternalUrl());
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();
}
}
}
}
}
function oembed_metadata_fetch_image_dimensions($file) {
$scheme = file_uri_scheme($file->uri);
if ($scheme != 'oembed') {
return;
}
if (file_entity_file_get_mimetype_type($file) != 'image') {
return;
}
$embed = $file->metadata['oembed'];
if ($embed && $embed['type'] == 'photo' && !empty($embed['width']) && !empty($embed['height'])) {
$file->metadata['width'] = $embed['width'];
$file->metadata['height'] = $embed['height'];
}
else {
$file->metadata['width'] = NULL;
$file->metadata['height'] = NULL;
}
}
function oembed_file_metadata_info() {
$info['oembed'] = array(
'label' => t('oEmbed'),
'type' => 'array',
);
return $info;
}