Created
April 27, 2013 15:52
-
-
Save karelbemelmans/5473574 to your computer and use it in GitHub Desktop.
Add a "Link to Link field" option to image fields.
Requires the field_formatter_settings module.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('FEATURE_SITE_LINK_FIELD', 'link_field'); | |
/** | |
* Implements hook_field_formatter_info_alter(). | |
*/ | |
function feature_site_field_formatter_info_alter(&$info){ | |
$info['image']['settings'] += array('image_link_field' => ''); | |
} | |
/** | |
* Implements hook_field_formatter_settings_form_alter(). | |
*/ | |
function feature_site_field_formatter_settings_form_alter(&$settings_form, &$context){ | |
$display = $context['instance']['display'][$context['view_mode']]; | |
$settings = $display['settings']; | |
// Only build this information for image displays | |
if ($display['type'] !== 'image') { | |
return NULL; | |
} | |
$settings_form['image_link']['#options'][FEATURE_SITE_LINK_FIELD] = 'Link field'; | |
$field_name = $context['field']['field_name']; | |
$entity_type = $context['instance']['entity_type']; | |
$bundle = $context['instance']['bundle']; | |
$instances = field_info_instances($entity_type, $bundle); | |
$link_fields = array(); | |
foreach($instances as $key => $instance) { | |
if ($instance['widget']['module'] == 'link') { | |
$link_fields[$key] = $instance['label']; | |
} | |
} | |
$settings_form['image_link_field'] = array( | |
'#type' => 'select', | |
'#title' => t('Link field'), | |
'#options' => $link_fields, | |
'#states' => array( | |
'visible' => array('select[name="fields[' . $field_name . '][settings_edit_form][settings][image_link]"]' => array('value' => FEATURE_SITE_LINK_FIELD)), | |
), | |
); | |
} | |
/** | |
* Implements hook_field_formatter_settings_summary_alter(). | |
*/ | |
function feature_site_field_formatter_settings_summary_alter(&$summary, array $context){ | |
$display = $context['instance']['display'][$context['view_mode']]; | |
$settings = $display['settings']; | |
// Only build this information for image displays | |
if($display['type'] !== 'image') { | |
return NULL; | |
} | |
if(!empty($summary)) { | |
$summary .= '<br />'; | |
} | |
if(!empty($settings['image_link']) && !empty($settings['image_link_field'])) { | |
if($settings['image_link'] == FEATURE_SITE_LINK_FIELD) { | |
$summary .= t('Linked to field: <em>%field</em>', array('%field' => $settings['image_link_field'])); | |
} | |
} | |
} | |
/** | |
* Implements hook_field_attach_view_alter(). | |
*/ | |
function feature_site_field_attach_view_alter(&$output, $context){ | |
foreach(element_children($output) as $field_name) { | |
$element = $output[$field_name]; | |
$view_mode = $context['view_mode']; | |
// Only work on image fields with the iamge formatter | |
if($element['#field_type'] == 'image' && $element['#formatter'] == 'image') { | |
$type = FALSE; | |
$field_info = field_info_instance($element['#entity_type'], $field_name, $element['#bundle']); | |
if(isset($field_info['display'][$view_mode])) { | |
$formatter_settings = $field_info['display'][$view_mode]['settings']; | |
if($formatter_settings['image_link'] != FEATURE_SITE_LINK_FIELD) { | |
return; | |
} | |
$link_field_name = $formatter_settings['image_link_field']; | |
if(!$link_field_name) { | |
return; | |
} | |
$field_items = field_get_items($element['#entity_type'], $context['entity'], $link_field_name); | |
$uri = $field_items[0]['url']; | |
$attributes = $field_items[0]['attributes']; | |
foreach(element_children($element) as $key) { | |
$output[$field_name][$key]['#path']['path'] = $uri; | |
$output[$field_name][$key]['#path']['options'] = array('attributes' => $attributes); | |
}; | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment