Drupal * _preprocess with module
I'm new to Drupal dev and was trying to add an existing scope variable to my module preprocessor function.
Basically, I created a new region for my site (here's the definition from within my theme's .info file):
regions[feeds] = Feeds
From Administer-> Blocks I added the blocks I want to the new "Feeds" area.
Then, from another module, the "Extended Front Page" module, I try to add some PHP to my "main page" inside that module. The Advanced Front Page module allows the site to have a landing page rather than immediately viewing a list of other site content, etc. I included PHP for the content area and then added the following:
<div>
<?php print $feeds; ?>
</div>
It does not print the "Channels" area, and I believe this is because the region variable is not available from outside the page.tpl.php file. So after looking around I came across these links:
From there I tried to add a preprocessor function for the "Advanced Front Page" module, which has the module name "front_page" (or perhaps just "front", I'm not 100% sure). Here's my preprocessor function that I tried to add to both template.php and / modules / front / front_page.module (not at the same time, mind you):
function front_preprocess(&$vars)
{
$vars['feeds'] = theme('blocks', 'feeds');
}
No matter where I put this file (template.php or front_page.module) it does nothing. Any idea where I might be going wrong?
source to share
I haven't tried the extended first page module, but when you're doing regions, you shouldn't be doing what you did. This is a bit hacky and really unnecessary. I don't know how the module connects to the templating system, but your problem is probably with cloud variables. But instead of trying to get the region on the homepage using a module, you should instead enter it into your page.tpl.php. You can actually do what you first tried, but I would suggest you change it a bit:
<?php if ($feeds): ?>
<div id="feeds">
<?php print $feeds; ?>
</div>
<?php endif; ?>
I improved in two ways.
- By adding an if statement, you are not adding empty markup. This way you won't get an empty div if $ feeds doesn't contain anything.
- Adding ID to regions is a good idea. This makes it easier to style them or their content, and also adds semantics to your page where the html resides.
Now, if you want your blocks to appear on the first page, you can set this in each of the block settings. So you can just use a region that already exists if you don't want your blocks to appear outside of the existing region. When adding regions, it is not a good practice to add a region to only one page, instead it is much better to control when the content should be displayed. Perhaps you don't need to create a new region, but just use the one that has already been created. Also, if you want to make some changes to the templates on your first page, you can also create front-page.tpl.php where you can create a different template layout for your main page if you so desire.
source to share
There are several questions in your question:
- I would say the second googletorps will answer that you should approach this differently.
- Functions
*_preprocess
can only be used for variable control / injection for templates or theme functions, for example. page.tpl.php, node.tpl.php,theme_links()
etc. Since the front_page module does not use a theme function or (special) template to output its output, you cannot make the $ feed variable there facility*_preprocess
. - Sidenote: When using features,
*_preprocess
naming is critical. You must prefix the function name with either the exact module name or the theme name, depending on where you are declaring it. So in your example, if you want to add preprocess function to the module, you must prefix it with'front_page_'
, if you add it to your template.php themes add'yourThemeName_'
. -
You can achieve what you want by creating blocks directly from code in the content area of the master page. Instead of trying to output the $ feed variable, you can call:
theme ('blocks', 'feeds')
This will cause Drupal to return the topic blocks for the given region (in this case, "channels"). Note that this is still not a very good way to do it, as even if you don't use a region on page.tpl.php, it is still generated for every page request made to your site!
So, I would go with googletorps' suggestion, adding a new region only if there are other uses for it. If I just wanted to add some blocks to the master page, I wouldn't create a new area, but set up the blocks to display in the content area and just restrict them to display only for <front>
in my visibility settings.
source to share