$vars doesn't seem to be working in preprocess
Hi all... I have the following code in my THEME_preprocess_page function in template.php
$vars['is_home_page'] = (arg(0).arg(1) == 'node1');A little explanation is in order, I think. I have a page-front.tpl.php that presents my splash screen, so node/1 is my real "home" page. I don't want the title on the home page, so I'm setting the $is_home_page var. I know I could just set $vars['title'] to NULL (or blanks), right? ...but I thought I might have other uses for $is_home_page later; that's why I'm doing it this way. (Btw, I tried setting $vars['title'] = NULL - that didn't work either.)
I know the above code is being executed, because I had a type-o in it at first that caused a fatal php error. But the following code from page.tpl.php doesn't work (i.e. it shows the title even on node/1)...
<?php if (($title) AND (! $is_home_page)) : ?>
<h1 class="title"><?php print $title; ?></h1>
<?php endif; ?>In page.tpl.php, I dumped the $variables array - $is_home_page is not there. My node/1 is aliased as "home", but I've tried it as mysite.com/home and mysite.com/node/1. Same result on both. When I changed the conditional to this...
<?php if (($title) AND (arg(0).arg(1) != 'node1')) : ?>
...it worked as expected.

What did you name the
What did you name the argument to THEME_preprocess_page, $variables or $vars?
Does it matter?
It doesn't really matter, does it? I mean it's an arg to the function, so I cud name it $xyz and it would still work, right?
I tried it both ways though, just in case - as $vars and $variables. And, btw, the arg is specified as a reference - &$vars.
It matters very much, the
It matters very much, the argument must be specified by reference since all changes are made to the argument. What does your function look like?
wrong approach?
I get the feeling that you're taking the wrong approach to setting your home page. The usual way to set node/1 as your home page it to go to /admin/settings/site-information and enter "node/1" in the 'Default front page' field. That way, you might not even need a separate page-front.tpl.php; page.tpl.php will automatically render node/1 when you navigate to your site root. Once you have your home page set up like this, you can use the standard drupal_is_front_page() function (http://api.drupal.org/api/function/drupal_is_front_page/6) to do your theming logic:
<?php if ($title && !drupal_is_front_page()): ?><h1 class="title"><?php print $title; ?></h1>
<?php endif; ?>
Using a splash screen...
@marcvangend:
My real (drupal) front_page is my splash screen - node/1 is what that page links to. In my site, I want all "home" links to return to node/1, not my splash screen. I've got the alias and all the links set up and working ok and, even for this "problem", I have a work-around. But I still want to understand how to init $vars (if I want/need to).
@nevets:
Ok, then I'm confused. Wherever Drupal calls my THEME_preprocess_page, it is simply passing its "vars" array to my function (by reference). My function doesn't know or care what Drupal named it. They (Drupal) could have named it $my_private_vars_and_you_cant_have_them. But in MY function, I can choose how *I* want to refer to that reference - by $vars, or $variables, or $my_local_array. Is this not correct?
And, as I mentioned before, I DID specify by reference. In any case, here is my function def:
function mytheme_preprocess_page(&$vars, $hook) {$vars['is_home_page'] = (arg(0).arg(1) == 'node1');
}
I think you understand the
I think you understand the naming of variables correctly. If I'm not mistaken, all Nevets meant to say that it matters which variable name ($vars or $variables) you use, because it MUST match the variable name used in your function statement (
function foo(&$vars)orfunction foo(&$variables)).Talking about splash pages: I have heard good things about http://drupal.org/project/splash, but I won't blame you for sticking to your current solution if that works for you.
@marcvangend, thanks for that
@marcvangend, thanks for that recommendation. I'll check it out.
Do you have any thoughts as to why the $is_home_page var is not getting created?
No, sorry, I couldn't figure
No, sorry, I couldn't figure out why that var isn't there. Have you tried to set it manually (ie.
$vars['is_home_page'] = FALSE;), just to see if that does work?Unless your theme is named
Unless your theme is named 'mytheme', mytheme in mytheme_preprocess_page() should be replace with you actual themes name.
Yes, of course...
...mytheme was just for my example. I didn't want to give away any trade secrets (jk - hehe).
I just tried setting $vars['is_home_page'] = FALSE -- it didn't work.
But the work-around I mentioned way back at the beginning of this thread was to use this in my page.tpl.php...
<?php if (($title) AND (arg(0).arg(1) != 'node1')) : ?>...instead of...
<?php if (($title) AND (! $is_home_page)) : ?>...and that works just fine. However, I know that at some point in my Drupal development career I will need to set vars in ...preprocess... that won't have such an easy work-around. I would still like to know why it's not working. Is there anything else you guys can think of that I need to check?... or any other code I could show you?
All your effort so far is greatly appreciated.
I don't have a clue what
I don't have a clue what could be wrong at this moment... but you could show us the complete preprocess function.
Ummmmmmm!!!
O...M...G!!! It's working now.
I was in the process of replying to ur last post - to include the full preprocess function (even though I showed the entire function in a previous post), and I was going over everything I have done up to this point. One of those things was your suggestion to set $vars['is_home_page'] = FALSE. At that point I realized I should have set it = TRUE in order to see if the title disappeared. I tried it like that again and it worked this time. So I put everything back the way it was in the beginning (when it wasn't working) and now it works.
What's changed since then is this... I was using page-front.tpl.php as my splash screen and using an alias of "home" for all other pages to return to (for breadcrumbs and other such links). Then I checked out the splash module (also at your suggestion) and decided to use that instead of page-front.tpl.php. I still needed this $is_home_page var in order to show or not show the title and, as I said, that logic is now working just fine. I don't know what it was in the previous set up that made it not work, but, at this point, I'm not really keen to put it all back just to figure it out. It ain't broke now, so I'm not gonna "fix" it - to paraphrase.
Thanks guys for hanging in there with me through all this. Sorry it turned out to be such a waste of time for us all.
Cheers
I think it wasn't a waste of
I think it wasn't a waste of time, because you have now learned about debugging, preprocess functions, Drupal's front page handling and probably more. Every developer has these gotcha-moments where you suddenly realize that you better throw out a lot of your code and do it differently.