Access to preprocess variables in a custom block
Question
I have created a custom block to display some contact information.
I have a preprocess function that sets a contact variable like so:
function atstump_preprocess(&$vars, $hook) {
$vars['contact'] = array(
'name' => 'Doug Gillum',
'business' => 'Portland Stump Grinding',
/...
);
}
I enabled the PHP filter, and set this as the text:
<ul>
<li><?php print $contact['name']; ?></li>
<li><?php print $contact['phone']; ?></li>
<li><?php print $contact['email']; ?></li>
</ul>
However it doesn't recognize $contact as a proper variable and generates the PHP error E_NOTICE:
Notice: Undefined variable: contact in eval() (line 2 of C:\xampp\htdocs\stumpgrinding\modules\php\php.module(75) : eval()'d code).
Notice: Undefined variable: contact in eval() (line 3 of C:\xampp\htdocs\stumpgrinding\modules\php\php.module(75) : eval()'d code).
Notice: Undefined variable: contact in eval() (line 4 of C:\xampp\htdocs\stumpgrinding\modules\php\php.module(75) : eval()'d code).
Is it even possible to access the preprocess variables in a PHP filter? if not, is there a better way than hard coding them? maybe variable_set
and variable_get
and how would I set that up via the theme, I'd rather not make a module for this.
Solution
It won't work.
If you use a preprocess function, the variables will be available in the template of the block.
I think that, in your case, you should edit the settings.php file and add the following line:
$GLOBALS['email1'] = 'example@email.com';
You'll be able to use the variable $email1
through the whole site.
Have in mind that it's not a good Drupal practice.
OTHER TIPS
Another alternative is to add variables in the site information variables table using the hook hook_form_FORM_ID_alter as follows:
function mymodule_form_system_site_information_settings_alter ( &$form, &$form_state ) {
$form['contact'] = array(
'#type' => 'fieldset',
'#title' => t('Contact'),
);
$form['contact']['contact_name'] = array(
'#type' => 'textfield',
'#title' => t('Contact name'),
'#default_value' => variable_get('contact_name', ''),
'#size' => 40,
);
}
Then in the php filter input you can get these variables using:
<?php echo variable_get('contact_name', '');?>