You are here

function social_profile_privacy_form_user_form_alter in Open Social 10.3.x

Same name and namespace in other branches
  1. 10.0.x modules/social_features/social_profile/modules/social_profile_privacy/social_profile_privacy.module \social_profile_privacy_form_user_form_alter()
  2. 10.1.x modules/social_features/social_profile/modules/social_profile_privacy/social_profile_privacy.module \social_profile_privacy_form_user_form_alter()
  3. 10.2.x modules/social_features/social_profile/modules/social_profile_privacy/social_profile_privacy.module \social_profile_privacy_form_user_form_alter()

Implements hook_form_FORM_ID_alter().

File

modules/social_features/social_profile/modules/social_profile_privacy/social_profile_privacy.module, line 194
The Social profile privacy module file.

Code

function social_profile_privacy_form_user_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  /** @var \Drupal\Core\Entity\ContentEntityFormInterface $form_object */
  $form_object = $form_state
    ->getFormObject();
  if ($form_object
    ->getOperation() !== 'default') {
    return;
  }
  $config = \Drupal::config('social_profile_privacy.settings');
  $global_states = (array) $config
    ->get('fields');

  /** @var \Drupal\user\UserInterface $account */
  $account = $form_object
    ->getEntity();
  $form_state
    ->set('account_id', $uid = $account
    ->id());
  if ($value = $config
    ->get('disclaimer.value')) {
    $form['profile_privacy']['disclaimer'] = [
      '#type' => 'markup',
      '#markup' => check_markup($value, $config
        ->get('disclaimer.format')),
      '#weight' => -100,
    ];
  }
  $form['profile_privacy']['fields'] = [
    '#type' => 'container',
  ];

  /** @var \Drupal\user\UserDataInterface $user_data */
  $user_data = \Drupal::service('user.data');
  $user_states = $user_data
    ->get('social_profile_privacy', $uid, 'fields');

  /** @var \Drupal\social_profile_privacy\Service\SocialProfilePrivacyHelperInterface $helper */
  $helper = \Drupal::service('social_profile_privacy.helper');
  foreach ($helper
    ->getFieldOptions($account) as $field => $options) {
    $state = $global_states[$field] ?? SocialProfilePrivacyHelperInterface::SHOW;
    $value = $status = TRUE;
    switch ($state) {
      case SocialProfilePrivacyHelperInterface::SHOW:
        $status = FALSE;
        break;
      case SocialProfilePrivacyHelperInterface::CONFIGURABLE:
        if (isset($user_states[$field])) {
          $value = $user_states[$field];
        }
        break;
      case SocialProfilePrivacyHelperInterface::HIDE:
        $value = $status = FALSE;
        break;
    }
    if (!$options['access']) {
      $value = $status = FALSE;
    }
    $form['profile_privacy']['fields'][$field] = [
      '#type' => 'radios',
      '#title' => $options['label'],
      '#options' => [
        1 => t('Show'),
        0 => t('Hide'),
      ],
      '#default_value' => (int) $value,
      '#disabled' => !$status,
    ];
  }

  // Ensure submission happens first to be sure that that profile name will be
  // updated correctly. In another case, the last one can be updated after the
  // second submit only.
  array_unshift($form['actions']['submit']['#submit'], '_social_profile_privacy_fields_submit');
}