English 中文(简体)
How does one inject variables into page templates from a custom Drupal module?
原标题:

We ve created a custom module for organizing and publishing our newsletter content.

The issue I m running into now -- and I m new to theming and Drupal module development, so it could just be a knowledge issue as opposed to a Drupal issue -- is how to get each newsletter themed.

At this point the URL structure of our newsletter will be:

/newsletters/{newsletter-name}/{edition-name}/{issue-date} which means that we can create template files in our theme using filenames like page-newsletters-{newsletter-name}-{edition-name}.tpl.php, which is great. The one issue I m running into is that all of the content comes through in the $content variable of the theme. I d like to have it come through as different variables (so that I can, inside the theme, place certain content in certain areas.)

Is there a proper way for doing this?

Edit: To answer some questions: The issue is a node (there are issue, edition and newsletter nodes) and the path is being set using hook_menu with wildcards and a router.

最佳回答

The best answer I could find was to add a check inside of phptemplate_preprocess_page to send the vars back to the module and have them be updated.

Like so:

function phptemplate_preprocess_page(&$vars) {
    if (module_exists( test_module )) {
        _test_module_injector($vars);
    }
}

then in my test_module.module file I created this function:

function _test_module_injector(&$vars) {
    $vars[] = call_to_other_functions_to_load_vars();
}

It seemed to work. I wish there was a way to do this without having to touch the theme s template.php file, but otherwise this works well.

问题回答

If there were better documentation for template preprocess functions, Drupal would be a lot more accessible - as it is, you need to piece together the information from a lot of different explanations. One place to start is here:

http://drupal.org/node/223430

but if you take the time to work through the tutorial below, you ll find you can do most things:

http://11heavens.com/theming-the-contact-form-in-Drupal-6

This is an old post, and the OP s issues seems to have been solved.

However, just for others finding this through Google (or otherwise):

  1. Install the Devel module: http://drupal.org/project/devel
  2. Also the Devel Themer module: http://drupal.org/project/devel_themer

Use Devel Themer to go through the $content variable and find what you need to pull out.

There are a bunch of Devel/Themer docs/tuts out there, but its usage is pretty straightforward. Note, though, that some stuff in there will need to be sanitized before printing in the theme.

The suggestion to show the node as a View and then modifying the view templates sounds pretty crazy, though it ll work.





相关问题
Drupal Multi-language: Simple strings not translated

I m adding additional languages to a Drupal site that I m building. Getting the translation of content working is fairly easy using the Internationalisation module. Yet, simple things such as date ...

Setting up a WYSIWYG editor for Drupal site users [closed]

Looking through the Drupal contrib modules, and after a few Google searches, it becomes evident that there are any number of choices and combos available to set up a WYSIWYG editor in Drupal. I m ...

Change size of user/password login box

I don t know how to change the size of the login username/password boxes on the drupal site that I m trying to build. I m stumbling through the theming, and don t know where to find the file that ...

How does Drupal provide an edit/review/publish model?

How does Drupal support a means to update and review a website before it is published? Does it only allow you to preview a page at a time before you publish it or is there a way to create a site ...

Term for rotating header

I m looking for terminology that describes this behavior: The header of a web-page contains a different image every time you visit it. Update: It is not an advertisement, but images related to the ...

Has anyone checked out Drupal 7? [closed]

Has anyone checked out a copy of Drupal 7 yet? What do people think? I m pretty excited about the PDO and all of the designers I work with a very excited about the new admin interface/structure. Do ...

热门标签