You are here

private function SamlauthMappingEditForm::getSubFields in SAML Authentication 8.3

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

Checks if the field has multiple columns that we can map values into.

This starts off as a private method with some hardcoded logic because I'm not sure how this will evolve and if it will be general enough.

Parameters

\Drupal\Core\Field\FieldDefinitionInterface $field: The field to check.

Return value

array All columns (as keys, with the label as values) inside this field which can be mapped - or an empty array if the field is a 'simple' field with just one mappable value column.

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

File

modules/samlauth_user_fields/src/Form/SamlauthMappingEditForm.php, line 258

Class

SamlauthMappingEditForm
Form for adding a mapped SAML attribute -> user field.

Namespace

Drupal\samlauth_user_fields\Form

Code

private function getSubFields(FieldDefinitionInterface $field) {

  // Hardcode for address only. It is possible that the below code is general
  // enough for all field types, but I don't know that for sure. I don't want
  // field types that used to be treated as single-value to return an array
  // here, thereby losing compatibility with previous module versions.
  if ($field
    ->getType() !== 'address') {
    return [];
  }

  // A FieldDefinitionInterface does not necessarily have
  // getPropertyDefinitions() and getSchema(). (The basic user fields do,
  // because they extend BaseFieldDefinition which implements both
  // FieldDefinitionInterface and FieldStorageDefinitionInterface.)
  $storage_definition = $field
    ->getFieldStorageDefinition();
  $property_definitions = $storage_definition
    ->getPropertyDefinitions();
  $schema = $storage_definition
    ->getSchema();

  // Not sure which fields we should support; start out with just varchar
  // (which is all fields in case of type 'address'). Also not sure if we
  // want to filter out the address subfields that are set to "invisible"; I
  // guess/hope not.
  $columns = array_filter($schema['columns'], function ($column) {
    return ($column['type'] ?? NULL) === 'varchar';
  });
  $subfields = [];
  if ($columns) {
    foreach (array_keys($columns) as $column_name) {
      if (isset($property_definitions[$column_name]) && $property_definitions[$column_name] instanceof DataDefinition) {
        $subfields[$column_name] = $property_definitions[$column_name]
          ->getLabel();
      }
      else {
        $subfields[$column_name] = $column_name;
      }
    }
  }
  return $subfields;
}