开发者

How Do I Use PHP To Show A Unique Header IMG Based On Path On A Drupal Site?

I have a Drupal site that needs to display a unique header image based on the path. I have found some helpful code. It gets me close to where I need to be, but not all the way. I have pasted it at the end of this post.

The issue I am having is that it bases the banner image off of the characters after the first "/" after example.com in the URL. For example, example.com/forum returns a banner of header-FORUM.png.

I need it to work a little differently. I would like it to base the banner returned off the characters after the second "/" after example.com in the URL. For example, example.com/category/term should return a banner of header-TERM.png.

Any help that you can offer with this is m开发者_如何学Cuch appreciated.

Here's the code I mentioned earlier via AdaptiveThemes (FYI, there is a comment on that page that attempts to solve a similar issue to mine but I can't get it to work).

    <?php
// Return a file based on the URL alias, else return a default file
function unique_section_header() {
  $path = drupal_get_path_alias($_GET['q']);
  list($sections, ) = explode('/', $path, 2);
  $section = safe_string($sections);
  $filepath = path_to_theme() . '/images/sections/header-' . $section .'.png';
  if (file_exists($filepath)) {
    $output = $filepath;
  }
  else {
    $output = path_to_theme() . '/images/sections/header-default.png';
  }
  return $output;
}

//Make a string safe
function safe_string($string) {
  $string = strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '-', $string));
  return $string;
}
?>

Thanks!


Not exactly sure what the output of drupal_get_path_alias is, but try this:

<?php
// Return a file based on the URL alias, else return a default file
function unique_section_header() {
  $path = drupal_get_path_alias($_GET['q']);
  $pathSegments = explode('/', $path, 3);
  $section = safe_string($pathSegments[2]);
  $filepath = path_to_theme() . '/images/sections/header-' . $section .'.png';
  if (file_exists($filepath)) {
    $output = $filepath;
  }
  else {
    $output = path_to_theme() . '/images/sections/header-default.png';
  }
  return $filepath;//$output;
}

//Make a string safe
function safe_string($string) {
  $string = strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '-', $string));
  return $string;
}

The only changes made were to the usage of explode. explode will separate the path based on the /, so you just need to access a different element in that array. The last parameter of explode is the maximum number of elements to be returned and may also need to be tweaked


I'm adding an answer so I can include code. This is based on Gilean's response.

/** Return a file based on the URL alias, else return a default file
 */
function unique_section_header() {
  $path = drupal_get_path_alias($_GET['q']);
  $pathSegments = explode('/', $path, 3);
  $section = safe_string($pathSegments[1]);
  $filepath = path_to_theme() . '/images/sections/header-' . $section .'.png';
  if (file_exists($filepath)) {
    $output = $filepath;
  }
  else {
    $output = path_to_theme() . '/images/sections/header-default.jpg';

  }
  return $output;
}

/** Make a string safe
 */
function safe_string($string) {
  $string = strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '-', $string));
  return $string;
}


This is what I would do:

In your theme's template.php, create the function THEMENAME_preprocess_page (replace THEMENAME with the name of your theme) as follows. If it already exists, add the following code to that function. (disclamer: untested code)

function THEMENAME_preprocess_page(&$variables) {
  $path = drupal_get_path_alias($_GET['q']);
  $path_segments = explode('/', $path, 3);
  if ($path_segments[0] == 'category' && !empty($path_segments[1])) {
    $safe_term = strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '-', $path_segments[1]));
    $filepath = path_to_theme() . '/images/sections/header-' . $safe_term .'.png';
    if (!file_exists($filepath)) {
      $filepath = path_to_theme() . '/images/sections/header-default.png';
    }
    $variables['header_image'] = theme('image', $filepath);
  }
}

Using a preprocess function (like the one above) is the Drupal way to make extra variables available for a template file. You only have to add a new element to the $variables array. Once you have done the above, you can simply put the following line in your page.tpl.php:

<?php print $header_image; ?>

This will print the complete <img> element.

PS. Usually, I advice not to base code like this on path aliases. It's a method that breaks easily because path aliases can change.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜