Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

PHP - How do I combine both of these functions

  • 28-05-2015 11:47am
    #1
    Registered Users Posts: 4,258 ✭✭✭


    I have 2 separate disclaimers but they are using the same code.

    The problem is they won't work together.

    How can I combine both of these functions into a single working function

    function decawave_preprocess_field(&$vars) {
    if($vars == "field_resource_public_pdf" || $vars == "field_resource_pdf") {
    $vars[0] = !empty($vars->field_disclaimer) ? $vars->field_disclaimer[LANGUAGE_NONE][0] : 0;}
    }

    function decawave_preprocess_field2(&$vars) {
    if($vars == "field_resource_public_pdf" || $vars == "field_resource_pdf") {
    $vars[0] = !empty($vars->field_antenna_disclaimer) ? $vars->field_antenna_disclaimer[LANGUAGE_NONE][0] : 0;
    }
    }


Comments

  • Registered Users Posts: 6,250 ✭✭✭Buford T Justice


    From what I can see the only difference is the method call to either field_disclaimer or field_antenna_disclaimer, so why not call a single method twice and pass the name of the method to call with it
    Something like below?
     function decawave_preprocess_field(&$vars, $mName) {
                    if($vars['element']['#field_name'] == "field_resource_public_pdf" || $vars['element']['#field_name'] == "field_resource_pdf") {
                        $vars['items'][0]['#disclaimer'] = !empty($vars['element']['#object']->$mName) ? $vars['element']['#object']->$mName[LANGUAGE_NONE][0]['value'] : 0;
                    }
                }
    

    edit: a more efficent way is to combine them in a single function, if you are always calling it from the same place
    function decawave_preprocess_field(&$vars) {
                    if($vars['element']['#field_name'] == "field_resource_public_pdf" || $vars['element']['#field_name'] == "field_resource_pdf") {
                        $vars['items'][0]['#disclaimer'] = !empty($vars['element']['#object']->field_disclaimer) ? $vars['element']['#object']->field_disclaimer[LANGUAGE_NONE][0]['value'] : 0;
                        $vars['items'][0]['#disclaimer'] = !empty($vars['element']['#object']->field_antenna_disclaimer) ? $vars['element']['#object']->field_antenna_disclaimer[LANGUAGE_NONE][0]['value'] : 0;
                    }
                }
    


  • Registered Users Posts: 4,258 ✭✭✭swingking


    Yes I have tried that but it's not allowing the disclaimer file to be displayed directly.

    Only when you have the first function operating and not the second one

    Thanks


Advertisement