image_url_formatter.module 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * @file
  4. * Add an URL formatter for image field
  5. */
  6. /**
  7. * Define constants for determine which type of URL should be used.
  8. */
  9. define('IMAGE_URL_FORMATTER_URI_PATH', '3');
  10. define('IMAGE_URL_FORMATTER_RELATIVE_PATH', '2');
  11. define('IMAGE_URL_FORMATTER_ABSOLUTE_PATH', '1');
  12. define('IMAGE_URL_FORMATTER_FULL_URL', '0');
  13. /**
  14. * Implements hook_theme().
  15. */
  16. function image_url_formatter_theme() {
  17. return array(
  18. 'image_url_formatter' => array(
  19. 'variables' => array(
  20. 'item' => NULL,
  21. 'path' => NULL,
  22. 'image_style' => NULL,
  23. 'url_type' => NULL,
  24. ),
  25. ),
  26. );
  27. }
  28. /**
  29. * Implements hook_field_formatter_info().
  30. */
  31. function image_url_formatter_field_formatter_info() {
  32. $formatters = array(
  33. 'image_url' => array(
  34. 'label' => t('Image URL'),
  35. 'field types' => array('image', 'imagefield_crop'),
  36. 'settings' => array(
  37. 'url_type' => '',
  38. 'image_style' => '',
  39. 'image_link' => '',
  40. ),
  41. ),
  42. );
  43. return $formatters;
  44. }
  45. /**
  46. * Implements hook_field_formatter_settings_form().
  47. */
  48. function image_url_formatter_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  49. $display = $instance['display'][$view_mode];
  50. $settings = $display['settings'];
  51. $element['url_type'] = array(
  52. '#title' => t('URL type'),
  53. '#type' => 'radios',
  54. '#options' => array(
  55. IMAGE_URL_FORMATTER_URI_PATH => t('URI path'),
  56. IMAGE_URL_FORMATTER_RELATIVE_PATH => t('Relative file path'),
  57. IMAGE_URL_FORMATTER_ABSOLUTE_PATH => t('Absolute file path (recommended)'),
  58. IMAGE_URL_FORMATTER_FULL_URL => t('Full URL'),
  59. ),
  60. '#default_value' => $settings['url_type'],
  61. );
  62. $element['url_type'][IMAGE_URL_FORMATTER_URI_PATH]['#description'] = t("Uses the URI path, like: 'public://image.png'");
  63. $element['url_type'][IMAGE_URL_FORMATTER_RELATIVE_PATH]['#description'] = t("No base URL or leading slash, like: 'sites/default/files/image.png'");
  64. $element['url_type'][IMAGE_URL_FORMATTER_ABSOLUTE_PATH]['#description'] = t("With leading slash, no base URL, like: '/sites/default/files/image.png'");
  65. $element['url_type'][IMAGE_URL_FORMATTER_FULL_URL]['#description'] = t("Like: 'http://example.com/sites/default/files/image.png'");
  66. $image_styles = image_style_options(FALSE);
  67. $element['image_style'] = array(
  68. '#title' => t('Image style'),
  69. '#type' => 'select',
  70. '#default_value' => $settings['image_style'],
  71. '#empty_option' => t('None (original image)'),
  72. '#options' => $image_styles,
  73. );
  74. $link_types = array(
  75. 'content' => t('Content'),
  76. 'file' => t('File'),
  77. );
  78. $element['image_link'] = array(
  79. '#title' => t('Link image url to'),
  80. '#type' => 'select',
  81. '#default_value' => $settings['image_link'],
  82. '#empty_option' => t('Nothing'),
  83. '#options' => $link_types,
  84. );
  85. return $element;
  86. }
  87. /**
  88. * Implements hook_field_formatter_settings_summary().
  89. */
  90. function image_url_formatter_field_formatter_settings_summary($field, $instance, $view_mode) {
  91. $display = $instance['display'][$view_mode];
  92. $settings = $display['settings'];
  93. $summary = array();
  94. switch ($settings['url_type']) {
  95. case IMAGE_URL_FORMATTER_URI_PATH:
  96. $summary[] = t('Use uri path');
  97. break;
  98. case IMAGE_URL_FORMATTER_RELATIVE_PATH:
  99. $summary[] = t('Use relative path');
  100. break;
  101. case IMAGE_URL_FORMATTER_ABSOLUTE_PATH:
  102. $summary[] = t('Use absolute path');
  103. break;
  104. case IMAGE_URL_FORMATTER_FULL_URL:
  105. $summary[] = t('Use full URL');
  106. break;
  107. }
  108. $image_styles = image_style_options(FALSE);
  109. // Unset possible 'No defined styles' option.
  110. unset($image_styles['']);
  111. // Styles could be lost because of enabled/disabled modules that defines
  112. // their styles in code.
  113. if (isset($image_styles[$settings['image_style']])) {
  114. $summary[] = t('URL for image style: @style', array('@style' => $image_styles[$settings['image_style']]));
  115. }
  116. else {
  117. $summary[] = t('Original image URL');
  118. }
  119. $link_types = array(
  120. 'content' => t('Linked to content'),
  121. 'file' => t('Linked to file'),
  122. );
  123. // Display this setting only if image is linked.
  124. if (isset($link_types[$settings['image_link']])) {
  125. $summary[] = $link_types[$settings['image_link']];
  126. }
  127. return implode('<br />', $summary);
  128. }
  129. /**
  130. * Implements hook_field_formatter_view().
  131. */
  132. function image_url_formatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  133. $element = array();
  134. switch ($display['type']) {
  135. case 'image_url':
  136. // Check if the formatter involves a link.
  137. if ($display['settings']['image_link'] == 'content') {
  138. $uri = entity_uri($entity_type, $entity);
  139. }
  140. elseif ($display['settings']['image_link'] == 'file') {
  141. $link_file = TRUE;
  142. }
  143. foreach ($items as $delta => $item) {
  144. if (isset($link_file)) {
  145. $uri = array(
  146. 'path' => file_create_url($item['uri']),
  147. 'options' => array(),
  148. );
  149. }
  150. $element[$delta] = array(
  151. '#theme' => 'image_url_formatter',
  152. '#item' => $item,
  153. '#image_style' => $display['settings']['image_style'],
  154. '#path' => isset($uri) ? $uri : '',
  155. '#url_type' => $display['settings']['url_type'],
  156. );
  157. }
  158. break;
  159. }
  160. return $element;
  161. }
  162. /**
  163. * Returns HTML for an image url field formatter.
  164. *
  165. * @param array $variables
  166. * An associative array containing:
  167. * - item: An array of image data.
  168. * - image_style: An optional image style.
  169. * - path: An array containing the link 'path' and link 'options'.
  170. *
  171. * @ingroup themeable
  172. */
  173. function theme_image_url_formatter($variables) {
  174. $item = $variables['item'];
  175. $image = array(
  176. 'path' => $item['uri'],
  177. 'alt' => $item['alt'],
  178. );
  179. // Do not output an empty 'title' attribute.
  180. if (drupal_strlen($item['title']) > 0) {
  181. $image['title'] = $item['title'];
  182. }
  183. // Return the URI path.
  184. if ($variables['url_type'] == 3) {
  185. return $item['uri'];
  186. }
  187. $output = file_create_url($item['uri']);
  188. if ($variables['image_style']) {
  189. $image['style_name'] = $variables['image_style'];
  190. $output = image_style_url($image['style_name'], $item['uri']);
  191. }
  192. $output = image_url_formatter_convert_full_url($output, $variables['url_type']);
  193. if ($variables['path']) {
  194. $path = $variables['path']['path'];
  195. $path = image_url_formatter_convert_full_url($path, $variables['url_type']);
  196. $options = $variables['path']['options'];
  197. // When displaying an image inside a link, the html option must be TRUE.
  198. $options['html'] = TRUE;
  199. $output = l($output, $path, $options);
  200. }
  201. return $output;
  202. }
  203. /**
  204. * Converts a full URL to the choosen format.
  205. *
  206. * @param string $url
  207. * The full URL to convet.
  208. * @param constant $format
  209. * IMAGE_URL_FORMATTER_RELATIVE_PATH for relative path,
  210. * IMAGE_URL_FORMATTER_ABSOLUTE_PATH for absolute path,
  211. * IMAGE_URL_FORMATTER_FULL_URL for full URL.
  212. *
  213. * @return string
  214. * The converted URL.
  215. */
  216. function image_url_formatter_convert_full_url($url, $format = IMAGE_URL_FORMATTER_FULL_URL) {
  217. switch ($format) {
  218. case IMAGE_URL_FORMATTER_RELATIVE_PATH:
  219. $url = _image_url_formatter_get_relative_file_url($url);
  220. break;
  221. case IMAGE_URL_FORMATTER_ABSOLUTE_PATH:
  222. $url = _image_url_formatter_get_absolute_file_url($url);
  223. break;
  224. }
  225. return $url;
  226. }
  227. /**
  228. * Returns an absolute url.
  229. */
  230. function _image_url_formatter_get_absolute_file_url($url) {
  231. global $base_url;
  232. if (strpos($url, $base_url) === 0) {
  233. $url = base_path() . ltrim(str_replace($GLOBALS['base_url'], '', $url), '/');
  234. }
  235. return $url;
  236. }
  237. /**
  238. * Returns a relative url.
  239. */
  240. function _image_url_formatter_get_relative_file_url($url) {
  241. $url = _image_url_formatter_get_absolute_file_url($url);
  242. if ($url[0] == '/') {
  243. $url = substr($url, 1);
  244. }
  245. return $url;
  246. }