0

A client has requested that administrators be able to submit forms as other users. I've created an administrator form that has a dynamically populated drop-down of existing users filtered by roles. When the form is submitted, a pre-submission filter will take that value from the drop-down of users and alter the form data to reflect that the form was created by the selected user.

Reading the Gravity Forms docs lead me to these properties in the form object:

useCurrentUserAsAuthor bool For forms with Post fields, this determines if the post should be created using the current logged in user as the author. 1 to use the current user, 0 otherwise.

postAuthor integer When useCurrentUserAsAuthor is set to 0, this property contains the user Id that will be used as the Post author.

Based on this information I wrote this filter function:

function submit_as_user_proxy( $form )
{
    $id = null;
    $proxy_user_meta = null;
    $user_role = '';

    $field_existing_users = null;
    $field_signed_up_as = null;

    foreach ( $form['fields'] as &$field )
    {
        if ( false !== strpos( $field->cssClass, 'existing_users' ) )
        {
            $field_existing_users = $field;
        }
        if ( false !== strpos( $field->cssClass, 'signed_up_as' ) )
        {
            $field_signed_up_as = $field;
        }
    }

    $id = $_POST[ 'input_' . $field_existing_users->id ];
    $proxy_user_meta = get_userdata( $id );
    $roles = $proxy_user_meta->roles;
    if ( ! empty( $roles ) )
    {
        $user_role = $roles[0];
    }
    $_POST[ 'input_' . $field_signed_up_as->id ] = $user_role;

    $form['useCurrentUserAsAuthor'] = 0;
    $form['postAuthor'] = $id;

    return $form;
}

This is not working, the $_POST data is successfully changed, but the alterations to the form object are not reflected in the submission when viewed in entries.

Does anyone have any insight into this?

0

A solution I have found is to manipulate the entry after submission using the following:

function submit_as_user_proxy( $form )
{
    $id = null;
    $proxy_user_meta = null;
    $user_role = '';

    $field_existing_users = null;
    $field_signed_up_as = null;

    foreach ( $form['fields'] as &$field )
    {
        if ( false !== strpos( $field->cssClass, 'existing_users' ) )
        {
            $field_existing_users = &$field;
        }
        if ( false !== strpos( $field->cssClass, 'signed_up_as' ) )
        {
            $field_signed_up_as = &$field;
        }
    }

    $id = $_POST[ 'input_' . $field_existing_users->id ];
    $proxy_user_meta = get_userdata( $id );
    $roles = $proxy_user_meta->roles;
    if ( ! empty( $roles ) )
    {
        $user_role = $roles[0];
    }
    $_POST[ 'input_' . $field_signed_up_as->id ] = $user_role;

    $form['useCurrentUserAsAuthor'] = 0;
    $form['postAuthor'] = $id;

    add_action(
        'gform_after_submission_' . $form['id'],
        function( $entry ) use( $id )
        {
            $entry_id = $entry['id'];
            GFAPI::update_entry_property( $entry_id, 'created_by', $id );
            return $entry;
        }
    );

    return $form;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.