0

Need help with one problem, I want to use different term title, 1-st for menu and breadcrumbs, 2-nd for term page

For example in menu and breadcrumbs: "doors" In term page: "glasses doors"

I thought create special field for alternative title and add to page.tpl but I don't know how I can do it.

Can anybody help?

3 Answers 3

1

You can use drupal_set_title() with implementing hook_preprocess_page().

/**
 * Implements hook_preprocess_page().
 */
function mymodule_preprocess_page(&$variables) {
  $langcode = $variables['language']->language;
  $term = menu_get_object('taxonomy_term', 2, $langcode);
  if (!empty($term)) {
    drupal_set_title($term->custom_field[$langcode][0]['value']);
  }
}

menu_get_object() may not work because some module may override the menu entry taxonomy/term/% as mentioned by mxh.

If you want more robust way to retrieve the term object. Please use below snippet.

if (arg(0) == 'taxonomy' && arg(1) == 'term' && arg(2)) {
  $term = taxonomy_term_load(arg(2));
}
if (isset($term) && !empty($term)) {
  // code.
}
3
  • cool, thanks! More pretty then I write for page.tpl )) // Check if term page and rewrite $title if ( arg(1) == 'term') { $term = taxonomy_term_load(arg(2)); $title = $term->custom_field['und'][0]['value']; }
    – Andy Mark
    Commented Jul 6, 2016 at 17:22
  • 1-st rule "Don't forget about clear cache"))) after this operation I seen what it's not work(( Now I use for this this code in page.tpl // Check if term page no empty and rewrite $title if ( arg(1) == 'term') { $term = taxonomy_term_load(arg(2)); $title_term = $term->field_title; if (!empty($title_term)) { $title = $title_term['und'][0]['value']; } }
    – Andy Mark
    Commented Jul 6, 2016 at 21:34
  • Yup, every new implemented hook function required clear cache. :)
    – Jimmy Ko
    Commented Jul 6, 2016 at 22:51
0

There a a lot of options to accomplish this. You have a few questions listed. Breadcrumbs can be a loaded question by itself. But Here is one option using the Menu Attributes module https://www.drupal.org/project/menu_attributes you could fill in a Title attribute. Then If you are using Panels, you could override the page name by using a Token of the Name attribute. This would let your breadcrumbs and menu use the 1st title, while giving you a 2nd title.

1
  • I don't use Panel, only Views and code. Menu Attributes don't change breadcrumbs.
    – Andy Mark
    Commented Jul 6, 2016 at 21:42
0

Jimmy Ko get me right way

You can use drupal_set_title() with implementing hook_preprocess_page().

/** * Implements hook_preprocess_page(). */ function mymodule_preprocess_page(&$variables) { $langcode = $variables['language']->language; $term = menu_get_object('taxonomy_term', 2, $langcode); if (!empty($term)) { drupal_set_title($term->custom_field[$langcode][0]['value']); } }

But This not work for me ((( I think about it and my page.tpl code and found right variant.

function mymodule_preprocess_page(&$variables) {
    //  Check if taxonomy page
    if ( arg(1) == 'term') {

        //  load term data
        $term = taxonomy_term_load(arg(2));
        $title_term = $term->custom_field;

        //Check if custom field not empty
        if (!empty($title_term)) {
            // Set Custom Title
            drupal_set_title($title_term['und'][0]['value']);
        }
    }
}

For multilanuages site, maybe need this part of Jimmy Ko code $langcode = $variables['language']->language; and chahge 'und' to $langcode )

1
  • I guess why menu_get_object() not working because of the reason stated in this comment on Drupal.org
    – Jimmy Ko
    Commented Jul 13, 2016 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.