frankyonnetti
2/1/2020 - 7:20 PM

#drupal #d7 #tpl

Drupal 7 - use specific field in page #drupal #d7 #tpl

<?php
/**
 *
 * http://josephfitzsimmons.com/creating-a-hero-page-template-in-drupal/
 *
 */


// template.php

/**
* Implements template_preprocess_page().
*/
function oswego_preprocess_page(&$variables) {
 
  // Check to see if a hero image field is set
  if (isset($variables['node']->field_landingpage_hero_image[LANGUAGE_NONE][0])) {
 
    // Store the image into $img for easy reference
    $img = $variables['node']->field_landingpage_hero_image[LANGUAGE_NONE][0];
 
    // Build the $hero array to be used in templates/page/page--hero.tpl.php
    $variables['hero']['path'] = image_style_url('landing_page_hero_image', $img['uri']);
    $variables['hero']['alt'] = $img['alt'];
    $variables['hero']['title'] = $img['title'];
    
    // Add this line if you only want the variables available on this specific template.
    // Add a theme hook suggestion to load templates/page/page--hero.tpl.php
    $variables['theme_hook_suggestions'][] = 'page__hero';
  }
}


// page.tpl

<div class="hero-image">
  <img src="<?php print $hero['path']; ?>" alt="<?php print $hero['alt']; ?>" title="<?php print $hero['title']; ?>">
  <?php if ($title): ?>
    <div class="hero-header">
 
    <div class="row">
      <div class="section-title-container">
        <div class="section-title-inner">
          <?php print $breadcrumb; ?>
        </div>
      </div>
    </div>
 
    <h1 class="hero-title"><?php print $title; ?></h1>
    </div>
  <?php endif; ?>
</div>