Drupal 7 - How to use preprocess functions correctly?

inside my page.tpl.php I have the following code which is causing some problems in the backend. So I want to solve it better, with preprocessing functions.

if (!path_is_admin(current_path())) {
                    $pathArray = explode('/', current_path());
                    if (!empty($pathArray)) {
                        $path_to_node = url("node/".$pathArray[1]);
                        $img = '<img src="'.$theme_path.'/images/default.png" alt="Default" />';                        
                        if (!empty($path_to_node)) {
                            $menuChildArray = explode('/', $path_to_node);
                            if (!empty($menuChildArray[2])) {
                                $menuParent = $menuChildArray[2];
                                    switch($menuParent) {
                                        case "one":
                                        $img = '<img src="'.$theme_path.'/images/one.png" alt="Pic tne!" />';
                                        break;

                                        case "two":
                                        default:
                                        $img = '<img src="'.$theme_path.'/images/two.png" alt="Pic two!" />';
                                        break;
                                    }

                            }
                        print $img;                         
                        }
                    }
                }

      

But how can I figure it out? To give it a try, I did the following:

I added template.php to the Theme folder and added:

function set2015_preprocess_page(&$variables) {
    $variables['set2015_pics'] = 'test';
}

      

Inside page.tpl.php I did the following:

<?php
    print $set2015_pics;

      

But nothing prints ... What am I doing wrong here?

Thank!

+3


source to share


2 answers


Assuming set2015 is the name of your theme, everything looks good, so clearing the cache with drush or in tweak / development / performance should make the variable appear. If set2015 is not the name of your theme, rename the set2015_preprocess_page function to YOURTHEME_preprocess_page



+1


source


Debug it. Add first echo "I'm here";

if

, then hit it, then after the second, if

and so on ... to see what's done and what's not. Try to localize the problem.



0


source







All Articles