Commit 96403e81 authored by Désiré's avatar Désiré Committed by g089h515r806

Issue #[1730946] relative path support, by desire.

parent 677137d9
...@@ -4,7 +4,14 @@ ...@@ -4,7 +4,14 @@
* @file * @file
* Add an URL formatter for image field * Add an URL formatter for image field
*/ */
/**
* Define constants for determine which type of URL should be used.
*/
define('IMAGE_URL_FORMATTER_RELATIVE_PATH', '2');
define('IMAGE_URL_FORMATTER_ABSOLUTE_PATH', '1');
define('IMAGE_URL_FORMATTER_FULL_URL', '0');
/** /**
* Implements hook_theme(). * Implements hook_theme().
*/ */
...@@ -12,14 +19,15 @@ function image_url_formatter_theme() { ...@@ -12,14 +19,15 @@ function image_url_formatter_theme() {
return array( return array(
'image_url_formatter' => array( 'image_url_formatter' => array(
'variables' => array( 'variables' => array(
'item' => NULL, 'item' => NULL,
'path' => NULL, 'path' => NULL,
'image_style' => NULL, 'image_style' => NULL,
'url_type' => NULL,
), ),
), ),
); );
} }
/** /**
* Implements hook_field_formatter_info(). * Implements hook_field_formatter_info().
*/ */
...@@ -42,6 +50,20 @@ function image_url_formatter_field_formatter_settings_form($field, $instance, $v ...@@ -42,6 +50,20 @@ function image_url_formatter_field_formatter_settings_form($field, $instance, $v
$display = $instance['display'][$view_mode]; $display = $instance['display'][$view_mode];
$settings = $display['settings']; $settings = $display['settings'];
$element['url_type'] = array(
'#title' => t('URL type'),
'#type' => 'radios',
'#options' => array(
IMAGE_URL_FORMATTER_RELATIVE_PATH => t('Relative file path'),
IMAGE_URL_FORMATTER_ABSOLUTE_PATH => t('Absolute file path (recommended)'),
IMAGE_URL_FORMATTER_FULL_URL => t('Full URL'),
),
'#default_value' => $settings['url_type'],
);
$element['url_type'][IMAGE_URL_FORMATTER_RELATIVE_PATH]['#description'] = t("No base URL or leading slash, like: 'sites/default/files/image.png'");
$element['url_type'][IMAGE_URL_FORMATTER_ABSOLUTE_PATH]['#description'] = t("With leading slash, no base URL, like: '/sites/default/files/image.png'");
$element['url_type'][IMAGE_URL_FORMATTER_FULL_URL]['#description'] = t("Like: 'http://example.com/sites/default/files/image.png'");
$image_styles = image_style_options(FALSE); $image_styles = image_style_options(FALSE);
$element['image_style'] = array( $element['image_style'] = array(
'#title' => t('Image style'), '#title' => t('Image style'),
...@@ -75,6 +97,20 @@ function image_url_formatter_field_formatter_settings_summary($field, $instance, ...@@ -75,6 +97,20 @@ function image_url_formatter_field_formatter_settings_summary($field, $instance,
$summary = array(); $summary = array();
switch ($settings['url_type']) {
case IMAGE_URL_FORMATTER_RELATIVE_PATH:
$summary[] = t('Use relative path');
break;
case IMAGE_URL_FORMATTER_ABSOLUTE_PATH:
$summary[] = t('Use absolute path');
break;
case IMAGE_URL_FORMATTER_FULL_URL:
$summary[] = t('Use full URL');
break;
}
$image_styles = image_style_options(FALSE); $image_styles = image_style_options(FALSE);
// Unset possible 'No defined styles' option. // Unset possible 'No defined styles' option.
unset($image_styles['']); unset($image_styles['']);
...@@ -121,12 +157,12 @@ function image_url_formatter_field_formatter_view($entity_type, $entity, $field, ...@@ -121,12 +157,12 @@ function image_url_formatter_field_formatter_view($entity_type, $entity, $field,
'options' => array(), 'options' => array(),
); );
} }
//debug($item);
$element[$delta] = array( $element[$delta] = array(
'#theme' => 'image_url_formatter', '#theme' => 'image_url_formatter',
'#item' => $item, '#item' => $item,
'#image_style' => $display['settings']['image_style'], '#image_style' => $display['settings']['image_style'],
'#path' => isset($uri) ? $uri : '', '#path' => isset($uri) ? $uri : '',
'#url_type' => $display['settings']['url_type'],
); );
} }
...@@ -140,7 +176,7 @@ function image_url_formatter_field_formatter_view($entity_type, $entity, $field, ...@@ -140,7 +176,7 @@ function image_url_formatter_field_formatter_view($entity_type, $entity, $field,
/** /**
* Returns HTML for an image url field formatter. * Returns HTML for an image url field formatter.
* *
* @param $variables * @param array $variables
* An associative array containing: * An associative array containing:
* - item: An array of image data. * - item: An array of image data.
* - image_style: An optional image style. * - image_style: An optional image style.
...@@ -160,12 +196,13 @@ function theme_image_url_formatter($variables) { ...@@ -160,12 +196,13 @@ function theme_image_url_formatter($variables) {
} }
$output = file_create_url($item['uri']); $output = file_create_url($item['uri']);
if ($variables['image_style']) { if ($variables['image_style']) {
//debug($image);
$image['style_name'] = $variables['image_style']; $image['style_name'] = $variables['image_style'];
$output = image_style_url($image['style_name'], $item['uri']); $output = image_style_url($image['style_name'], $item['uri']);
} }
$output = image_url_formatter_convert_full_url($output, $variables['url_type']);
if ($variables['path']) { if ($variables['path']) {
$path = $variables['path']['path']; $path = $variables['path']['path'];
$path = image_url_formatter_convert_full_url($path, $variables['url_type']);
$options = $variables['path']['options']; $options = $variables['path']['options'];
// When displaying an image inside a link, the html option must be TRUE. // When displaying an image inside a link, the html option must be TRUE.
$options['html'] = TRUE; $options['html'] = TRUE;
...@@ -173,4 +210,53 @@ function theme_image_url_formatter($variables) { ...@@ -173,4 +210,53 @@ function theme_image_url_formatter($variables) {
} }
return $output; return $output;
} }
\ No newline at end of file
/**
* Converts a full URL to the choosen format.
*
* @param string $url
* The full URL to convet.
* @param constant $format
* IMAGE_URL_FORMATTER_RELATIVE_PATH for relative path,
* IMAGE_URL_FORMATTER_ABSOLUTE_PATH for absolute path,
* IMAGE_URL_FORMATTER_FULL_URL for full URL.
*
* @return string
* The converted URL.
*/
function image_url_formatter_convert_full_url($url, $format = IMAGE_URL_FORMATTER_FULL_URL) {
switch ($format) {
case IMAGE_URL_FORMATTER_RELATIVE_PATH:
$url = _image_url_formatter_get_relative_file_url($url);
break;
case IMAGE_URL_FORMATTER_ABSOLUTE_PATH:
$url = _image_url_formatter_get_absolute_file_url($url);
break;
}
return $url;
}
/**
* Returns an absolute url.
*/
function _image_url_formatter_get_absolute_file_url($url) {
global $base_url;
if (strpos($url, $base_url) === 0) {
$url = base_path() . ltrim(str_replace($GLOBALS['base_url'], '', $url), '/');
}
return $url;
}
/**
* Returns a relative url.
*/
function _image_url_formatter_get_relative_file_url($url) {
$url = _image_url_formatter_get_absolute_file_url($url);
if ($url[0] == '/') {
$url = substr($url, 1);
}
return $url;
}
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