You are here

function hook_workflow_permitted_state_transitions_alter in Workflow 8

Same name and namespace in other branches
  1. 7.2 workflow.api.php \hook_workflow_permitted_state_transitions_alter()

Implements hook_workflow_permitted_state_transitions_alter().

This hook allows you to add custom filtering of allowed target states, add new custom states, change labels, etc. It is invoked in WorkflowState::getOptions().

Parameters

array $transitions: An array of allowed transitions from the current state (as provided in $context). They are already filtered by the settings in Admin UI.

array $context: An array of relevant objects. Currently:

$context = array(
  'user' => $user,
  'workflow' => $workflow,
  'state' => $current_state,
  'force' => $force,
);
1 call to hook_workflow_permitted_state_transitions_alter()
workflow_devel_workflow_permitted_state_transitions_alter in modules/workflow_devel/workflow_devel.module
@inheritdoc
1 function implements hook_workflow_permitted_state_transitions_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

workflow_devel_workflow_permitted_state_transitions_alter in modules/workflow_devel/workflow_devel.module
@inheritdoc
1 invocation of hook_workflow_permitted_state_transitions_alter()
WorkflowState::getTransitions in src/Entity/WorkflowState.php
Returns the allowed transitions for the current state.

File

./workflow.api.php, line 217
Hooks provided by the workflow module.

Code

function hook_workflow_permitted_state_transitions_alter(array &$transitions, array $context) {

  // workflow_debug(__FILE__, __FUNCTION__, __LINE__, '', '');
  // User may have the custom role AUTHOR.
  $user = $context['user'];

  // The following could be fetched from each transition.
  $workflow = $context['workflow'];
  $current_state = $context['state'];

  // The following could be fetched from the $user and $transition objects.
  $force = $context['force'];

  // Implement here own permission logic.
  foreach ($transitions as $key => $transition) {

    /** @var \Drupal\workflow\Entity\WorkflowTransitionInterface $transition*/
    if (!$transition
      ->isAllowed($user, $force)) {

      // unset($transitions[$key]);
    }
  }

  // This example creates a new custom target state.
  $values = [
    // Fixed values for new transition.
    'wid' => $context['workflow']
      ->id(),
    'from_sid' => $context['state']
      ->id(),
    // Custom values for new transition.
    // The ID must be an integer, due to db-table constraints.
    'to_sid' => '998',
    'label' => 'go to my new fantasy state',
  ];
  $new_transition = WorkflowConfigTransition::create($values);

  // $transitions[] = $new_transition;
}