Issue #1907674 by DYdave: Added support to fragment query string for links path wrapp...
[project/image_link_formatter.git] / image_link_formatter.module
1 <?php
2 /**
3 * @file
4 * This file is essentially a copy/paste from the core image module:
5 * image.module.
6 * Applied the patch and few changes suggested at:
7 * http://drupal.org/node/1570072
8 */
9
10 /**
11 * Implements hook_field_formatter_info().
12 */
13 function image_link_formatter_field_formatter_info() {
14 $formatters = array(
15 'image_link_formatter' => array(
16 'label' => t('Image Link Formatter'),
17 'field types' => array('image'),
18 'settings' => array('image_style' => '', 'image_link' => ''),
19 ),
20 );
21
22 return $formatters;
23 }
24
25 /**
26 * Implements hook_field_formatter_settings_form().
27 */
28 function image_link_formatter_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
29 $display = $instance['display'][$view_mode];
30 $settings = $display['settings'];
31
32 $image_styles = image_style_options(FALSE);
33 $element['image_style'] = array(
34 '#title' => t('Image style'),
35 '#type' => 'select',
36 '#default_value' => $settings['image_style'],
37 '#empty_option' => t('None (original image)'),
38 '#options' => $image_styles,
39 );
40
41 $link_types = image_link_formatter_image_field_link_types($instance);
42 $element['image_link'] = array(
43 '#title' => t('Link image to'),
44 '#type' => 'select',
45 '#default_value' => $settings['image_link'],
46 '#empty_option' => t('Nothing'),
47 '#options' => $link_types,
48 );
49
50 return $element;
51 }
52
53 /**
54 * Implements hook_field_formatter_settings_summary().
55 */
56 function image_link_formatter_field_formatter_settings_summary($field, $instance, $view_mode) {
57 $display = $instance['display'][$view_mode];
58 $settings = $display['settings'];
59
60 $summary = array();
61
62 $image_styles = image_style_options(FALSE);
63 // Unset possible 'No defined styles' option.
64 unset($image_styles['']);
65 // Styles could be lost because of enabled/disabled modules that defines
66 // their styles in code.
67 if (isset($image_styles[$settings['image_style']])) {
68 $summary[] = t('Image style: @style', array('@style' => $image_styles[$settings['image_style']]));
69 }
70 else {
71 $summary[] = t('Original image');
72 }
73
74 $link_types = image_link_formatter_image_field_link_types($instance);
75 // Display this setting only if image is linked.
76 if (isset($link_types[$settings['image_link']])) {
77 $summary[] = filter_xss_admin($link_types[$settings['image_link']]);
78 }
79
80 return implode('<br />', $summary);
81 }
82
83 /**
84 * Implements hook_field_formatter_view().
85 */
86 function image_link_formatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
87 $element = array();
88
89 // Check if the formatter involves a link.
90 $image_link = $display['settings']['image_link'];
91 if ($image_link == 'content') {
92 $uri = entity_uri($entity_type, $entity);
93 }
94 elseif ($image_link == 'file') {
95 $link_file = TRUE;
96 }
97 elseif ($image_link) {
98 if (isset($entity->$image_link)) {
99 // Support for field translations.
100 $language = field_language($entity_type, $entity, $field['field_name']);
101 $link_field = $entity->$image_link;
102
103 if (isset($link_field[$language])) {
104 $link_values = $link_field[$language];
105 }
106 }
107 }
108
109 foreach ($items as $delta => $item) {
110 if (isset($link_file)) {
111 $uri = array(
112 'path' => file_create_url($item['uri']),
113 'options' => array(),
114 );
115 }
116 // Handle multiple link with image values.
117 if (isset($link_values)) {
118 if (isset($link_values[$delta]['url'])) {
119 $uri = array(
120 'path' => $link_values[$delta]['url'],
121 'options' => array('attributes' => $link_values[$delta]['attributes']),
122 );
123 // Handle query fragment if there is any.
124 if (isset($link_values[$delta]['query']) && count($link_values[$delta]['query'])>0) {
125 $uri['options']['query'] = $link_values[$delta]['query'];
126 }
127 }
128 // If there are more image values than link values unset the link.
129 else {
130 unset($uri);
131 }
132 }
133 $element[$delta] = array(
134 '#theme' => 'image_formatter',
135 '#item' => $item,
136 '#image_style' => $display['settings']['image_style'],
137 '#path' => isset($uri) ? $uri : '',
138 );
139 }
140
141 return $element;
142 }
143
144 /**
145 * Helper function to compute the list of possible link types.
146 */
147 function image_link_formatter_image_field_link_types($instance) {
148 $link_types = array(
149 'content' => t('Content'),
150 'file' => t('File'),
151 );
152 // If the link module is installed, also allow any link fields to be used.
153 foreach (field_info_fields() as $field_key => $field_info) {
154 if ($field_info['type'] == 'link_field') {
155 $field_instance = field_info_instance($instance['entity_type'], $field_info['field_name'], $instance['bundle']);
156 if ($field_instance) {
157 $link_types[$field_key] = "$field_instance[label] ($field_info[field_name])";
158 }
159 }
160 }
161 return $link_types;
162 }