1

I need to use the image resulting from a view as the background image to a div in my page.tpl.php.

I am able to render the page title with the view below it using:

<div class="container-inner clearfix">
  <h1 id="page-title">
    <span><?php print $title ?></span>            
  </h1>
  <?php $views = views_embed_view('photos', 'block'); print render($views);?>
</div>

However, is there a way that I can pull the image from the view, and use it as the background image for container-inner?

2

Sure. Just look at what's inside $views and grab the path/url of the image there and stuff that into $background_image and then do something like:

<div class="container-inner clearfix" style="background: url(<?php print $background_image; ?>)">

Sidenote: it might be better to this in a preprocess hook and pass in this as a variable instead of putting this logic in your page.tpl.php file, eg, something like:

function my_module_or_theme_preprocess_page(&$vars) {

  // $views=views_embed_view('photos', 'block');
  $views=views_get_view_result('photo', 'block'); // appears to work easier :)
  $background_image=// however you get the url/path to the image from $views;

  $vars['background_image']=$background_image;

}

which will create a $background_image variable that is now available inside page.tpl.php.

  • So, if I am to use it in a preprocess hook, how can I manually call the correct view? As far as I know, hook_views_pre_render and the like apply to a view that is already being used on the page. – Mrweiner Nov 6 '13 at 0:55
  • I meant in the page preprocess hook, eg, don't put all this Views php logic in your page template, but rather just print variables there, and instead do all the Views stuff in your page preprocess function. I added what I mean to the answer above. – Jimajamma Nov 6 '13 at 0:57
  • Right, I understand that. I just mean that when utilizing the preprocess hook, how do I call the correct view? When I dpm($view) it utilizes the view currently displayed on the page. But if there is no view on the page (being rendered as a block or a views pages) then there is nothing to print. In my situation, no actual view will be used on the page itself, because all that I need is the image being called by this function. It doesn't need to display anywhere else. Is there a way to force call the correct view within the preprocess hook? – Mrweiner Nov 6 '13 at 1:05
  • Ah, yes, Views will put a $view variable in almost every single template it touches. But in your example you were setting a $views variable yourself inside your page.tpl.php and not relying upon $view being there already hence my confusion. But either way, you can call views_embed_view() or views_get_view_result() from just about anywhere you like and use the results equally any way you like. – Jimajamma Nov 6 '13 at 1:09
  • Looks like that was the ticket. As a sidenote, though, I had to use views_get_view_result() in order for this to work. When using views_embed_view(), dpm($views) simply returned the raw html for the view as if it were to be embedded somewhere. views_get_view_result() gave me access to the variables. Not sure if you want to should edit your answer accordingly or not. Thanks for the help! – Mrweiner Nov 6 '13 at 1:42
1

Make sure to have a look at Background Images Formatter. It allows you to define a background-image (and size) for any given CSS-selector, as specified in your view (using an image field).

  • I actually just recently used this for an unrelated project. Great suggestion for people still coming across this. – Mrweiner Nov 5 '14 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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