You are here

protected function Keycloak::getGroupRuleTable in Keycloak OpenID Connect 8

Helper method returning user group mapping rules form array table.

Parameters

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array Form array definition for a draggable table of user group mapping rules.

1 call to Keycloak::getGroupRuleTable()
Keycloak::buildConfigurationForm in src/Plugin/OpenIDConnectClient/Keycloak.php
Form constructor.

File

src/Plugin/OpenIDConnectClient/Keycloak.php, line 499

Class

Keycloak
OpenID Connect client for Keycloak.

Namespace

Drupal\keycloak\Plugin\OpenIDConnectClient

Code

protected function getGroupRuleTable(FormStateInterface &$form_state) {
  $form = [];
  $form['keycloak_groups']['rules'] = [
    '#type' => 'table',
    '#title' => $this
      ->t('Group mapping rules'),
    '#prefix' => '<div id="keycloak-group-roles-replace">',
    '#suffix' => '</div>',
    '#header' => [
      '',
      $this
        ->t('Weight'),
      $this
        ->t('User role'),
      $this
        ->t('Action'),
      $this
        ->t('Evaluation type'),
      $this
        ->t('Pattern'),
      $this
        ->t('Case sensitive'),
      $this
        ->t('Enabled'),
      '',
    ],
    '#empty' => $this
      ->t('There are no rules yet.'),
    '#tableselect' => FALSE,
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'keycloak-groups-rules-weight',
      ],
    ],
  ];
  $roles = [
    'NONE' => '',
  ] + $this->roleMatcher
    ->getRoleOptions();
  $operations = $this->roleMatcher
    ->getEvalOperationOptions();

  // Get saved rules from configuration.
  $config_rules = $this->configuration['keycloak_groups']['rules'];

  // Create associative array of rules with rule id as keys.
  $rules = [];
  foreach ($config_rules as $rule) {
    $rules[$rule['id']] = $rule;
  }

  // Cross-check whether the rules are stored in the form state.
  $fs_rules = $form_state
    ->get('rules');
  if (empty($fs_rules)) {

    // Get the rule keys.
    $fs_rules = array_keys($rules);

    // Add a new item at the bottom.
    array_push($fs_rules, $this->uuid
      ->generate());

    // Remember these rows by IDs.
    $form_state
      ->set('rules', $fs_rules);
  }

  // For every rule add a row to our form.
  foreach ($fs_rules as $key) {
    $row = $this
      ->getGroupRuleRow($roles, $operations, isset($rules[$key]) ? $rules[$key] : [
      'id' => $key,
    ]);
    $form['keycloak_groups']['rules'][$key] = $row;
  }
  $form['keycloak_groups']['add'] = [
    '#type' => 'submit',
    '#name' => 'add',
    '#value' => $this
      ->t('Add rule'),
    '#submit' => [
      [
        $this,
        'addRuleSubmit',
      ],
    ],
    '#ajax' => [
      'callback' => [
        $this,
        'rulesAjaxCallback',
      ],
      'wrapper' => 'keycloak-group-roles-replace',
      'effect' => 'none',
    ],
  ];
  return $form;
}