You are here

function simple_oauth_form_consumer_form_alter in Simple OAuth (OAuth2) & OpenID Connect 5.x

Same name and namespace in other branches
  1. 8.4 simple_oauth.module \simple_oauth_form_consumer_form_alter()
  2. 8.3 simple_oauth.module \simple_oauth_form_consumer_form_alter()

Implements hook_form_FORM_ID_alter().

File

./simple_oauth.module, line 154
Contains simple_oauth.module.

Code

function simple_oauth_form_consumer_form_alter(array &$form, FormStateInterface $form_state, $form_id) {

  // Add a custom submit behavior.
  $form['#entity_builders'][] = 'simple_oauth_form_consumer_form_submit';
  $entity_type_manager = \Drupal::service('entity_type.manager');

  // Remove automatic roles and administrator roles.
  unset($form['roles']['widget']['#options'][RoleInterface::ANONYMOUS_ID]);
  unset($form['roles']['widget']['#options'][RoleInterface::AUTHENTICATED_ID]);

  // Get the admin role.
  $admin_roles = $entity_type_manager
    ->getStorage('user_role')
    ->getQuery()
    ->condition('is_admin', TRUE)
    ->execute();
  $default_value = reset($admin_roles);
  unset($form['roles']['widget']['#options'][$default_value]);
  $recommendation_text = t('Create a <a href=":url">role</a> for every logical group of permissions you want to make available to a consumer.', [
    ':url' => Url::fromRoute('entity.user_role.collection')
      ->toString(),
  ]);
  $form['roles']['widget']['#description'] .= '<br>' . $recommendation_text;
  if (empty($form['roles']['widget']['#options'])) {
    \Drupal::service('messenger')
      ->addMessage($recommendation_text, 'error');
    $form['actions']['#disabled'] = TRUE;
  }
  $description = t('Use this field to create a hash of the secret key. This module will never store your consumer key, only a hash of it.');
  $form['new_secret'] = [
    '#type' => 'password',
    '#title' => t('New Secret'),
    '#description' => $description,
  ];
  $form['pkce']['#states']['visible'] = [
    ':input[name="confidential[value]"]' => [
      'checked' => FALSE,
    ],
  ];
}