1

I asked a similar question a few months ago, but the truth is that there is very little information about the correct way of doing this.

The simplest way is to use the arg() function, but as the docs clearly state, that is the wrong way of doing it. While trying to follow the correct steps, this is what I have so far:

A menu with the necessary paths:

$items['products/search/%/%'] = array(
        'title'             => 'Products search filtering',
        'description'       => 'Show review categories.',
        'page callback'     => 'ThemeSearchProducts',
        'page arguments'    => array(2,3),
        'access callback'   => 'user_access',
        'access arguments'  => array('access content'),
        'type'              => MENU_NORMAL_ITEM,
        'file'              => 'includes/mymodule_reviews.inc',
);

These arguments are sent via ... arguments (sorry for the joke). to the themesearchproducts function

/**
 * THEME List of reviewable products
 *
 */
function ThemeSearchProducts($arg1, $arg2){

//  $output = theme('search_products', $arg1);
  $output = theme('search_products', 'test');

  echo 'args in theme: ' . $arg1 . $arg2 . ' || ';


  return $output . theme('pager', 10);
}

The echo works fine, but now the question becomes how do I send this variable to the hook_theme of the module, and from there to the tpl.php? I have something like the following:

function MYMODULE_theme ($existing, $type, $theme, $path) {
    $path = drupal_get_path('module', 'MYMODULE'); // . '/templates';
    // ADD THE templates DIRECTORY
    $path = $path . '/templates'; 


    return array(
            // as in 'theme('verbose_method',...)
            'main_reviews' => array(
            'template' => 'MYMODULE-main_page_reviews',
            'path' => $path,
            'variables' => array('forums' => NULL, 'topics' => NULL),
            ),

as you can see, the module now calls the tpl in templates/MYMODULE-main_page_reviews.tpl.php, but I cannot find the correct way to send a variable from the previous callback to the hook_theme. Adding a new argument to the MYMODULE_theme throws an error, and I cannot get rid of it, not even by adding (for example) this new argument in ThemeSearchProducts, like this:

 $output = theme('search_products', 'test');

Is using variable_set also incorrect? I'm trying to follow the "Drupal Standard Practices" as much as possible, but this last step is eluding me.

11
  • 1
    As side note, every function implemented by a module should be prefixed by its short name.
    – apaderno
    Feb 7 '13 at 10:18
  • @kiamlaluno Unless it's implementing a hook on behalf of another module (the token module for example) ;)
    – Clive
    Feb 7 '13 at 10:25
  • what do you mean @kiamlaluno? Are you speaking about the ThemeSearchProducts function? Feb 7 '13 at 10:34
  • @Clive Implementing a hook on behalf of another module is always a hack-way to do things. In the case the other module implements the hook it was not implementing before, you would get an error. Yes, it is what it is normally done when a module implements a hook in behalf of a Drupal core module, but IMO there are better ways. :)
    – apaderno
    Feb 7 '13 at 10:37
  • Yes, I am talking of that function.
    – apaderno
    Feb 7 '13 at 10:38
3

The arguments you pass to theme() when you invoke a theme function are not passed to hook_theme(), and from there to the template file. They are passed directly to the theme function, or the template file, in the case the theme function is using it.

hook_theme() defines the arguments used from the theme function. How those arguments are then passed to theme(), and to the theme functions, depends from the Drupal version:

  • With Drupal 7, they are all passed in an array, which in your case is similar to the following one:

    array('forums' => $forum, 'topics' => $topic);
    

    hook_theme() defines the default arguments used for the variables when no value is given. In your case, you could use array('forums' => $forum); and NULL will be used for 'topics'. (Replace $forum with the value you need to use.)

  • With Drupal 6, you pass the variable as single arguments to theme(), for example as in theme('search_products', $forum, $topic). As with Drupal 7, the default value is used when you don't pass that argument.

In both the cases, the variable declared for the theme function in hook_theme() is the name of the variable passed to the template file. In your case, $topics, and $forums are two variables accessible from the template file.

As side note, the template file you declare in hook_theme() with "template" for your theme_search_products() needs to be search-products, or search_products; Drupal will add the extension .tpl.php to the name, when looking for the template file. You cannot have a theme function that is declared as example_one, and the template file that declared to be example_two. Also, you are trying to use theme_search_products(), but your hook_theme() implementation doesn't define that theme function. The code you are showing contains the definition for theme_main_reviews(). If the theme function you are calling is not defined from another module, that code will not work.

1
  • 1
    yeah, i think I got it. Simply this did the trick in ThemeSearchProducts function: $output = theme('search_products', array('argument1' =>$arg1, 'argument2' =>$arg2, 'argument3' =>$arg3)); Feb 7 '13 at 11:43

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.