1

How can we tell views to use relative paths for dispalying images using image fields. I have created a views slideshow which is displaying images using absolute url which is causing the images to reload on each page load.

facing the same issue in other type of views displays like grid and unformated etc...

3 Answers 3

0

Add your image to the view (mine is called Image2)

Create a theme file for your image field, see screenshot. (mine is called views-view-field--relative-url-example--page--field-image2.tpl.php and it's bolded). Pick the last one for the most granular themeing possible.

screenshot of theme information in view (full size here)

After you have added the theme file to your theme templates (/sites/all/themes/THEME/templates or similar), hit the "Rescan template files" button. Your field template should be bolded now.

Add the following code to your theme file, I removed the comments but you can leave them in if you like:

<?php

/**
 * @file
 * This template is used to print a single field in a view.
 *
 * It is not actually used in default Views, as this is registered as a theme
 * function which has better performance. For single overrides, the template is
 * perfectly okay.
 *
 * Variables available:
 * - $view: The view object
 * - $field: The field handler object that can process the input
 * - $row: The raw SQL result that can be used
 * - $output: The processed output that will normally be used.
 *
 * When fetching output from the $row, this construct should be used:
 * $data = $row->{$field->field_alias}
 *
 * The above will guarantee that you'll always get the correct data,
 * regardless of any changes in the aliasing that might happen if
 * the view is modified.
 */
?>
<?php

    $url  = file_create_url($row->field_field_image2[0]["raw"]["uri"]);
    $url  = parse_url($url);
    $path = $url['path'];

    print $path;
?>

I only printed the path to demonstrate it works so you will need to add the image tag, etc.

Here is my output:

working output from view

You may need to clear the cache and be sure to save your view after you update the theme file.

3
  • I have followed the steps you have defined above but its throwing the following error Notice: Undefined property: stdClass::$field_image_slider in include() (line 27 of /home/beatabun/public_html/sites/all/themes/mytheme/templates/views-view-field--image-slider--block--field-image-slider.tpl.php). Commented Nov 13, 2013 at 19:56
  • is it because I am using multivalue field ?? Commented Nov 13, 2013 at 20:03
  • It could be, you will need to account for that in your template. Commented Nov 13, 2013 at 20:34
0

You can use image_url_formatter module ,it add url formatter in image field formatter than you can use it in content dispaly or field display in views.

0

This did not work for me, but stripping the domain from $output in my view template did:

$output = preg_replace('@http(s)?://www.example.com@', '', $output);
print $output; 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.