You are here

public function OpenIDConnectSettingsForm::buildForm in OpenID Connect / OAuth client 8

Same name and namespace in other branches
  1. 2.x src/Form/OpenIDConnectSettingsForm.php \Drupal\openid_connect\Form\OpenIDConnectSettingsForm::buildForm()

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 ConfigFormBase::buildForm

File

src/Form/OpenIDConnectSettingsForm.php, line 116

Class

OpenIDConnectSettingsForm
Provides the OpenID Connect settings form.

Namespace

Drupal\openid_connect\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $settings = $this
    ->configFactory()
    ->getEditable('openid_connect.settings');
  $form['#tree'] = TRUE;
  $form['clients_enabled'] = [
    '#title' => $this
      ->t('Enabled OpenID Connect clients'),
    '#description' => $this
      ->t('Choose enabled OpenID Connect clients.'),
    '#type' => 'checkboxes',
  ];
  $clients = $this
    ->getClients();
  $options = [];
  $clients_enabled = [];
  foreach ($clients as $client_plugin) {
    $plugin_definition = $client_plugin
      ->getPluginDefinition();
    $plugin_id = $plugin_definition['id'];
    $plugin_label = $plugin_definition['label'];
    $options[$plugin_id] = $plugin_label;
    $enabled = $this
      ->configFactory()
      ->getEditable('openid_connect.settings.' . $plugin_id)
      ->get('enabled');
    $clients_enabled[$plugin_id] = (bool) $enabled ? $plugin_id : 0;
    $element = 'clients_enabled[' . $plugin_id . ']';
    $form['clients'][$plugin_id] = [
      '#title' => $plugin_label,
      '#type' => 'fieldset',
      '#tree' => TRUE,
      '#states' => [
        'visible' => [
          ':input[name="' . $element . '"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['clients'][$plugin_id]['settings'] = [];
    $subform_state = SubformState::createForSubform($form['clients'][$plugin_id]['settings'], $form, $form_state);
    $form['clients'][$plugin_id]['settings'] += $client_plugin
      ->buildConfigurationForm($form['clients'][$plugin_id]['settings'], $subform_state);
  }
  $form['clients_enabled']['#options'] = $options;
  $form['clients_enabled']['#default_value'] = $clients_enabled;
  $form['override_registration_settings'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Override registration settings'),
    '#description' => $this
      ->t('If enabled, a user will be registered even if registration is set to "Administrators only".'),
    '#default_value' => $settings
      ->get('override_registration_settings'),
  ];
  $form['always_save_userinfo'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Save user claims on every login'),
    '#description' => $this
      ->t('If disabled, user claims will only be saved when the account is first created.'),
    '#default_value' => $settings
      ->get('always_save_userinfo'),
  ];
  $form['connect_existing_users'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Automatically connect existing users'),
    '#description' => $this
      ->t('If disabled, authentication will fail for existing email addresses.'),
    '#default_value' => $settings
      ->get('connect_existing_users'),
  ];
  $form['user_login_display'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('OpenID buttons display in user login form'),
    '#options' => [
      'hidden' => $this
        ->t('Hidden'),
      'above' => $this
        ->t('Above'),
      'below' => $this
        ->t('Below'),
      'replace' => $this
        ->t('Replace'),
    ],
    '#description' => $this
      ->t("Modify the user login form to show the the OpenID login buttons. If the 'Replace' option is selected, only the OpenID buttons will be displayed. In this case, pass the 'showcore' URL parameter to return to a password-based login form."),
    '#default_value' => $settings
      ->get('user_login_display'),
  ];
  $form['userinfo_mappings'] = [
    '#title' => $this
      ->t('User claims mapping'),
    '#type' => 'fieldset',
  ];
  $form['override_registration_settings'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Override registration settings'),
    '#description' => $this
      ->t('If enabled, user creation will always be allowed, even if the registration setting is set to require admin approval, or only allowing admins to create users.'),
    '#default_value' => $settings
      ->get('override_registration_settings'),
  ];
  $properties = $this->entityFieldManager
    ->getFieldDefinitions('user', 'user');
  $properties_skip = $this->openIDConnect
    ->userPropertiesIgnore();
  $claims = $this->claims
    ->getOptions();
  $mappings = $settings
    ->get('userinfo_mappings');
  foreach ($properties as $property_name => $property) {
    if (isset($properties_skip[$property_name])) {
      continue;
    }

    // Always map the timezone.
    $default_value = '';
    if ($property_name == 'timezone') {
      $default_value = 'zoneinfo';
    }
    $form['userinfo_mappings'][$property_name] = [
      '#type' => 'select',
      '#title' => $property
        ->getLabel(),
      '#description' => $property
        ->getDescription(),
      '#options' => (array) $claims,
      '#empty_value' => '',
      '#empty_option' => $this
        ->t('- No mapping -'),
      '#default_value' => isset($mappings[$property_name]) ? $mappings[$property_name] : $default_value,
    ];
  }
  return parent::buildForm($form, $form_state);
}