You are here

public function WorkflowConfigTransitionRoleForm::buildRow in Workflow 8

Builds a row for the following table: Transitions, for example: 18 => [ 20 => [ 'author' => 1, 1 => 0, 2 => 1, ] ] means the transition from state 18 to state 20 can be executed by the content author or a user in role 2. The $transitions array should contain ALL transitions for the workflow.

File

src/Form/WorkflowConfigTransitionRoleForm.php, line 63

Class

WorkflowConfigTransitionRoleForm
Defines a class to build a listing of Workflow Config Transitions entities.

Namespace

Drupal\workflow\Form

Code

public function buildRow(EntityInterface $entity) {
  $row = [];
  $workflow = $this->workflow;
  if ($workflow) {

    // Each $entity is a from-state.

    /** @var \Drupal\workflow\Entity\WorkflowState $entity */
    $from_state = $entity;
    $from_sid = $from_state
      ->id();

    /** @var \Drupal\workflow\Entity\WorkflowState[] $states */
    $states = $workflow
      ->getStates($all = 'CREATION');
    if ($states) {

      // Only get the roles with proper permission + Author role.
      $type_id = $workflow
        ->id();
      $roles = workflow_get_user_role_names("create {$type_id} workflow_transition");

      // Prepare default value for 'stay_on_this_state'.
      // array_combine(array_keys($roles), array_keys($roles));
      $allow_all_roles = [];

      /** @var \Drupal\workflow\Entity\WorkflowState $state */
      foreach ($states as $state) {
        $row['to'] = [
          '#type' => 'value',
          '#markup' => $this
            ->t('@label', [
            '@label' => $from_state
              ->label(),
          ]),
        ];

        /** @var \Drupal\workflow\Entity\WorkflowState $to_state */
        foreach ($states as $to_state) {

          // Don't allow transition TO (creation).
          if ($to_state
            ->isCreationState()) {
            continue;
          }

          // Only allow transitions from $from_state.
          if ($state
            ->id() != $from_state
            ->id()) {
            continue;
          }
          $to_sid = $to_state
            ->id();

          // Load existing config_transitions. Create if not found.
          $config_transitions = $workflow
            ->getTransitionsByStateId($from_sid, $to_sid);
          if (!($config_transition = reset($config_transitions))) {
            $config_transition = $workflow
              ->createTransition($from_sid, $to_sid);
          }
          $stay_on_this_state = !$config_transition
            ->hasStateChange();
          $row[$to_sid]['workflow_config_transition'] = [
            '#type' => 'value',
            '#value' => $config_transition,
          ];
          $row[$to_sid]['roles'] = [
            '#type' => 'checkboxes',
            '#options' => $stay_on_this_state ? [] : $roles,
            '#disabled' => $stay_on_this_state,
            '#default_value' => $stay_on_this_state ? $allow_all_roles : $config_transition->roles,
          ];
        }
      }
    }
  }
  return $row;
}