You are here

protected function CasAttributesSettings::generateRoleMappingFormElements in CAS Attributes 8

Same name and namespace in other branches
  1. 2.x src/Form/CasAttributesSettings.php \Drupal\cas_attributes\Form\CasAttributesSettings::generateRoleMappingFormElements()

Generate a form elements for describing a role mapping.

Parameters

array $roleOptions: The available roles to map to.

array $existingData: Default data for each form element, if available.

Return value

array The form elements for the mapping.

1 call to CasAttributesSettings::generateRoleMappingFormElements()
CasAttributesSettings::buildForm in src/Form/CasAttributesSettings.php
Form constructor.

File

src/Form/CasAttributesSettings.php, line 221

Class

CasAttributesSettings
CAS Attributes settings form.

Namespace

Drupal\cas_attributes\Form

Code

protected function generateRoleMappingFormElements(array $roleOptions, array $existingData = []) {
  $elements = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Role Mapping'),
  ];
  $elements['rid'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Role to Assign'),
    '#options' => $roleOptions,
  ];
  $elements['attribute'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Attribute Name'),
    '#description' => $this
      ->t('See a <a href="@link">list of available attributes</a> for the currently logged in user (do not provide a token here, use the actual attribute name).', [
      '@link' => Url::fromRoute('cas_attributes.available_attributes')
        ->toString(),
    ]),
    '#size' => 30,
  ];
  $elements['value'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Attribute Value'),
    '#size' => 30,
  ];
  $elements['method'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Comparison Method'),
    '#options' => [
      'exact_single' => $this
        ->t('Exact (Single)'),
      'exact_any' => $this
        ->t('Exact (Any)'),
      'contains_any' => $this
        ->t('Contains'),
      'regex_any' => $this
        ->t('Regex'),
    ],
    '#description' => $this
      ->t("\n        The 'Exact (Single)' method passes if the attribute value has one value only and it matches the given string exactly.\n        The 'Exact (Any)' method passes if any item in the attribute value array matches the given string exactly.\n        The 'Contains' method passes if any item in the attribute value array contains the given string within it anywhere.\n        The 'Regex' method passes if any item in the attribute value array passes the regular expression provided.\n      "),
  ];
  $elements['negate'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Negate'),
    '#description' => $this
      ->t('When checked, the specified role will be applied to the user if the attribute comparison fails to match. This can be useful if you want to assign a role based on the lack of some attribute value.'),
  ];
  $elements['remove_without_match'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Remove role from user if match fails?'),
    '#description' => $this
      ->t('IMPORTANT! If enabled, this will also remove the role if it was manually assigned to the user.'),
  ];
  if (!empty($existingData)) {
    foreach ($existingData as $key => $val) {
      $elements[$key]['#default_value'] = $val;
    }
  }
  return $elements;
}