1

I'm working on a custom theme with message field(textfield) in custom advanced theme setting.

In "theme-settings.php", I added a "textfield" for "message" field

<?php
function MYTHEME_form_system_theme_settings_alter(&$form, $form_state) {
    $form['message_value'] = array(
        '#type'     => 'textfield',
        '#size' => 12,
        '#maxlength' => 100,
        '#default_value' => theme_get_setting('message_value'),
        '#description' => t('Some text here'),
    );
}
?>

In "template.php", I added "theme_get_setting"

<?php
  function MYTHEME_preprocess_page(&$vars) {
    $message = theme_get_setting('message_value');
  }

And in my "page.tpl.php", I render the "message" if it is not empty

<?php if(!empty($message)) echo $message; ?>

The problem is, it does not render anything and has no error.

It works if I put the theme_get_setting in page.tpl.php

<?php $message = theme_get_setting('message_value'); ?>

How to make it work if I want to put that in "template.php"?

4

In your template.php preprocess function, you need to pass the message back as a key/value in the variables array. (I set the variable key as "custom_msg" as "message" may be too generic and be accidentally confused with any system message variables"

function MYTHEME_preprocess_page(&$vars) {
    $message = theme_get_setting('message_value');
    $vars['custom_msg'] = $message;
}

Then, in your page.tpl.php, the variable would be accessible as $custom_msg (or whatever custom key you set in the preprocess function) and can be included in your page template as:

<?php if(!empty($custom_msg)) echo $custom_msg; ?>

More information about this can be found on this Drupal.org page: Setting up variables for use in a template (preprocess and process functions)

2

Your code in template.php needs modification

instead of $message, you need to mention $vars['message']. If you need to send a variable from template.php file to a tpl file, then the variable has to be mentioned in $vars['variable_name'] or $variables['variable_name'], depending upon the argument set in the preprocess method.

So here goes the modification to your code:

function MYTHEME_preprocess_page(&$vars) {
$vars['message'] = theme_get_setting('message_value');
}

And then in you tpl file, you will be able to render the 'message' variable.

if(!empty($message)) echo $message;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.