0

my code not working. I'm trying to get custom breadcrumbs if user is different taxonomy vocabulary category page. if elseif's not working, but I don't know why..

Example: If I go to taxonomy term page that vocabulary id is 2, breadcrumbs is:

Page 1 > Page 1 > Term name

and vocabulary id 2, breadcrumb looks like this:

Page 2 > Page 2 > Term name

and if ID is not defined it shows default breadcrumbs ( else )..

It shows page 1 and else breadcrumbs but else if statement not working..

Is that code correct or what?

 /*
 * Taxonomy term callback page
 */

function taxonomy_taxonomy_term_page($term_id) {
$term = taxonomy_term_load($term_id);
$html = '';
$breadcrumb = array();
$parents = taxonomy_get_parents($term->tid);
$desired_vocab_1 = 1; // Vocabulary ID
$desired_vocab_2 = 2; // Vocabulary ID
$desired_vocab_3 = 3; // Vocabulary ID

if(count($parents) > 0) {

    $show_sub_cat = false;
    $list = get_legal_articles($term->tid);
    foreach($parents as $item)
        $_parent = $item;

    $breadcrumb[] = l(t('Default page'), 'Default page');
    $breadcrumb[] = l($_parent->name, 'taxonomy/term/'.$_parent->tid);

    $html =  theme('category_details', array('term' => $term, 'list' => $list, 'is_sub_cat' => $show_sub_cat));

 } else {

 if (((arg(0) == 'taxonomy')
 && (arg(1) == 'term')
 && is_numeric(arg(2))
 && ($term = taxonomy_term_load(arg(2)))
 && ($term->vid == $desired_vocab_1))) {

$breadcrumb[] = l(t('Page 1'), 'Page 1');

$childs = taxonomy_get_children($term->tid);

$html = theme('parent_category_details', array('term' => $term, 'list' => $childs));


 } elseif (((arg(0) == 'taxonomy') 
  && (arg(1) == 'term') 
  && is_numeric(arg(2)) 
  && ($term = taxonomy_term_load(arg(2))) 
  && ($term->vid == $desired_vocab_2))) {

$breadcrumb[] = l(t('Page 2'), 'Page 2');
$breadcrumb[] = l(t('Page 2.1'), 'Page 2.1');

$childs = taxonomy_get_children($term->tid);

$html = theme('private_category_details', array('term' => $term, 'list' => $childs));

} else {

$breadcrumb[] = l(t('Page 3'), 'Page 3');
$breadcrumb[] = l(t('Page 3.1'), 'Page 3.1');

$childs = taxonomy_get_children($term->tid);

$html = theme('private_category_details', array('term' => $term, 'list' => $childs));


}

// Set Breadcrumbs
drupal_set_breadcrumb($breadcrumb);
drupal_set_title($term->name);

return $html;
}
  • I don't know if it is copy paste mistake, but I don't see the declaration for $desired_vocab_1. In addition, there are too many circle brackets in your condition. – jmu Sep 22 '16 at 21:24
  • Yes sry I missed $desired_vocab_1. Added now. – Penalse Sep 28 '16 at 7:32
0

In your code the html of 'Default page' is never returned. I have refactored it a little bit to make it cleaner and easier to maintain.

/*
 * Taxonomy term callback page
 */
function taxonomy_taxonomy_term_page($term_id) {
  $term = taxonomy_term_load($term_id);
  $breadcrumb = array();
  $parents = taxonomy_get_parents($term->tid);
  // Vocabulary IDs
  $desired_vocab_1 = 1;
  $desired_vocab_2 = 2;
  $desired_vocab_3 = 3;

  if (count($parents) > 0) {
    $show_sub_cat = false;
    $list = get_legal_articles($term->tid);

    foreach ($parents as $item) {
      $_parent = $item;
    }

    $breadcrumb[] = l(t('Default page'), 'Default page');
    $breadcrumb[] = l($_parent->name, 'taxonomy/term/' . $_parent->tid);
    $html = theme('category_details', array('term' => $term, 'list' => $list, 'is_sub_cat' => $show_sub_cat));
  }
  else if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
    $term = taxonomy_term_load(arg(2));

    switch ($term->vid) {
      case $desired_vocab_1:
        $breadcrumb[] = l(t('Page 1'), 'Page 1');
        break;
      case $desired_vocab_2:
        $breadcrumb[] = l(t('Page 2'), 'Page 2');
        $breadcrumb[] = l(t('Page 2.1'), 'Page 2.1');
        break;
      case $desired_vocab_3:
        $breadcrumb[] = l(t('Page 3'), 'Page 3');
        $breadcrumb[] = l(t('Page 3.1'), 'Page 3.1');
        break;
      default:
        $breadcrumb[] = l(t('Default page'), 'Default page');
    }

    $childs = taxonomy_get_children($term->tid);
    $html = theme('private_category_details', array('term' => $term, 'list' => $childs));

    drupal_set_title($term->name);
  }

  // Set Breadcrumbs
  drupal_set_breadcrumb($breadcrumb);

  return $html;
}
  • Thanks for the response, but now breadcrumbs doesn't works and I need add different html theme file for default page and page 1 and page 2, but I can't get these working inside switch function – Penalse Sep 28 '16 at 7:16

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.