Search code examples
drupaldrupal-7

drupal 7 modify taxonomy page with hook_alter_menu


i want to change or modify all taxonomy pages i write this piece of code in template.php in my bartik theme but some weird things happens.

the $term argument in second function "bartik_term_page" is null.

what is missing? and it sometimes an error like this

it gives an error. Warning: Parameter 1 to bartik_term_page() expected to be a reference, value given in menu_execute_active_handler() (line path\includes\menu.inc).

function bartik_menu_alter(&$menu) {
    $menu['taxonomy/term/%']['page callback'] = 'bartik_term_page'; 
    $menu['taxonomy/term/%']['access arguments'] = array('access content');
}



function bartik_term_page(&$term){

    $voc = taxonomy_vocabulary_load($term->vid);
    var_dump( $term ); die();

      // here you generate the actual content of the page
      // could be done e.g. with an entityfieldquery as follows

      $query = new EntityFieldQuery();
      $query->entityCondition('entity_type', 'node')
              ->fieldCondition('field_category', 'tid', $term->tid);
      $result = $query->execute();
      if (!empty($result['node'])) {
        $build['content']['nodes'] = node_view_multiple(node_load_multiple(array_keys($result['node'])), 'teaser'); // output the node teasers. you can control the markup of the 'teaser' view mode with a template in the theme folder
      } else {
        $build['content']['status']['#markup'] = t('No results found for term ID !tid.', array('!tid' => $term->tid));
      }
      return $build;


}

Solution

  • You're actually introducing a new router item there instead of overriding the existing one.

    The path for the taxonomy page is taxonomy/term/%taxonomy_term, so...

    function bartik_menu_alter(&$menu) {
      $menu['taxonomy/term/%taxonomy_term']['page callback'] = 'bartik_term_page'; 
      $menu['taxonomy/term/%taxonomy_term']['access arguments'] = array('access content');
    }
    

    Clear caches and you should be good to go.