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 there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

PHP - How do I combine both of these functions

  • 28-05-2015 10:47am
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 6,262 ✭✭✭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,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