views:

470

answers:

2

I'm new to Drupal dev, and was trying to add an existing region variable to my module's preprocessor function.

Basically, I've created a new region for my site (here's the definition from within side my theme's .info file):

regions[feeds] = Feeds

From Administer->Blocks, I've added the blocks I want to the new "Feeds" region.

Then, from another module, the "Advanced Front Page" module, I'm trying to add some PHP to my "front page" inside this module. The Advanced Front Page module just allows the site to have a landing page, instead of immediately viewing a list of other site content, etc. I've enabled PHP for the content area, and then added the following:

<div>
    <?php print $feeds; ?>
</div>

It does not print the "Feeds" region, and I believe it's because that region variable is not accessible from outside of the page.tpl.php file. So after looking around, I came upon these links:

http://drupal.org/node/223430

http://drupal.org/node/237391

From there, I tried to add a preprocessor function for the module "Advanced Front Page", which has a module name of "front_page" (or possibly just "front", I'm not 100% sure). Here's my preprocessor function, that I tried to add to both my template.php file, and the /modules/front/front_page.module file (not at the same time, mind you):

function front_preprocess(&$vars)
{
 $vars['feeds'] = theme('blocks', 'feeds');
}

Regardless of where I've placed this file (template.php or front_page.module) it doesn't seem to do anything. Any idea where I might be going wrong?

+3  A: 

I haven't tried the advanced front page module, but when dealing with regions, you shouldn't do what you have done. It's a bit hacky and actually not needed. I don't know how the module hook into the templating system, but your problem is probably getting region variables into it's scope. But instead of trying to get the region into the frontpage using the module, you should instead get it into your page.tpl.php. You can actually do what you first tried, but I would suggest that you alter it a bit like this:

<?php if ($feeds): ?>
<div id="feeds">
    <?php print $feeds; ?>
</div>
<?php endif; ?>

I have improved in two ways.

  1. By adding the if statement, you don't add empty markup. So you wont get an empty div if $feeds doesn't contain anything.
  2. Adding id's to regions is a good idea. It makes styling them or their content a lot easier, also it adds semantic to your page which html is all about.

Now if you only want your blocks to be shown in the front page you can set that up in each of the blocks settings. So you could possibly just use a region that already exist, unless you want your blocks display a outside an existing region. When adding regions it's not a good practice to only add a region to a single page, instead it's much better to control when it's content should be shown. It might be that you don't need to create a new region, but simply can use one that's already made. Also if you want to make some template changes to your front page, you can also create the front-page.tpl.php where you can create a different template layout for your front page if you so desire.

googletorp
Thank you, I will try out this suggestion, and let you all know.
Carl
In then end, you were right. I decided not to use a custom region, and instead I used the existing "content" region, and applied visibility rules to the blocks that I only wanted to show on the home page. I now cannot remember why I thought a custom region was necessary, but, oh well. Live and learn :)Thank you very much.
Carl
+3  A: 

There are several points to address in your question:

  1. I'd second googletorps answer in that you should approach this in a different way.
  2. The *_preprocess functions can only be used to manipulate/inject variables for templates or theme functions, e.g. page.tpl.php, node.tpl.php, theme_links(), etc. As the front_page module does not use a theme function or (special) template to render its output, you can not make the $feed variable there by means of a *_preprocess function.
  3. Sidenote: With *_preprocess functions, naming is crucial. You need to prefix the function name either with the exact module name or the theme name, depending on where you declare it. So in your example, if you want to add a preprocess function to the module, you'd prefix it with 'front_page_', if you add it to your themes template.php, you'd add 'yourThemeName_'.
  4. You could achieve what you want by creating the blocks directly from code in your frontpage content area. Instead of trying to output the (not available) $feed variable, you could call:

    theme('blocks', 'feeds')

    This will cause Drupal to return the themed blocks for the given region ('feeds' in this case). Note that this is still not a good way to do it, as even if you don't use the region in your page.tpl.php, it still gets created for every page request made to your site!

So I would go with googletorps suggestion, adding the new region only if there are other uses for it also. If I'd just wanted to add some blocks to the frontpage, I would not create a new region, but configure the blocks to show in the content region and simply restrict them to show only for <front> in their visibility settings.

Henrik Opel
Thank you for the suggestions. I will give yours, and googletorp's suggestions a try. I appreciate the lengthy response.
Carl
Thanks for the suggestions! I'll definitely keep the ideas in mind. Very helpful for a webdev with little Drupal experience.Take care!
Carl