You are here

function bakery_form_alter in Bakery Single Sign-On System 8.2

Same name and namespace in other branches
  1. 6.2 bakery.module \bakery_form_alter()
  2. 6 bakery.module \bakery_form_alter()
  3. 7.4 bakery.module \bakery_form_alter()
  4. 7.2 bakery.module \bakery_form_alter()
  5. 7.3 bakery.module \bakery_form_alter()

Implements hook_form_alter().

File

./bakery.module, line 54
For implementing different hooks for bakery SSO functionality.

Code

function bakery_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  switch ($form_id) {
    case 'user_profile_form':
    case 'user_form':
      $config = \Drupal::config('bakery.settings');

      /** @var \Drupal\bakery\BakeryService $bakery */
      $bakery = \Drupal::service('bakery.bakery_service');
      if (!\Drupal::currentUser()
        ->hasPermission('administer users') && $bakery
        ->isChild()) {
        $master_uri = $config
          ->get('bakery_master');

        // $init_url = _bakery_init_field_url($form['#user']->init);.
        if (isset($form['account'])) {
          \Drupal::messenger()
            ->addStatus(t('You can change the name, mail, and password <a href=":url">at the master site</a>.', [
            ':url' => $master_uri,
          ]), FALSE);
          $form['account']['#access'] = FALSE;
          $form['account']['name']['#access'] = FALSE;
          $form['account']['pass']['#access'] = FALSE;
          $form['account']['mail']['#access'] = FALSE;
        }
        foreach (\Drupal::config('bakery.settings')
          ->get('bakery_supported_fields') as $type => $value) {
          if ($value) {
            switch ($type) {
              case 'mail':
              case 'name':
                break;
              case 'picture':
                if (isset($form['picture'])) {
                  $form['picture']['picture_delete']['#access'] = FALSE;
                  $form['picture']['picture_upload']['#access'] = FALSE;
                  $form['picture']['#description'] = t('You can change the image <a href=":url">at the master site</a>.', [
                    ':url' => $master_uri,
                  ]);
                }
                break;
              case 'language':
                if (isset($form['locale'][$type])) {
                  $form['locale'][$type]['#disabled'] = TRUE;
                  $form['locale'][$type]['#description'] .= ' ' . t('You can change the language setting <a href=":url">at the master site</a>.', [
                    ':url' => $master_uri,
                  ]);
                }
                break;
              case 'signature':
                if (isset($form['signature_settings'][$type])) {
                  $form['signature_settings'][$type]['#disabled'] = TRUE;
                  $form['signature_settings'][$type]['#description'] .= ' ' . t('You can change the signature <a href=":url">at the master site</a>.', [
                    ':url' => $master_uri,
                  ]);
                }
                break;
              default:
                if (isset($form[$type])) {
                  $form[$type]['#disabled'] = TRUE;
                }
                if (isset($form[$type][$type])) {
                  $form[$type][$type]['#disabled'] = TRUE;
                  $form[$type][$type]['#description'] .= ' ' . t('You can change this setting <a href=":url">at the master site</a>.', [
                    ':url' => $master_uri,
                  ]);
                }
                break;
            }
          }
        }
      }
      break;
    case 'user_login_form':
      $config = \Drupal::config('bakery.settings');
      if ($config
        ->get('bakery_is_master')) {

        // Use both validate and submit, in case other modules like TFA are
        // also altering the login process.
        $form['#validate'][] = '_bakery_login_redirect';
        $form['#submit'][] = '_bakery_login_redirect';
      }
      else {
        if ($config
          ->get('subsite_login')) {

          // TODO allow login...
          // Replace two validators from user module because they log the user in
          // and test if account exists. We want to check if the account exists on
          // the master instead.
          // dpm($form['#validate']);.
          $form['#validate'] = array_diff($form['#validate'], [
            '::validateAuthentication',
            '::validateFinal',
          ]);

          // Also replace the submit handler with our own to
          // set a redirect cookie.
          $form['#submit'] = [
            '_bakery_login_submit',
          ];
        }
        else {

          // Remove all form elements and just link to main site for login.
          // We _could_ redirect but because this form is shared with the login
          // block in 8+ this would lead to some really weird behaviors.
          $link = Link::fromTextAndUrl('Log in on main site', Url::fromUri($config
            ->get('bakery_master') . 'user', [
            'query' => [
              'bd' => \Drupal::getContainer()
                ->get('bakery.redirect')
                ->get(),
            ],
          ])
            ->setAbsolute(TRUE))
            ->toRenderable();
          unset($form['name'], $form['pass'], $form['actions']);
          $link['#cache']['tags'] = [
            'config:bakery.settings',
          ];
          $form['login'] = $link;
        }
      }
      break;
  }
}