Getting Gravity Forms Field Attributes by Label Text

Getting Gravity Forms Field Attributes by Label Text

I am working on a function to find a field from any form based on the label text. I'd like to return different attributes of the field so that I can use them later.

Originally, I just needed the value of the field, so I used the work here to return the value of a field based on the label:

function itsg_get_value_by_label( $form, $entry, $label ) {
            foreach ( $form['fields'] as $field ) {
                $lead_key = $field->label;
                if ( strToLower( $lead_key ) == strToLower( $label ) ) {
                    return $entry[ $field->id ];
                }
            }
            return false;
        }

I get my value by setting a variable and passing in the field label that I'm looking for:

$mobile_phone = itsg_get_value_by_label( $form, $entry, "Mobile Phone" );

Later on, as I continued to work on my solution, I found that I also needed to find those fields and return the ID. Initially, I wrote the same function and just returned the ID, but I'd like to make the solution more efficient by rewriting the function to return multiple field attributes in an array, as such:

function get_field_atts_by_label( $form, $entry, $label ) {
            foreach ( $form['fields'] as $field ) {
                $lead_key = $field->label;
                if ( strToLower( $lead_key ) == strToLower( $label ) ) {
                    $field_atts = array(
                        'value' => $entry[ $field->id ],
                        'id'    => $field->id,
                    );
                    return $field_atts;
                }
            }
            return false;
        }

My problem now is that I am not quite sure how to retrieve the specific attributes from my function and set them to a variable.

0

1 Answer

Well, I'll go ahead and answer my own question. Such a simple solution to this one. Had a momentary brain fart.

$mobile_phone = get_field_atts_by_label( $form, $entry, "Mobile Phone" );

$mobile_phone_id = $mobile_phone['id'];
$mobile_phone_value = $mobile_phone['value'];

Your Answer

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

Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.