Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Intercepting and overriding variables and Making new variables available in Drupal theme

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 102
    Comment on it

    Hold the existing variable and override it

    grasp and override the existing variables, to do it we use a function in the template.php file

    For example

    <?php
        function themename_preprocess(&$vars){
            $var['title'] = 'your text here';
        }
    ?>
    

    Please note variables have to be passed by reference (&$vars). Please Clear the theme registery after saving the changes. Otherwise they wont pickup the changes you have made. If not clear how to clear the theme registery please visit Clean Theme Registery

    Making new variables available in the theme

    As we have override the existing variable is assigned in same manner. you need to write the preproccess function in template.php file under the activated theme folder. Do not forget to clear the cache

    For example :

    <?php
        function themename_preprocess(&$vars){
            $var['newvariable_name'] = t('your text here');
        }
    ?>
    

    now newvariable_name will available in variable array of theme. Note I have included the t function to provide for translation of the new string.

    Printing variable

     <?php print $newvariable_name; ?>
    

    Thanks

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: