function magnific_popup_field_formatter_prepare_view in Magnific Popup 7
Implements hook_field_formatter_prepare_view().
File
- includes/
magnific_popup.formatters.inc, line 98 - Formatters for Magnific Popup.
Code
function magnific_popup_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
// Items are some kind of entity reference, be it via image or file type.
// Load the corresponding items into a common format for our formatter_view().
$static =& drupal_static(__FUNCTION__, array(
'loaded_items' => array(),
'thumbnail_schemes' => NULL,
));
$loaded_items =& $static['loaded_items'];
$thumbnail_schemes =& $static['thumbnail_schemes'];
if (!isset($thumbnail_schemes)) {
$thumbnail_schemes = module_invoke_all('magnific_popup_thumbnail_schemes');
}
// Iterate through $items looking for valid gallery items. Refactor them into
// a consistent format for magnific_popup_field_formatter_view().
$wrappers = array();
foreach ($entities as $entity_id => $entity) {
foreach ($items[$entity_id] as $delta => &$item) {
// Fail-safe check to not load items.
if (empty($item['uri'])) {
continue;
}
// Check if we've already parsed this fid, and build it if not.
if (!isset($loaded_items[$item['fid']])) {
$loaded_items[$item['fid']] = FALSE;
$item_scheme = file_uri_scheme($item['uri']);
// Attempt to re-use, or load from scratch, a wrapper for this scheme.
if (empty($wrappers[$item_scheme])) {
// Create a new wrapper.
$wrappers[$item_scheme] = file_stream_wrapper_get_instance_by_uri($item['uri']);
}
else {
// Attempt to re-use an existing wrapper.
$wrappers[$item_scheme]
->setUri($item['uri']);
}
// Check that the item itself is accessible.
if ($wrappers[$item_scheme] && $wrappers[$item_scheme]
->url_stat($item['uri'], STREAM_URL_STAT_QUIET) !== FALSE) {
$item_wrapper = $wrappers[$item_scheme];
$classes = array();
$thumbnail_path = '';
$thumbnail_style = '';
// We'll need to determine if the thumbnail path is an external URL.
$is_external_url = FALSE;
// Check for display settings.
if (!empty($displays[$entity_id]) && !empty($displays[$entity_id]['settings'])) {
$current_display = $displays[$entity_id]['settings'];
$thumbnail_field_name = !empty($current_display['thumbnail_field']) ? $current_display['thumbnail_field'] : '';
// Check for thumbnail overrides (where a different field is used
// to derive thumbnails).
$valid_override_thumbnail = !empty($thumbnail_field_name) && $thumbnail_field_name !== $field['field_name'];
if ($valid_override_thumbnail && !empty($entities[$entity_id])) {
$thumbnail_field_item = field_get_items($entity_type, $entities[$entity_id], $thumbnail_field_name, NULL);
// Only override the thumbnail if we have an actual thumbnail to
// use instead (the field override may be empty!).
if (!empty($thumbnail_field_item[$delta]['uri'])) {
$thumbnail_path = $thumbnail_field_item[$delta]['uri'];
}
}
$thumbnail_style = !empty($current_display['thumbnail_style']) ? $current_display['thumbnail_style'] : '';
$classes[] = 'mfp-custom-thumbnail';
}
// If we don't have an override thumbnail_path, pull it from the item
// itself.
if (empty($thumbnail_path)) {
// MediaYouTubeStreamWrapper implements getLocalThumbnailPath() to
// get the thumbnail path. Otherwise use getUri().
if (method_exists($item_wrapper, 'getLocalThumbnailPath')) {
$thumbnail_path = $item_wrapper
->getLocalThumbnailPath();
}
else {
$thumbnail_path = $item_wrapper
->getUri();
}
}
// Get oEmbed data for the item.
$oembed_data = NULL;
if ($item_scheme === 'oembed' && function_exists('oembed_get_data')) {
// Get oembed data.
$oembed_data = oembed_get_data($item_wrapper
->getExternalUrl());
if (!empty($oembed_data) && !empty($oembed_data['html'])) {
$item['oembed_html'] = $oembed_data['html'];
}
}
// Handle the determined thumbnail path (could be external, oembed,
// etc.).
if (!empty($thumbnail_path)) {
$thumbnail_scheme = file_uri_scheme($thumbnail_path);
if ($thumbnail_scheme === 'oembed') {
// Check if the thumbnail is the same as the item, to avoid
// redundant oembed calls.
if ($thumbnail_path !== $item['uri'] && function_exists('oembed_get_data')) {
if ($wrapper = file_stream_wrapper_get_instance_by_uri($thumbnail_path)) {
$oembed_data = oembed_get_data($wrapper
->getExternalUrl());
}
}
// Use oembed data.
if (!empty($oembed_data) && !empty($oembed_data['thumbnail_url'])) {
$is_external_url = TRUE;
$thumbnail_path = '//' . file_uri_target($oembed_data['thumbnail_url']);
if (empty($item['title']) && !empty($oembed_data['title'])) {
$item['title'] = $oembed_data['title'];
}
}
}
}
// Default $thumbnail_path based on the $item_scheme.
if (empty($thumbnail_style)) {
$thumbnail_style = 'magnific_popup_thumbnail';
if (!empty($thumbnail_schemes[$item_scheme])) {
$thumbnail_style = $thumbnail_schemes[$item_scheme];
}
}
// Do not request image style URLs for externally-derived thumbs.
if (!$is_external_url) {
$thumbnail_path = image_style_url($thumbnail_style, $thumbnail_path);
}
// Get the title if it exists, or fallback to the filename.
$title = !empty($item['title']) ? $item['title'] : $item['filename'];
// Get the alt if it exists, or fallback to the title.
$alt = !empty($item['alt']) ? $item['alt'] : $title;
// Build the finished gallery item.
$gallery_item = array(
'title' => $title,
'alt' => $alt,
'thumbnail_path' => $thumbnail_path,
'target_uri' => $item_wrapper
->getExternalUrl(),
'classes' => $classes,
) + $item;
$loaded_items[$item['fid']] = $gallery_item;
}
}
// Replace $item with the parsed version of info for this fid.
$item = $loaded_items[$item['fid']];
// Check if this item was unloadable and if so remove it from the list.
if (empty($item)) {
unset($items[$entity_id][$delta]);
}
}
// Remove the pointer.
if (isset($item)) {
unset($item);
}
}
}