读书人

Drupal 6amp;七 Custom Template And Inv

发布时间: 2013-09-28 10:01:20 作者: rapoo

Drupal 6&7 Custom Template And Invoke Module

function mymodule_theme($existing, $type, $theme, $path) {? ? return array(? ? ? ? 'mymodule_theme_function' => array(? ? ? ? ? ? 'template' => 'mymodule_theme_function',? ? ? ? ? ? 'arguments' => array(? ? ? ? ? ? ? ? 'argument_1' => NULL,? ? ? ? ? ? ? ? 'argument_2' => NULL,? ? ? ? ? ? ? ? 'argument_n' => NULL,? ? ? ? ? ? )? ? ? ? ),? ? );}

Throughout your code, when you wanted to call that theming function, you would call something like:

$themed_content = theme('mymodule_theme_function', ? ? ? ? 'Argument 1 Text Sample', ? ? ? ? 'Argument 2 Text', ? ? ? ? 'Argument 3 number or image'? ? );

The declaration would go and look for the mymodule_theme_function.tpl.php in the same folder as your module and render the contents according to that template.

the drupal 7 way

Doing it in Drupal 7 has slightly changed the names and ordering of the parameters:

function my_d7_module_theme($existing, $type, $theme, $path) {? ? return array(? ? ? ? 'my_d7_module_theme_function' => array(? ? ? ? ? ? 'template' => 'my_d7_module_theme_function',? ? ? ? ? ? 'variables' => array(? ? ? ? ? ? ? ? 'argument_1' => NULL,? ? ? ? ? ? ? ? 'argument_2' => NULL,? ? ? ? ? ? ? ? 'argument_n' => NULL,? ? ? ? ? ? )? ? ? ? ),? ? );}

The first note is that it's now called 'variables' in the hook_theme() override.? If you use the old name 'arguments', then you will likely see error messages come up in your Drupal log:

$themed_content = theme('my_d7_module_theme_function', ? ? ? ? 'Argument 1 Text Sample', ? ? ? ? 'Argument 2 Text', ? ? ? ? 'Argument 3 number or image'? ? );

The first thing you will notice is that you only get 'A' in your function for argument_1 and that the other arguments are null.

This is due to how the new, improved and Drupal 7 version of the theme() function needs to be called:

$themed_content = theme('my_d7_module_theme_function', array(? ? ? ? "argument_1" => 'Argument 1 Text Sample',? ? ? ? "argument_2" => 'Argument 2 Text',? ? ? ? "argument_n" => 'Argument 3 number or image',? ? ));

As you can see from the code above, the theme function takes an array as its second parameter. This array is a keyed array with the key being the name of the variable that was defined in the my_d7_module_theme(...) function and the value is the data that you want to assign to that variable and pass to the template.

读书人网 >行业软件

热点推荐