0
votes

I am using Gravity forms and I have got it making an auto notification. This notification happens after my android app connects to it and adds an entry. My issue is that it does not perform variable replacement.

So for example Subject in the form editor is defined as New Moving House Apps submission from {Full Name:2}

But the email ends up as New Moving House Apps submission from

All form variables are replaced with nothing in the email. but if i check the entry out in the website it is there.

I have added my code to the post_entries function in the Gravity Forms WebApi as follows:

public function post_entries($data, $form_id = null) {

    $this->authorize('gravityforms_edit_entries');

    $result = GFAPI::add_entries($data, $form_id);

    if (is_wp_error($result)) {
        $response = $this->get_error_response($result);
        $status = $this->get_error_status($result);
    } else {
        $status = 201;
        $response = $result;

        // This is the form object from Gravity Forms.
        $form = \GFAPI::get_form($form_id);

        $event = 'form_submission';

        $notifications = GFCommon::get_notifications_to_send($event, $form, $lead[0]);
        $notifications_to_send = array();

        //running through filters that disable form submission notifications
        foreach ($notifications as $notification) {
            if (apply_filters("gform_disable_notification_{$form['id']}", apply_filters('gform_disable_notification', false, $notification, $form, $lead), $notification, $form, $lead)) {
                //skip notifications if it has been disabled by a hook
                continue;
            }

            $notifications_to_send[] = $notification['id'];
        }

        GFCommon::send_notifications($notifications_to_send, $form, $lead, true, $event);
    }

    $this->end($status, $response);
}

EDIT: here's the working code with Naomi's help

public function post_entries( $data, $form_id = null ) {

    $this->authorize( 'gravityforms_edit_entries' );
    $result = GFAPI::add_entries( $data, $form_id );

    if ( is_wp_error( $result ) ) {
        $response = $this->get_error_response( $result );
        $status   = $this->get_error_status( $result );
    } else {
        $status   = 201;
        $response = $result;

       $lead = \GFAPI::get_entry($result);
       // This is the form object from Gravity Forms.
       $form = \GFAPI::get_form($form_id);
       $event ='form_submission';

        $notifications         = GFCommon::get_notifications_to_send( $event, $form, $lead );
        $notifications_to_send = array();

        //running through filters that disable form submission notifications
        foreach ( $notifications as $notification ) {
            if ( apply_filters( "gform_disable_notification_{$form['id']}", apply_filters( 'gform_disable_notification', false, $notification, $form, $lead ), $notification, $form, $lead ) ) {
                //skip notifications if it has been disabled by a hook
                continue;
            }
            $notifications_to_send[] = $notification['id'];
        }
        GFCommon::send_notifications( $notifications_to_send, $form, $lead, true, $event );
    }

    $this->end( $status, $response );
}
1

1 Answers

1
votes

In your code, the $lead variable that you're sending to the notification functions hasn't been set anywhere (you just pulled it out of thin air :-) ). This variable needs to hold a Gravity Forms entry object which the notification functions will then be able to use to replace the variables.

Your $result from the GFAPI::add_entries will give you an array of entry IDs and you can use the GFAPI::get_entry function to get an entry object from an entry ID.