Search code examples
phpdrupalhttp-redirectdrupal-7hook-menu

Drupal dynamic internal redirect


What I want is pretty simple. I have registered a path

function spotlight_menu() {
    $items = array();

    $items['congres'] = array(
        'title' => 'Congres',
        'title arguments' => array(2),
        'page callback' => 'taxonomy_term_page',
        'access callback' => TRUE,
        'type' => MENU_NORMAL_ITEM,
    );

    return $items;
}

When this menu item is triggered, I want to redirect (without changing the url) to a taxonomy page, of which the term is chosen in a function that runs when this function is called.

How can I do this (especcially without changing the url) ?


Solution

  • You can't call taxonomy_term_page directly as your page callback as you'd need to provide a load function to load the term, which is just going to be too difficult with the setup you've got.

    Instead, define your own page callback as an intermediary and just return the output from taxonomy_term_page directly:

    function spotlight_menu() {
      $items = array();
    
      $items['congres'] = array(
        'title' => 'Congres',
        'page callback' => 'spotlight_taxonomy_term_page',
        'access callback' => TRUE,
        'type' => MENU_NORMAL_ITEM,
      );
    
      return $items;
    }
    
    function spotlight_taxonomy_term_page() {
      // Get your term ID in whatever way you need
      $term_id = my_function_to_get_term_id();
    
      // Load the term
      $term = taxonomy_term_load($term_id);
    
      // Make sure taxonomy_term_page() is available
      module_load_include('inc', 'taxonomy', 'taxonomy.pages');
    
      // Return the page output normally provided at taxonomy/term/ID
      return taxonomy_term_page($term);
    }