- Patch #812688 by andypost, Damien Tournoud: organize image formatters around settings.
parent
0777093053
commit
619c35992e
|
@ -428,33 +428,74 @@ function image_field_formatter_info() {
|
|||
'image' => array(
|
||||
'label' => t('Image'),
|
||||
'field types' => array('image'),
|
||||
),
|
||||
'image_link_content' => array(
|
||||
'label' => t('Image linked to content'),
|
||||
'field types' => array('image'),
|
||||
),
|
||||
'image_link_file' => array(
|
||||
'label' => t('Image linked to file'),
|
||||
'field types' => array('image'),
|
||||
'settings' => array('image_style' => '', 'image_link' => ''),
|
||||
),
|
||||
);
|
||||
|
||||
foreach (image_styles() as $style) {
|
||||
$formatters['image__' . $style['name']] = array(
|
||||
'label' => t('Image "@style"', array('@style' => $style['name'])),
|
||||
'field types' => array('image'),
|
||||
);
|
||||
$formatters['image_link_content__' . $style['name']] = array(
|
||||
'label' => t('Image "@style" linked to content', array('@style' => $style['name'])),
|
||||
'field types' => array('image'),
|
||||
);
|
||||
$formatters['image_link_file__' . $style['name']] = array(
|
||||
'label' => t('Image "@style" linked to file', array('@style' => $style['name'])),
|
||||
'field types' => array('image'),
|
||||
);
|
||||
return $formatters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_formatter_settings_form().
|
||||
*/
|
||||
function image_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
|
||||
$display = $instance['display'][$view_mode];
|
||||
$settings = $display['settings'];
|
||||
|
||||
$image_styles = array('' => t('None (original image)')) + image_style_options(FALSE);
|
||||
$form['image_style'] = array(
|
||||
'#title' => t('Image style'),
|
||||
'#type' => 'select',
|
||||
'#default_value' => $settings['image_style'],
|
||||
'#options' => $image_styles,
|
||||
);
|
||||
|
||||
$link_types = array(
|
||||
'' => t('<none>'),
|
||||
'content' => t('Content'),
|
||||
'file' => t('File'),
|
||||
);
|
||||
$form['image_link'] = array(
|
||||
'#title' => t('Link image to'),
|
||||
'#type' => 'select',
|
||||
'#default_value' => $settings['image_link'],
|
||||
'#options' => $link_types,
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_formatter_settings_summary().
|
||||
*/
|
||||
function image_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['image_style']])) {
|
||||
$summary[] = t('Image style: @style', array('@style' => $image_styles[$settings['image_style']]));
|
||||
}
|
||||
else {
|
||||
$summary[] = t('Original image');
|
||||
}
|
||||
|
||||
return $formatters;
|
||||
$link_types = array(
|
||||
'content' => t('Linked to content'),
|
||||
'file' => t('Linked to file'),
|
||||
);
|
||||
// Display this setting only if image is linked.
|
||||
if (isset($link_types[$settings['image_link']])) {
|
||||
$summary[] = $link_types[$settings['image_link']];
|
||||
}
|
||||
|
||||
return implode('<br />', $summary);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -463,17 +504,11 @@ function image_field_formatter_info() {
|
|||
function image_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
|
||||
$element = array();
|
||||
|
||||
// Check if the formatter involves a particular image style.
|
||||
$matches = array();
|
||||
if (preg_match('/__([a-z0-9_-]+)/', $display['type'], $matches)) {
|
||||
$image_style = $matches[1];
|
||||
}
|
||||
|
||||
// Check if the formatter involves a link.
|
||||
if (strpos($display['type'], 'image_link_content') === 0) {
|
||||
if ($display['settings']['image_link'] == 'content') {
|
||||
$uri = entity_uri($entity_type, $entity);
|
||||
}
|
||||
elseif (strpos($display['type'], 'image_link_file') === 0) {
|
||||
elseif ($display['settings']['image_link'] == 'file') {
|
||||
$link_file = TRUE;
|
||||
}
|
||||
|
||||
|
@ -487,7 +522,7 @@ function image_field_formatter_view($entity_type, $entity, $field, $instance, $l
|
|||
$element[$delta] = array(
|
||||
'#theme' => 'image_formatter',
|
||||
'#item' => $item,
|
||||
'#image_style' => isset($image_style) ? $image_style : '',
|
||||
'#image_style' => $display['settings']['image_style'],
|
||||
'#path' => isset($uri) ? $uri : '',
|
||||
);
|
||||
}
|
||||
|
|
|
@ -365,14 +365,11 @@ function image_image_style_save($style) {
|
|||
$instance_changed = FALSE;
|
||||
foreach ($instance['display'] as $view_mode => $display) {
|
||||
// Check if the formatter involves an image style.
|
||||
$matches = array();
|
||||
if (preg_match('/__([a-z0-9_]+)/', $display['type'], $matches)) {
|
||||
if ($display['type'] == 'image' && $display['settings']['image_style'] == $style['old_name']) {
|
||||
// Update display information for any instance using the image
|
||||
// style that was just deleted.
|
||||
if ($style['old_name'] == $matches[1]) {
|
||||
$instance['display'][$view_mode]['type'] = str_replace($style['old_name'], $style['name'], $display['type']);
|
||||
$instance_changed = TRUE;
|
||||
}
|
||||
$instance['display'][$view_mode]['settings']['image_style'] = $style['name'];
|
||||
$instance_changed = TRUE;
|
||||
}
|
||||
}
|
||||
if ($instance['widget']['settings']['preview_image_style'] == $style['old_name']) {
|
||||
|
|
|
@ -592,7 +592,8 @@ class ImageAdminStylesUnitTest extends ImageFieldTestCase {
|
|||
// Create an image field that uses the new style.
|
||||
$field_name = strtolower($this->randomName(10));
|
||||
$instance = $this->createImageField($field_name, 'article');
|
||||
$instance['display']['default']['type'] = 'image__' . $style_name;
|
||||
$instance['display']['default']['type'] = 'image';
|
||||
$instance['display']['default']['settings']['image_style'] = $style_name;
|
||||
field_update_instance($instance);
|
||||
|
||||
// Create a new node with an image attached.
|
||||
|
@ -662,21 +663,23 @@ class ImageFieldDisplayTestCase extends ImageFieldTestCase {
|
|||
|
||||
// Test the image linked to file formatter.
|
||||
$instance = field_info_instance('node', $field_name, 'article');
|
||||
$instance['display']['default']['type'] = 'image_link_file';
|
||||
$instance['display']['default']['type'] = 'image';
|
||||
$instance['display']['default']['settings']['image_link'] = 'file';
|
||||
field_update_instance($instance);
|
||||
$default_output = l(theme('image', $image_info), file_create_url($image_uri), array('html' => TRUE));
|
||||
$this->drupalGet('node/' . $nid);
|
||||
$this->assertRaw($default_output, t('Image linked to file formatter displaying correctly on full node view.'));
|
||||
|
||||
// Test the image linked to content formatter.
|
||||
$instance['display']['default']['type'] = 'image_link_content';
|
||||
$instance['display']['default']['settings']['image_link'] = 'content';
|
||||
field_update_instance($instance);
|
||||
$default_output = l(theme('image', $image_info), 'node/' . $nid, array('html' => TRUE, 'attributes' => array('class' => 'active')));
|
||||
$this->drupalGet('node/' . $nid);
|
||||
$this->assertRaw($default_output, t('Image linked to content formatter displaying correctly on full node view.'));
|
||||
|
||||
// Test the image style 'thumbnail' formatter.
|
||||
$instance['display']['default']['type'] = 'image__thumbnail';
|
||||
$instance['display']['default']['settings']['image_link'] = '';
|
||||
$instance['display']['default']['settings']['image_style'] = 'thumbnail';
|
||||
field_update_instance($instance);
|
||||
// Ensure the derrivative image is generated so we do not have to deal with
|
||||
// image style callback paths.
|
||||
|
|
|
@ -384,12 +384,14 @@ function standard_install() {
|
|||
'display' => array(
|
||||
'default' => array(
|
||||
'label' => 'hidden',
|
||||
'type' => 'image__large',
|
||||
'type' => 'image',
|
||||
'settings' => array('image_style' => 'large', 'image_link' => ''),
|
||||
'weight' => -1,
|
||||
),
|
||||
'teaser' => array(
|
||||
'label' => 'hidden',
|
||||
'type' => 'image_link_content__medium',
|
||||
'type' => 'image',
|
||||
'settings' => array('image_style' => 'medium', 'image_link' => 'content'),
|
||||
'weight' => -1,
|
||||
),
|
||||
),
|
||||
|
|
Loading…
Reference in New Issue