1

I'm a newbie in Drupal and I want to add an icon before (or after) the node title, if the node have a 'field_fav' filled. I know it can be done using preprocess functions, but I don't know how to append the title's with the class I desired. Any how to?

Found this : Optionally add image/icon after node title based on boolean content type field? but it only for boolean type value.

1

I found your answer on drupal.org.

function mytheme_preprocess_page(&$variables) {
  $titleFile = 'myicon.png';
  $titleImage = base_path() . path_to_theme() . "/images/titles/" . $titleFile;

  $newTitle = $variables['title'].' <img alt="' . $variables['title'] .'" src="' . $titleImage.'" />';
  $variables['title'] = $newTitle;    
}
  • OK tried but not success. Apparently the title generated was not the title coming from the node.tpl.php, but coming from page.tpl.php. Is the preprocess function will apply to the page template, instead of the node template? Oh by the way, I'm using a template based on Omega. – Akmal FIkri Apr 5 '12 at 13:34
  • function fourlegged_omega_alpha_preprocess_page(&$variables) { if (isset($variables['node']) && $variables['node']->type == 'entry') { if($variables['node']->field_entry_fav) { //print_r($variables['node']->title); $titleFile = 'ic-favourite.png'; $titleImage = base_path() . path_to_theme() . "/images/" . $titleFile; $newTitle = ' <img alt="' . $variables['node']->title . '" src="' . $titleImage . '" />' . $variables['node']->title; $variables['node']->title = $newTitle; } } } – Akmal FIkri Apr 5 '12 at 14:15
1

This is my solution. Note that I am using an Omega sub theme.

function fourlegged_omega_alpha_preprocess_page(&$variables) {
  if (isset($variables['node']->type) && $variables['node']->type == 'entry') {
    if(!empty($variables['node']->field_entry_fav)) {
      $titleFile = 'ic-favourite.png';
      $titleImage = base_path() . "sites/all/themes/fourlegged_omega/images/" . $titleFile;

      $newTitle = $variables['node']->title . '<i class="icon-love" /></i>';
      $variables['title'] = $newTitle;
    } 
  }
}
  • 1
    The "sites/all/themes" part of the path should not be hard-coded; I could install the themes in "sites/all/contributed/themes" and Drupal would find them. – kiamlaluno Apr 6 '12 at 0:56
  • How do we use $variables['title'] in node.tpl.php then? – user1071840 May 9 '13 at 7:20

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.