Skip to main content
fixed the code
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284
<?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; ?>
<?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; ?>
<?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; ?>
<?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; ?>
<?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; ?>
<?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; ?>
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

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().