You are here

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

Same name and namespace in other branches
  1. 2.x src/Form/OpenIDConnectAccountsForm.php \Drupal\openid_connect\Form\OpenIDConnectAccountsForm::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 FormInterface::buildForm

File

src/Form/OpenIDConnectAccountsForm.php, line 124

Class

OpenIDConnectAccountsForm
Provides the user-specific OpenID Connect settings form.

Namespace

Drupal\openid_connect\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, AccountInterface $user = NULL) {
  $form_state
    ->set('account', $user);
  $clients = $this->pluginManager
    ->getDefinitions();
  $form['help'] = [
    '#prefix' => '<p class="description">',
    '#suffix' => '</p>',
  ];
  if (empty($clients)) {
    $form['help']['#markup'] = $this
      ->t('No external account providers are available.');
    return $form;
  }
  elseif ($this->currentUser
    ->id() == $user
    ->id()) {
    $form['help']['#markup'] = $this
      ->t('You can connect your account with these external providers.');
  }
  $connected_accounts = $this->authmap
    ->getConnectedAccounts($user);
  foreach ($clients as $client) {
    $enabled = $this->configFactory
      ->getEditable('openid_connect.settings.' . $client['id'])
      ->get('enabled');
    if (!$enabled) {
      continue;
    }
    $form[$client['id']] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Provider: @title', [
        '@title' => $client['label'],
      ]),
    ];
    $fieldset =& $form[$client['id']];
    $connected = isset($connected_accounts[$client['id']]);
    $fieldset['status'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Status'),
      '#markup' => $this
        ->t('Not connected'),
    ];
    if ($connected) {
      $fieldset['status']['#markup'] = $this
        ->t('Connected as %sub', [
        '%sub' => $connected_accounts[$client['id']],
      ]);
      $fieldset['openid_connect_client_' . $client['id'] . '_disconnect'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Disconnect from @client_title', [
          '@client_title' => $client['label'],
        ]),
        '#name' => 'disconnect__' . $client['id'],
      ];
    }
    else {
      $fieldset['status']['#markup'] = $this
        ->t('Not connected');
      $fieldset['openid_connect_client_' . $client['id'] . '_connect'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Connect with @client_title', [
          '@client_title' => $client['label'],
        ]),
        '#name' => 'connect__' . $client['id'],
        '#access' => $this->currentUser
          ->id() == $user
          ->id(),
      ];
    }
  }
  return $form;
}