You are here

protected function Oauth2ClientPluginBase::expandedProviderOptions in OAuth2 Client 8.3

Helper method to build the credential provider elements of the form.

Only needed if we have more than one provider. Currently supporting oauth2_client controlled local storage and Key module controlled optional storage.

Parameters

array $form: The configuration form.

1 call to Oauth2ClientPluginBase::expandedProviderOptions()
Oauth2ClientPluginBase::buildConfigurationForm in src/Plugin/Oauth2Client/Oauth2ClientPluginBase.php
Form constructor.

File

src/Plugin/Oauth2Client/Oauth2ClientPluginBase.php, line 440

Class

Oauth2ClientPluginBase
Base class for Oauth2Client plugins.

Namespace

Drupal\oauth2_client\Plugin\Oauth2Client

Code

protected function expandedProviderOptions(array &$form) {
  $provider = $this
    ->getCredentialProvider();
  $grantType = $this
    ->getGrantType();

  // Provide selectors for the api key credential provider.
  $form['credential_provider'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Credential provider'),
    '#default_value' => empty($provider) ? 'oauth2_client' : $provider,
    '#options' => [
      'oauth2_client' => $this
        ->t('Local storage'),
      'key' => $this
        ->t('Key module'),
    ],
    '#attributes' => [
      'data-states-selector' => 'provider',
    ],
    '#weight' => -99,
  ];
  $form['oauth2_client']['#states'] = [
    'required' => [
      ':input[data-states-selector="provider"]' => [
        'value' => 'oauth2_client',
      ],
    ],
    'visible' => [
      ':input[data-states-selector="provider"]' => [
        'value' => 'oauth2_client',
      ],
    ],
    'enabled' => [
      ':input[data-states-selector="provider"]' => [
        'value' => 'oauth2_client',
      ],
    ],
  ];
  $key_id = $provider == 'key' ? $this
    ->getStorageKey() : '';
  $form['key'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Managed by the Key module'),
    '#states' => [
      'required' => [
        ':input[data-states-selector="provider"]' => [
          'value' => 'key',
        ],
      ],
      'visible' => [
        ':input[data-states-selector="provider"]' => [
          'value' => 'key',
        ],
      ],
      'enabled' => [
        ':input[data-states-selector="provider"]' => [
          'value' => 'key',
        ],
      ],
    ],
    'id' => [
      '#type' => 'key_select',
      '#title' => $this
        ->t('Select a stored Key'),
      '#default_value' => $key_id,
      '#empty_option' => $this
        ->t('- Please select -'),
      '#key_filters' => [
        'type' => 'oauth2_client',
      ],
      '#description' => $this
        ->t('Select the key you have configured to hold the Oauth credentials.'),
    ],
  ];
  if ($grantType == 'resource_owner') {
    $form['key']['username'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Username'),
      '#description' => $this
        ->t('The username and password entered here are not saved, but are only used to request the token.'),
    ];
    $form['key']['password'] = [
      '#type' => 'password',
      '#title' => $this
        ->t('Password'),
    ];
  }
}