You are here

public function SamlauthMappingListForm::listMappings in SAML Authentication 8.3

Same name and namespace in other branches
  1. 4.x modules/samlauth_user_fields/src/Form/SamlauthMappingListForm.php \Drupal\samlauth_user_fields\Form\SamlauthMappingListForm::listMappings()

Returns the list of attribute-field mappings.

Parameters

array $mappings: The attribute-field mappings.

Return value

array A renderable content array.

1 call to SamlauthMappingListForm::listMappings()
SamlauthMappingListForm::buildForm in modules/samlauth_user_fields/src/Form/SamlauthMappingListForm.php
Form for adding or editing a mapping.

File

modules/samlauth_user_fields/src/Form/SamlauthMappingListForm.php, line 143

Class

SamlauthMappingListForm
Displays the list of attribute-field mappings; edits related configuration.

Namespace

Drupal\samlauth_user_fields\Form

Code

public function listMappings(array $mappings) {
  $linking_enabled = $this
    ->configFactory()
    ->get(SamlController::CONFIG_OBJECT_NAME)
    ->get('map_users');
  $output['table'] = [
    '#theme' => 'table',
    '#header' => [
      $this
        ->t('SAML Attribute'),
      $this
        ->t('User Field'),
      $this
        ->t('Operations'),
    ],
    '#sticky' => TRUE,
    '#empty' => $this
      ->t("There are no mappings. You can add one using the link above."),
  ];
  if ($linking_enabled) {
    array_splice($output['table']['#header'], 2, 0, [
      $this
        ->t('Use for linking'),
    ]);
  }
  if ($mappings) {
    $fields = $this->entityFieldManager
      ->getFieldDefinitions('user', 'user');

    // We're identifying individual mappings by their numeric indexes in the
    // configuration value (which is defined as a 'sequence' in the config
    // schema). These are not renumbered while saving a mapping, so the
    // danger of using them is acceptable. (URLs would only pointing to a
    // different mapping if we delete the highest numbered mapping and re-add
    // one. Maybe things are renumbered arter exporting configuration, I
    // haven't tested, but that's also an acceptable risk.)
    foreach ($mappings as $id => $mapping) {
      $operations = [
        '#type' => 'dropbutton',
        '#links' => [
          'edit' => [
            'title' => $this
              ->t('edit'),
            'url' => Url::fromRoute('samlauth_user_fields.edit', [
              'mapping_id' => $id,
            ]),
          ],
          'delete' => [
            'title' => $this
              ->t('delete'),
            'url' => Url::fromRoute('samlauth_user_fields.delete', [
              'mapping_id' => $id,
            ]),
          ],
        ],
      ];
      $real_field_name = strstr($mapping['field_name'], ':', TRUE);
      if ($real_field_name) {
        $sub_field_name = substr($mapping['field_name'], strlen($real_field_name) + 1);
        if (isset($fields[$real_field_name])) {
          $property_definitions = $fields[$real_field_name]
            ->getFieldStorageDefinition()
            ->getPropertyDefinitions();
          if (isset($property_definitions[$sub_field_name]) && $property_definitions[$sub_field_name] instanceof DataDefinition) {
            $sub_field_name = $property_definitions[$sub_field_name]
              ->getLabel();
          }
        }
      }
      else {
        $real_field_name = $mapping['field_name'];
        $sub_field_name = '';
      }
      $user_field = (isset($fields[$real_field_name]) ? $fields[$real_field_name]
        ->getLabel() : $this
        ->t('Unknown field %name', [
        '%name' => $real_field_name,
      ])) . ($sub_field_name ? ": {$sub_field_name}" : '');
      $output['table']['#rows'][$id] = [
        $mapping['attribute_name'],
        $user_field,
        render($operations),
      ];
      if ($linking_enabled) {
        array_splice($output['table']['#rows'][$id], 2, 0, [
          $mapping['link_user_order'] ?? '',
        ]);
      }
    }
  }
  return $output;
}