0

I came across some threads dealing with this, but they're all quite specific and therefore confusing.

What's the best practice to combine multiple functions in general? I'm using this in a template.php file of a Drupal installation for theming purposes.

Say I have two functions:

function mytheme_preprocess_page(&$variables){
    (...)
}

and

function mytheme_preprocess_page(&$vars, $hook){
    (...)
}

When I use these two functions in my template.php, I'm getting the fatal error: Cannot redeclare mytheme_preprocess_page() (previously declared in (file).

Here's the full code:

function mytheme_preprocess_page(&$variables){

    // Add information about the number of sidebars.
    if (!empty($variables['page']['sidebar_first']) && !empty($variables['page']['sidebar_second'])) {
        $variables['content_column_class'] = ' class="col-sm-6 main-content"';
    } elseif (!empty($variables['page']['sidebar_first']) || !empty($variables['page']['sidebar_second'])) {
        $variables['content_column_class'] = ' class="col-sm-9 main-content"';
    } else {
        $variables['content_column_class'] = ' class="col-sm-12 main-content"';
    }
}


function mytheme_preprocess_page(&$vars, $hook){
    if (isset($vars['node'])) {
        switch ($vars['node']->type) {
            case 'article':
                $vars['title'] = '';
                break;
        }
    }
}

I understand I should merge these functions. Question is: how?

Because these (&$variables) and (&$vars, $hook) seem to be necessary for every separate part.

3 Answers 3

0

defining it like this:

function mytheme_preprocess_page(&$variables, $hook = null) {

Makes the hook parameter optional, you then can call the function like this:

mytheme_preprocess_page($var); or mytheme_preprocess_page($var, $another_var).

In the first case $hook will be null, in the second $hook will have the value of $another_var

You can check in your function like this: if ($hook === null) { Which of the two is called

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I stripped it down to one function with your help. The function name looks like this now: function mytheme_preprocess_page(&$vars, $hook = null){ I placed the content of both my former functions in there. Note: I replaced $variables with $vars in the first block. I wasn't aware both did the same thing.
The one suggestion that I would add to this would be to actually make this three functions. The first function (mytheme_preprocess_page) would be defined as suggested, but have that function call one of the other two functions that you have now renamed with unique names. It is good practice to have your function do one thing and do it well rather than large functions that alter their behavior based on the arguments passed in.
0

My answer probably won't help you solve the issue you are facing, but I'll answer the question:

What's the best practice to combine multiple functions in general?

The best way to combine functions is a class. That's their purpose - to group functions (methods) that deal with a common task and isolate it so that you can focus on solving that one common task / problem.

Once a function needs to be changed, you extend the class and implement new function functionality. That lets you have any number of classes that contain functions with the same name but each can do different things.

Basically you can't get fatal error: Cannot redeclare... that way. However, whether that's applicable with Drupal or if it requires fully procedural code is what I'm not familiar with.

Comments

0

But here you have just one function, not 2 functions. Only second parameter is optional. Unlike some other languages (as i.e. Java) in PHP you can't have 2 functions with same name and different parameters. You can not overload the function.

Same named function with multiple arguments in PHP

So, I guess, you just have to copy content from "both" your functions and place it on one function and delete other one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.