1

Page was rendered correctly but it display the following error message:

Strict warning: Only variables should be passed by reference in include() (line 31 of C:\xampp\htdocs\mysite\sites\all\themes\mytheme\templates\page.tpl.php).

I have a image field name field_header_image and I want to used the image as the header background image in my page.tpl.php. here is my code

<div id="hero-container" class="hero-container" style="background:linear-gradient(rgba(0, 0, 0, 0.35), rgba(0, 0, 0, 0.35)), url('<?php echo render(file_create_url($node->field_header_image['und'][0]['uri'])); ?>');">

It display correctly and I can use the field image as my header background but it display the above error. Is there a better way to get rid of this error without using a module.

0

First of all, the 'und' is not prefered, you should use LANGUAGE_NONE constant instead. This is not part of the sollution, but part of the Drupal coding standards.

This is a tricky one. The function render() expects a string, but you give a part of an object with some deeper array keys. The simplest way to get around the strict warning is to use the following code:

<?php $uri = file_create_url($node->field_header_image[LANGUAGE_NONE][0]['uri']); ?>
<div id="hero-container" class="hero-container" style="background:linear-gradient(rgba(0, 0, 0, 0.35), rgba(0, 0, 0, 0.35)), url('<?php print render($uri); ?>');">
| improve this answer | |
  • 1
    That kind of processing should be done in preprocess functions, is not a good practice to have them inside tpl files. – sanzante Nov 18 '15 at 20:43
  • Oh, I agree with tunic on that. – absoludo Nov 18 '15 at 20:44

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.