gravityflow_admin_actions_workflow_detail

Description

The gravityflow_admin_actions_workflow_detail filter can be used to add or remove choices from the admin actions drop down on the workflow detail page. If you use it to create a custom admin action, you must also hook onto gravityflow_admin_action_feedback filter to process custom choice(s).

Parameters

ParameterTypeDetails
$admin_actionsBooleanDefaults to true.
$current_stepBoolean or Gravity_Flow_StepThe current step.
$stepsArray of Gravity_Flow_StepsAn array of steps for the current form.
$formForm ObjectThe current form.
$entryEntry ObjectThe current entry.

Examples

Add a new choice to the admin actions drop down.

1
2
3
4
5
6
add_filter( 'gravityflow_admin_actions_workflow_detail', 'filter_admin_actions_workflow_detail', 10, 5 );
function filter_admin_actions_workflow_detail( $admin_actions, $current_step, $steps, $form, $entry ) {
    $admin_actions[] = array( 'label' => 'your new action', 'value' => 'your_new_action' );
 
    return $admin_actions;
}

Add a new choice to the admin actions drop down limited to display on completed entries.

1
2
3
4
5
6
7
add_filter( 'gravityflow_admin_actions_workflow_detail', 'filter_admin_actions_workflow_detail', 10, 5 );
function filter_admin_actions_workflow_detail( $admin_actions, $current_step, $steps, $form, $entry ) {
    if( gform_get_meta( $entry['id'], 'workflow_final_status' ) == 'complete') {
        $admin_actions[] = array( 'label' => 'Cancel Completed Workflow', 'value' => 'sh_cancel_completed_workflow' );
    }
    return $admin_actions;
}

Add a new optgroup to the admin actions drop down

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
add_filter( 'gravityflow_admin_actions_workflow_detail', 'admin_actions_add_optgroup', 10, 5 );
function admin_actions_add_optgroup( $admin_actions, $current_step, $steps, $form, $entry ) {
   $admin_actions[] = array(
      'label'   => esc_html__( 'Your custom optgroup:', 'gravityflow' ),
      'choices' => array(
         array(
            'label' => 'Choice 1',
            'value' => 'custom_optgroup|choice_1'
         ),
         array(
            'label' => 'Choice 2',
            'value' => 'custom_optgroup|choice_2'
         ),
      )
   );
 
   return $admin_actions;
}

Adjust the ‘Send to Step’ options to only show a subset

By default, the ‘Send to Step’ options include all active steps in a workflow. The following example shows how you can adjust the ‘Send to Step’ options to only show a subset based on if they meet their step conditions. Note that step conditions can also be customized through the gravityflow_step_is_condition_met filter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
add_filter( 'gravityflow_admin_actions_workflow_detail', 'jo_gravityflow_admin_actions_workflow_detail', 10, 5 );
function jo_gravityflow_admin_actions_workflow_detail( $admin_actions, $current_step, $steps, $form, $entry ) {
 
    foreach ( $admin_actions as $action_key => $action ) {
        //May need to adjust if you are using a multi-lingual site
        if ( isset( $action['choices'] ) && $action['label'] == 'Send to step:') {
            if ( count( $steps ) > 1 ) {
                $new_choices = array();                
                foreach ( $steps as $potential_step ) {
                    if ( ! $potential_step->is_active() || ! $potential_step->is_condition_met( $form ) ) {
                        continue;
                    }
                    $new_choices[] = array(
                        'label' => $potential_step->get_name(),
                        'value' => 'send_to_step|' . $potential_step->get_id()
                    );
                }
            }
            $admin_actions[ $action_key ]['choices'] = $new_choices;
        }
    }
    return $admin_actions;
}

Customize the drop-down based on the user role accessing the page

It either returns no options in the case of a non-administrator, or only a subset of options if it is an administrator. This example is more of a reference implementation that is meant to be customized to fit your your use case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
add_filter( 'gravityflow_admin_actions_workflow_detail', 'jo_filter_admin_actions_workflow_detail', 10, 5 );
function filter_admin_actions_workflow_detail( $admin_actions, $current_step, $steps, $form, $entry ) {
    $new_actions = array();
 
    $current_user = wp_get_current_user();
    if ( ! ( $current_user instanceof WP_User ) ) {
        return $new_actions;
    }
 
    if ( isset( $current_user->roles ) && in_array( 'administrator', $current_user->roles ) ) {
        $new_actions = array();
 
        //To specify a new custom action
        $new_actions[] = array( 'label' => 'Cancel Completed Workflow', 'value' => 'sh_custom_action_function' );
 
        //Or you could loop through existing $admin_actions to determine which ones you want to map to the new array
        foreach ( $admin_actions as $potential_action ) {
            //optgroups like Send to Step do not have value at top level, you may want to also check/adjust $potential_action['choices']
            if ( isset( $potential_action['value'] ) && in_array( $potential_action['value'], array( 'restart_step', 'restart_workflow' ) ) ) {
                $new_actions[] = $potential_action;
            }
        }
    }
    return $new_actions;
}

Placement

This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.

See also the PHP section in this article: Where Do I Put This Code?