Commit 03ac1077 authored by frjo's avatar frjo
Browse files

#982626: Path by frjo to use formatters setting instead of new formatter for...

#982626: Path by frjo to use formatters setting instead of new formatter for every Image style. Suggested by vito_swat.
parent 569ee742
......@@ -20,13 +20,10 @@ function colorbox_theme() {
return array(
'colorbox_imagefield' => array(
'variables' => array(
'style_name' => NULL,
'image' => array(),
'path' => NULL,
'alt' => NULL,
'title' => NULL,
'gid' => NULL,
'attributes' => array(),
'getsize' => TRUE,
),
'file' => 'colorbox.theme.inc',
),
......@@ -45,7 +42,8 @@ function colorbox_theme() {
'item' => NULL,
'node' => NULL,
'field' => array(),
'image_style' => NULL,
'colorbox_node_style' => NULL,
'colorbox_image_style' => NULL,
),
'file' => 'colorbox.theme.inc',
),
......@@ -335,35 +333,83 @@ function colorbox_link_alter(&$links, $node) {
/**
* Implementation of hook_field_formatter_info().
*
* Adds certain colorbox+imagecache formatters to CCK image fields if imagefield.module and imagecache.module exist.
*/
function colorbox_field_formatter_info() {
$formatters = array();
if (module_exists('image')) {
foreach (image_styles() as $style) {
$formatters['colorbox__' . $style['name']] = array(
'label' => t('Colorbox: @preset image', array('@preset' => $style['name'])),
'field types' => array('image', 'file'),
);
}
$formatters = array(
'colorbox' => array(
'label' => t('Colorbox'),
'field types' => array('image'),
'settings' => array('colorbox_node_style' => '', 'colorbox_image_style' => ''),
),
);
}
return $formatters;
}
/**
* Implementation of hook_field_formatter_settings_form().
*/
function colorbox_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$image_styles = image_style_options(FALSE);
$element['colorbox_node_style'] = array(
'#title' => t('Node image style'),
'#type' => 'select',
'#default_value' => $settings['colorbox_node_style'],
'#empty_option' => t('None (original image)'),
'#options' => $image_styles,
);
$element['colorbox_image_style'] = array(
'#title' => t('Colorbox image style'),
'#type' => 'select',
'#default_value' => $settings['colorbox_image_style'],
'#empty_option' => t('None (original image)'),
'#options' => $image_styles,
);
return $element;
}
/**
* Implements hook_field_formatter_view().
* Implementation of hook_field_formatter_settings_summary().
*/
function colorbox_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
function colorbox_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = array();
$image_styles = image_style_options(FALSE);
// Unset possible 'No defined styles' option.
unset($image_styles['']);
// Styles could be lost because of enabled/disabled modules that defines
// their styles in code.
if (isset($image_styles[$settings['colorbox_node_style']])) {
$summary[] = t('Node image style: @style', array('@style' => $image_styles[$settings['colorbox_node_style']]));
}
else {
$summary[] = t('Node image style: Original image');
}
// Check if the formatter involves a particular image style.
$matches = array();
if (preg_match('/__([a-z0-9_]+)/', $display['type'], $matches)) {
$image_style = $matches[1];
if (isset($image_styles[$settings['colorbox_image_style']])) {
$summary[] = t('Colorbox image style: @style', array('@style' => $image_styles[$settings['colorbox_image_style']]));
}
else {
$summary[] = t('Colorbox image style: Original image');
}
return implode('<br />', $summary);
}
/**
* Implementation of hook_field_formatter_view().
*/
function colorbox_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
foreach ($items as $delta => $item) {
$element[$delta] = array(
......@@ -371,7 +417,8 @@ function colorbox_field_formatter_view($entity_type, $entity, $field, $instance,
'#item' => $item,
'#node' => $entity,
'#field' => $field,
'#image_style' => isset($image_style) ? $image_style : '',
'#colorbox_node_style' => $display['settings']['colorbox_node_style'],
'#colorbox_image_style' => $display['settings']['colorbox_image_style'],
);
}
......
......@@ -27,11 +27,11 @@ function theme_colorbox_image_formatter($variables) {
'path' => $item['uri'],
'alt' => $item['alt'],
'title' => $item['title'],
'style_name' => $variables['image_style'],
'style_name' => $variables['colorbox_node_style'],
);
// If the title is empty use alt or the node title in that order.
if (empty($item['title'])) {
if (empty($image['title'])) {
if (!empty($image['alt'])) {
$image['title'] = $image['alt'];
}
......@@ -41,9 +41,9 @@ function theme_colorbox_image_formatter($variables) {
}
// Shorten the title for the example styles or when title shortening is active.
$style = variable_get('colorbox_style', 'default');
$colorbox_style = variable_get('colorbox_style', 'default');
$trim_length = variable_get('colorbox_title_trim_length', 75);
if (((strpos($style, 'colorbox/example') !== FALSE) || variable_get('colorbox_title_trim', 0)) && (drupal_strlen($item['title']) > $trim_length)) {
if (((strpos($colorbox_style, 'colorbox/example') !== FALSE) || variable_get('colorbox_title_trim', 0)) && (drupal_strlen($item['title']) > $trim_length)) {
$image['title'] = drupal_substr($item['title'], 0, $trim_length - 5) . '...';
}
......@@ -64,7 +64,14 @@ function theme_colorbox_image_formatter($variables) {
break;
}
return theme('colorbox_imagefield', array('style_name' => $image['style_name'], 'path' => $image['path'], 'alt' => $image['alt'], 'title' => $image['title'], 'gid' => $gallery_id));
if ($style_name = $variables['colorbox_image_style']) {
$path = image_style_url($style_name, $image['path']);
}
else {
$path = file_create_url($image['path']);
}
return theme('colorbox_imagefield', array('image' => $image, 'path' => $path, 'title' => $image['title'], 'gid' => $gallery_id));
}
/**
......@@ -87,16 +94,18 @@ function theme_colorbox_image_formatter($variables) {
* @ingroup themeable
*/
function theme_colorbox_imagefield($variables) {
$image = theme('image_style', $variables);
if ($style_name = variable_get('colorbox_imagecache_preset', 0)) {
$link_path = image_style_url($style_name, $variables['path']);
}
else {
$link_path = file_create_url($variables['path']);
}
$class = 'colorbox imagefield imagefield-imagelink';
$image = theme('image_style', $variables['image']);
$options = array(
'html' => TRUE,
'attributes' => array(
'title' => $variables['title'],
'class' => 'colorbox imagefield imagefield-imagelink',
'rel' => 'gallery-' . $variables['gid']
)
);
$output = l($image, $variables['path'], $options);
return l($image, $link_path, array('html' => TRUE, 'attributes' => array('title' => $variables['title'], 'class' => $class, 'rel' => 'gallery-' . $variables['gid'])));
return $output;
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment