1

I'm attempting to replace the titles on my pages with images. I've added an image field to my content type and have created a view that displays the image for the selected page with a contextual filter.

I am embedding the view in my page.tpl.php page with views_embed_view().

I'm having a problem with the conditional statement. If no image exists for the selected page, I want to show the regular page title. Currently, my code will print an empty <h1> tag on pages with no mages. Can I get some help with this?

I am using Drupal 7 and the Views module version 7.3.3.

<?php $TitleImage = views_embed_view('title_image', 'block'); //Embed View ?>

<?php if (!empty($TitleImage)): //Check to see of View is empty ?>
    <h1 class="title" id="page-title"><?php print $TitleImage; ?> </h1>
<?php else: //Display regular title ?>
    <?php print render($title_prefix); ?>
    <?php if ($title): ?>
        <h1 class="title" id="page-title"><?php print $title; ?></h1>
    <?php endif; ?>
    <?php print render($title_suffix); ?>
<?php endif; ?>

I need to replace the page title in the page.tpl.php file. There are 3 content types that have the field_title_image field. I thought that creating a view with a contextual filter would be the best approach, to not display on content types that do not have the field_title_image field.

I came across this comment that helped me with displaying the image.

Below is my revised code. Is there a better solution?

<?php if (isset($node->field_title_image['und']['0']['filename'])): ?>
<h1 class="title" id="page-title"><img src="/sites/default/files/images/<?php print $node->field_title_image['und']['0']['filename']; ?>" alt="<?php print $node->field_title_image['und']['0']['alt']; ?>"></h1>
<?php else: ?>
<?php print render($title_prefix); ?>
<?php if ($title): ?>
    <h1 class="title" id="page-title"><?php print $title; ?></h1>
<?php endif; ?>
<?php print render($title_suffix); ?>
<?php endif; ?>
  • Hi Adrian, if you have a solution you are allowed to answer your own question, but putting an answer into the question just makes it difficult to read. Also try and work with Drupal array rendering rather than pulling values from arrays print render( $node->field_title_image ). – Duncanmoo Jul 10 '12 at 11:27
2

To answer your specific question; clearly $TitleImage is not empty, views_embed_view is returning wrapper elements or a space. in the view settings make sure "no results" returns nothing using the no results behavior of the field and the view (under advanced).

Why use a view? If that image is on that piece of content then there is no need for a view at all, just use the field:

<?php hide($content['field_title_image']); // So it does not render elsewhere ?>
<?php if (!empty($content['field_title_image'])): ?>
  <h1 class="title" id="page-title">
      <?php print render($content['field_title_image']); ?>
  </h1>
<?php else: //Display regular title ?>
  <?php print render($title_prefix); ?>
  <?php if ($title): ?>
    <h1 class="title" id="page-title"><?php print $title; ?></h1>
  <?php endif; ?>
  <?php print render($title_suffix); ?>
<?php endif; ?>

(if you are on node.tpl.php the filed will be as above, if you are on page.tpl.php it will be inside $page['content'] array)

| improve this answer | |
0

As already said from Duncanmoo, using a view to show the content of a field is a bit excessive. The purpose of the Views module is to show a list of entities, and using a view for getting the information of a single node is not using the module for the purpose it is thought.

You second snippet can be written in a better way.

The template file should just contain the code to show values, not the code to compute those values. You should also avoid to hard code the file path into the template file.

I would rather use the following code, if you need it in a theme.

template.php

function mytheme_preprocess_node(&$variables) {
  if (!empty($variables['node'])) {
    $images = field_get_items('node', $variables['node'], 'field_title_image');
    if (!empty($images)) {
      $image = array_shift($images);

      if ($wrapper = file_stream_wrapper_get_instance_by_uri($image['uri'])) {
        $variables['image_title_url'] = $wrapper->getExternalUrl();
        $variables['image_title_alt_text'] = $image['alt'];
      }
    }
  }
}

page.tpl.php

<?php if (!empty($image_title_url)): ?>
  <h1 class="title" id="page-title"><img src="<?php print $image_title_url; ?>" alt="<?php print $image_title_alt_text; ?>"></h1>
<?php else: ?>
  <?php print render($title_prefix); ?>
  <?php if ($title): ?>
    <h1 class="title" id="page-title"><?php print $title; ?></h1>
  <?php endif; ?>
  <?php print render($title_suffix); ?>
<?php endif; ?>

If you need it in a module, then the module should contain the following code.

function mymodule_preprocess_node(&$variables) {
  if (!empty($variables['node'])) {
    $images = field_get_items('node', $variables['node'], 'field_title_image');
    if (!empty($images)) {
      $image = array_shift($images);

      if ($wrapper = file_stream_wrapper_get_instance_by_uri($image['uri'])) {
        $variables['image_title_url'] = $wrapper->getExternalUrl();
        $variables['image_title_alt_text'] = $image['alt'];
      }
    }
  }
}

The template file being used should contain the following code.

<?php if (!empty($image_title_url)): ?>
  <h1 class="title" id="page-title"><img src="<?php print $image_title_url; ?>" alt="<?php print $image_title_alt_text; ?>"></h1>
<?php else: ?>
  <?php print render($title_prefix); ?>
  <?php if ($title): ?>
    <h1 class="title" id="page-title"><?php print $title; ?></h1>
  <?php endif; ?>
  <?php print render($title_suffix); ?>
<?php endif; ?>

I am using $wrapper->getExternalUrl() because it doesn't take the assumption the file is local to the server, and it works also in the case stream wrappers are used to support remote file storage. See the documentation for drupal_realpath().

| improve this answer | |
  • I could not get this to work. I tied adding the function to my template.php page and the conditional statement to my page.tpl.php. I would see a white screen. Do I need to make any changes to the code? Thanks – Adrian Jul 11 '12 at 14:47
  • That is my bad: A parenthesis was missing. – kiamlaluno Jul 11 '12 at 14:59
  • I updated my pages (template.php, page.tpl.php), with the revisions and am no longer getting the white screen, but the image still does not display. I get the text title on all pages, even if there is an image available. I confirmed that my field name is correct and cleared my cache. Is there something else I am missing? Thanks again. – Adrian Jul 12 '12 at 0:03

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.