function picture_field_formatter_view in Picture 7
Same name and namespace in other branches
- 7.2 picture.module \picture_field_formatter_view()
Implements hook_field_formatter_view().
File
- ./
picture.module, line 648 - Picture formatter.
Code
function picture_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
// Check if the formatter involves a link.
$image_link = $display['settings']['image_link'];
if ($image_link == 'content') {
$uri = entity_uri($entity_type, $entity);
}
elseif ($image_link == 'file') {
$link_file = TRUE;
}
elseif ($image_link) {
if (isset($entity->{$image_link})) {
// Support for field translations.
$language = field_language($entity_type, $entity, $field['field_name']);
$link_field = $entity->{$image_link};
if (isset($link_field[$language])) {
$link_values = $link_field[$language];
}
}
}
$fallback_image_style = '';
$mappings = picture_mapping_load($display['settings']['picture_group']);
$breakpoint_styles = picture_get_mapping_breakpoints($mappings, $fallback_image_style);
if (isset($display['settings']['fallback_image_style']) && !empty($display['settings']['fallback_image_style'])) {
$fallback_image_style = $display['settings']['fallback_image_style'];
}
// Assume regular display.
$formatter = 'picture_formatter';
$colorbox_breakpoints = array();
$colorbox_fallback_image_style = '';
// Check for colorbox link.
if (module_exists('colorbox') && $display['settings']['image_link'] == 'colorbox') {
$formatter = 'picture_formatter_colorbox';
$mappings = picture_mapping_load($display['settings']['colorbox_settings']['colorbox_group']);
$colorbox_breakpoints = picture_get_mapping_breakpoints($mappings, $colorbox_fallback_image_style);
}
foreach ($items as $delta => $item) {
if (isset($link_file)) {
$uri = array(
'path' => file_create_url($item['uri']),
'options' => array(),
);
}
// Handle multiple link with image values.
if (isset($link_values)) {
if (isset($link_values[$delta]['url'])) {
$uri = array(
'path' => $link_values[$delta]['url'],
'options' => array(
'attributes' => $link_values[$delta]['attributes'],
),
);
// Handle query fragment if there is any.
if (!empty($link_values[$delta]['query'])) {
$uri['options']['query'] = $link_values[$delta]['query'];
}
}
else {
unset($uri);
}
}
$element[$delta] = array(
'#theme' => $formatter,
'#attached' => array(
'library' => array(
array(
'picture',
'matchmedia',
),
array(
'picture',
'picturefill',
),
array(
'picture',
'picture.ajax',
),
),
),
'#item' => $item,
'#image_style' => $fallback_image_style,
'#breakpoints' => $breakpoint_styles,
'#path' => isset($uri) ? $uri : '',
'#colorbox_group' => $colorbox_breakpoints,
'#colorbox_image_style' => $colorbox_fallback_image_style,
);
// Add css and js for colorbox.
if ($formatter == 'picture_formatter_colorbox') {
$element[$delta]['#attached']['css'][drupal_get_path('module', 'picture') . '/picture_colorbox.css'] = array(
'type' => 'file',
);
$element[$delta]['#attached']['js'][drupal_get_path('module', 'picture') . '/picture_colorbox.js'] = array(
'type' => 'file',
);
if (!variable_get('colorbox_inline', 0)) {
$element[$delta]['#attached']['js'][drupal_get_path('module', 'colorbox') . '/js/colorbox_inline.js'] = array(
'type' => 'file',
);
}
$colorbox_settings = $display['settings']['colorbox_settings'];
// Add the group ID.
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
$entity_id = !empty($id) ? $entity_type . '-' . $id : 'entity-id';
// If this is a file entity field check for the referencing entity to do
// the proper grouping.
if (isset($entity->referencing_entity) && isset($entity->referencing_field)) {
// Because we don't have the entity type we use some "magic" to get a
// unique entity identifier.
$entity_id = spl_object_hash($entity->referencing_entity);
$colorbox_group_field = $entity->referencing_field;
}
else {
$colorbox_group_field = $field['field_name'];
}
switch ($colorbox_settings['colorbox_gallery']) {
case 'post':
$gallery_id = 'gallery-' . $entity_id;
break;
case 'page':
$gallery_id = 'gallery-all';
break;
case 'field_post':
$gallery_id = 'gallery-' . $entity_id . '-' . $colorbox_group_field;
break;
case 'field_page':
$gallery_id = 'gallery-' . $colorbox_group_field;
break;
case 'custom':
$gallery_id = $colorbox_settings['colorbox_gallery_custom'];
break;
default:
$gallery_id = '';
}
$element[$delta]['#colorbox_group_id'] = $gallery_id;
// Add the caption.
$entity_title = entity_label($entity_type, $entity);
switch ($colorbox_settings['colorbox_caption']) {
case 'auto':
// If the title is empty use alt or the entity title in that order.
if (!empty($item['title'])) {
$caption = $item['title'];
}
elseif (!empty($item['alt'])) {
$caption = $item['alt'];
}
elseif (!empty($entity_title)) {
$caption = $entity_title;
}
else {
$caption = '';
}
break;
case 'title':
$caption = $item['title'];
break;
case 'alt':
$caption = $item['alt'];
break;
case 'node_title':
$caption = $entity_title;
break;
case 'custom':
$caption = token_replace($colorbox_settings['colorbox_caption_custom'], array(
$entity_type => $entity,
'file' => (object) $item,
), array(
'clear' => TRUE,
));
break;
default:
$caption = '';
}
// Shorten the caption for the example styles or when caption shortening
// is active.
$colorbox_style = variable_get('colorbox_style', 'default');
$trim_length = variable_get('colorbox_caption_trim_length', 75);
if ((strpos($colorbox_style, 'colorbox/example') !== FALSE || variable_get('colorbox_caption_trim', 0)) && drupal_strlen($caption) > $trim_length) {
$caption = drupal_substr($caption, 0, $trim_length - 5) . '...';
}
$element[$delta]['#colorbox_caption'] = $caption;
}
}
return $element;
}