You are here

public function WorkflowAccessRoleForm::buildRow in Workflow 8

File

modules/workflow_access/src/Form/WorkflowAccessRoleForm.php, line 58

Class

WorkflowAccessRoleForm
Provides the base form for workflow add and edit forms.

Namespace

Drupal\workflow_access\Form

Code

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

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

    // A list of role names keyed by role ID, including the 'author' role.
    // Only get the roles with proper permission + Author role.
    $type_id = $workflow
      ->id();
    $roles = workflow_get_user_role_names("create {$type_id} workflow_transition");
    if ($state
      ->isCreationState()) {

      // No need to set perms on creation.
      return [];
    }
    $view = $update = $delete = [];
    $count = 0;
    foreach (workflow_access_get_workflow_access_by_sid($sid) as $rid => $access) {
      $count++;
      $view[$rid] = $access['grant_view'] ? $rid : 0;
      $update[$rid] = $access['grant_update'] ? $rid : 0;
      $delete[$rid] = $access['grant_delete'] ? $rid : 0;
    }

    // Allow view grants by default for anonymous and authenticated users,
    // if no grants were set up earlier.
    if (!$count) {
      $view = [
        AccountInterface::ANONYMOUS_ROLE => AccountInterface::ANONYMOUS_ROLE,
        AccountInterface::AUTHENTICATED_ROLE => AccountInterface::AUTHENTICATED_ROLE,
      ];
    }
    $row['label_new'] = [
      '#type' => 'value',
      '#markup' => $this
        ->t('@label', [
        '@label' => $state
          ->label(),
      ]),
    ];
    $row['view'] = [
      '#type' => 'checkboxes',
      '#options' => $roles,
      '#default_value' => $view,
    ];
    $row['update'] = [
      '#type' => 'checkboxes',
      '#options' => $roles,
      '#default_value' => $update,
    ];
    $row['delete'] = [
      '#type' => 'checkboxes',
      '#options' => $roles,
      '#default_value' => $delete,
    ];
  }
  return $row;
}