Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

PHP - How do I combine both of these functions

  • 28-05-2015 11:47AM
    #1
    Registered Users, Registered Users 2 Posts: 4,260 ✭✭✭


    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, Registered Users 2 Posts: 6,271 ✭✭✭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, Registered Users 2 Posts: 4,260 ✭✭✭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