You are here

public function SimplesamlphpCustomAttributesEditForm::buildForm in SimpleSAMLphp Custom Attribute Mapping 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

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

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/SimplesamlphpCustomAttributesEditForm.php, line 57

Class

SimplesamlphpCustomAttributesEditForm

Namespace

Drupal\simplesamlphp_custom_attributes\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $mapping = NULL) {
  $saml_attribute = '';
  $field_name = '';
  if ($mapping !== NULL) {

    // Get the mappings from the settings.
    $mappings = $this->mappingConfig
      ->get('mappings');

    // Get this specific mapping attribute name and field name.
    $saml_attribute = $mappings[$mapping]['attribute_name'];
    $field_name = $mappings[$mapping]['field_name'];
  }

  // Get any fields that are not provided by core's user module. They could be
  // added via Field API, or as base fields on the user entity.
  $options = [
    'custom' => $this
      ->t('Custom'),
  ];
  $fields = $this->entityFieldManager
    ->getFieldDefinitions('user', 'user');
  foreach ($fields as $name => $field) {
    if ($field
      ->getFieldStorageDefinition()
      ->getProvider() != 'user') {
      $options[$name] = $field
        ->getLabel();
    }
  }
  $form['attribute_name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('SAML Attribute'),
    '#description' => $this
      ->t('The name of the SAML attribute you want to sync to the user profile.'),
    '#required' => TRUE,
    '#default_value' => $saml_attribute,
  ];
  $form['field_name'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('User Field'),
    '#description' => $this
      ->t('The user field you want to sync this attribute to.'),
    '#required' => TRUE,
    '#options' => $options,
    '#default_value' => $field_name,
  ];

  // Add this value so we know if it's an add or an edit.
  $form['mapping_id'] = [
    '#type' => 'hidden',
    '#value' => $mapping,
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Submit'),
  ];
  return $form;
}