gravityflow_step_assignees

The gravityflow_step_assignees filter allows for the assignees of a step to be filtered.

Example #1: Adding a role-based assignee based on a field value

    add_filter( 'gravityflow_step_assignees', 'sh_step_assignees_role_field_value', 10, 2 );
    function sh_step_assignees_role_field_value( $assignees, $step ) {
       if ( gravity_flow()->is_workflow_detail_page() && $step->get_id() == 63 ) {
            $entry = $step->get_entry();
            //Add logic to confirm the entry value actually matches to a role within your site
            $new_role = str_replace( '-', '_', $entry['10'] );
            $args = array(
                'id'  => $new_role,
                'type' => 'role',
                'key' => 'role|' . $new_role,
                'editable_fields' => array( '1000' ),
            );
            $new_assignee = new Gravity_Flow_Assignee( $args, $step );
            $assignees[] = $new_assignee;
        }
        return $assignees;
    }

Apart from checking on the step id, we can also check on the step type ('approval' for instance).

add_filter( 'gravityflow_step_assignees', 'sh_step_assignees_role_field_value', 10, 2 );
function sh_step_assignees_role_field_value( $assignees, $step ) {
   if ( $step->step_type ) {
        $entry = $step->get_entry();
        //Add logic to confirm the entry value actually matches to a role within your site
        $new_role = str_replace( '-', '_', $entry['10'] );
        $args = array(
            'id'  => $new_role,
            'type' => 'role',
            'key' => 'role|' . $new_role,
            'editable_fields' => array( '1000' ),
        );
        $new_assignee = new Gravity_Flow_Assignee( $args, $step );
        $assignees[] = $new_assignee;
    }
    return $assignees;
}

Example #2: Remove current user from assignees if they created the entry.

The following snippet can be used to remove the current user from the list of assignees if the current user was the one who created the entry. The delisting of the current user is checked from all 3 possibilities - user_id, role, or email assignment.

add_filter( 'gravityflow_step_assignees', 'sh_step_assignees_role_field_value', 10, 2 );
function sh_step_assignees_role_field_value( $assignees, $step ) {
   if ( gravity_flow()->is_workflow_detail_page() && $step->get_id() == 822 ) {
        $entry = $step->get_entry();
        $created_by_user_id = $entry['created_by'];
        $current_user = wp_get_current_user();
        if ( $created_by_user_id == $current_user->id ) {
          $new_assignees = $assignees;
          foreach ( $assignees as $assignee ) {
            switch ( $assignee->get_type() ) {
              case 'user_id':
                $assignee_id = $assignee->get_id();
                if ( $assignee_id == $current_user->id ) {
                  $new_assignees = array_diff( $new_assignees, array( $assignee ) );
                }
              break;
              case 'role':
                $assignee_role = $assignee->get_id();
                if ( in_array( $assignee_role, $current_user->roles ) ) {
                  $new_assignees = array_diff( $new_assignees, array( $assignee ) );
                }
              break;
              case 'email':
                $assignee_email = $assignee->get_id();
                if ( $assignee_email == $current_user->user_email ) {
                  $new_assignees = array_diff( $new_assignees, array( $assignee ) );
                }
              break;
            }
          }
          $assignees = $new_assignees;
        }
    }
    return $assignees;
}
Example #3: Convert role-based assignee into users.

This filter ensures that the step is always assigned to every user of a given role - over ridding whatever assignee(s) are defined in the step settings.

add_filter( 'gravityflow_step_assignees', 'jo_step_assignees_by_user_role', 10, 2 );
function jo_step_assignees_by_user_role( $assignees, $step ) {

	//Modify this to match the step ID of your approval step that needs customization.
	$step_id_approval = 570;
	//Modify this to match the role which you want to have assignes derived from.
	$step_role        = 'administrator';

	if ( $step->get_id() == $step_id_approval ) {

		//Add logic to confirm the entry value actually matches to a role within your site
		$assignees = array();
		$users     = get_users( array( 'fields' => array( 'ID' ), 'role' => $step_role ) );

		foreach ( $users as $user ) {
			$args         = array(
				'name'            => 'generic',
				'id'              => $user->ID,
				'type'            => 'user_id',
				'key'             => 'user_id|' . $user->ID,
				'editable_fields' => array(),
			);
			$new_assignee = new Gravity_Flow_Assignee( $args, $step );
			$assignees[]  = $new_assignee;
		}
	}
	return $assignees;
}

Placement

This code should be placed in the functions.php file of your active theme.