You are here

function social_profile_privacy_form_social_profile_admin_settings_form_alter in Open Social 10.2.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_profile/modules/social_profile_privacy/social_profile_privacy.module \social_profile_privacy_form_social_profile_admin_settings_form_alter()
  2. 8 modules/social_features/social_profile/modules/social_profile_privacy/social_profile_privacy.module \social_profile_privacy_form_social_profile_admin_settings_form_alter()
  3. 8.2 modules/social_features/social_profile/modules/social_profile_privacy/social_profile_privacy.module \social_profile_privacy_form_social_profile_admin_settings_form_alter()
  4. 8.3 modules/social_features/social_profile/modules/social_profile_privacy/social_profile_privacy.module \social_profile_privacy_form_social_profile_admin_settings_form_alter()
  5. 8.4 modules/social_features/social_profile/modules/social_profile_privacy/social_profile_privacy.module \social_profile_privacy_form_social_profile_admin_settings_form_alter()
  6. 8.5 modules/social_features/social_profile/modules/social_profile_privacy/social_profile_privacy.module \social_profile_privacy_form_social_profile_admin_settings_form_alter()
  7. 8.6 modules/social_features/social_profile/modules/social_profile_privacy/social_profile_privacy.module \social_profile_privacy_form_social_profile_admin_settings_form_alter()
  8. 8.7 modules/social_features/social_profile/modules/social_profile_privacy/social_profile_privacy.module \social_profile_privacy_form_social_profile_admin_settings_form_alter()
  9. 8.8 modules/social_features/social_profile/modules/social_profile_privacy/social_profile_privacy.module \social_profile_privacy_form_social_profile_admin_settings_form_alter()
  10. 10.3.x modules/social_features/social_profile/modules/social_profile_privacy/social_profile_privacy.module \social_profile_privacy_form_social_profile_admin_settings_form_alter()
  11. 10.0.x modules/social_features/social_profile/modules/social_profile_privacy/social_profile_privacy.module \social_profile_privacy_form_social_profile_admin_settings_form_alter()
  12. 10.1.x modules/social_features/social_profile/modules/social_profile_privacy/social_profile_privacy.module \social_profile_privacy_form_social_profile_admin_settings_form_alter()

Implements hook_form_FORM_ID_alter().

File

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

Code

function social_profile_privacy_form_social_profile_admin_settings_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $config = \Drupal::config('social_profile_privacy.settings');

  // Add setting to hide Full Name for users without the `social profile privacy
  // always show full name` module.
  $form['privacy']['limit_search_and_mention'] = [
    '#type' => 'checkbox',
    '#title' => t('Limit search and mention'),
    '#description' => t("Enabling this setting causes users' full name to be hidden on the platform when the user has filled in their nickname. This setting won't hide the full name of users who didn't fill in a nickname. Users with the '%display_name' permission will still see the full name whenever available. Only users with the '%search_name' permission will find users using their full name through search or mentions.", [
      '%display_name' => t('View full name when restricted'),
      '%search_name' => t('View full name when restricted'),
    ]),
    '#default_value' => $config
      ->get('limit_search_and_mention'),
  ];
  $form['privacy']['fields'] = [
    '#type' => 'details',
    '#title' => t('Profile fields visibility'),
    '#description' => t('Please choose which profile fields can be displayed in user profiles. Site managers can always see all the filled-in profile information.'),
    '#open' => TRUE,
    '#tree' => TRUE,
  ];
  $actions = [
    SocialProfilePrivacyHelperInterface::SHOW => t('Always show for everyone'),
    SocialProfilePrivacyHelperInterface::CONFIGURABLE => t('Show, but can be hidden by each user'),
    SocialProfilePrivacyHelperInterface::HIDE => t('Hide for others'),
  ];
  $form['privacy']['fields']['list'] = [
    '#type' => 'table',
    '#header' => array_merge([
      t('Field name'),
    ], $actions),
  ];

  /** @var \Drupal\social_profile_privacy\Service\SocialProfilePrivacyHelperInterface $helper */
  $helper = \Drupal::service('social_profile_privacy.helper');

  // Fields options.
  $field_options = $helper
    ->getFieldOptions();
  foreach ($field_options as $field => $options) {
    $row = [
      [
        '#plain_text' => $options['label'],
      ],
    ];
    if ($options['access']) {
      $value = $config
        ->get('fields.' . $field) ?: SocialProfilePrivacyHelperInterface::SHOW;
    }
    else {
      $value = SocialProfilePrivacyHelperInterface::HIDE;
    }
    foreach ($actions as $state => $label) {
      $row[] = [
        '#type' => 'radio',
        '#title' => $label,
        '#return_value' => $state,
        '#default_value' => $value === $state ? $state : NULL,
        '#disabled' => !$options['access'],
        '#parents' => [
          'fields',
          $field,
        ],
      ];
    }
    $form['privacy']['fields']['list'][] = $row;
  }
  $form['privacy']['fields']['disclaimer'] = [
    '#type' => 'text_format',
    '#title' => t('Disclaimer'),
    '#default_value' => $config
      ->get('disclaimer.value'),
    '#format' => $config
      ->get('disclaimer.format'),
  ];

  // Add warning for site_manager to inform about updating all the profiles
  // after form is submitted.
  // We need to get Profile name fields.
  $account_name_fields = SocialProfileNameService::getProfileNameFields();

  // Var where will we stack the Profile name fields labels.
  $profile_name_fields_labels = [];

  // Getting the Profile name fields labels.
  foreach ($account_name_fields as $account_name_field) {
    if (isset($field_options[$account_name_field]) && !empty($label = $field_options[$account_name_field]['label'])) {
      $profile_name_fields_labels[] = $label;
    }
  }

  // We need to add a warning message only if the Profile name fields settings
  // available.
  if (!empty($profile_name_fields_labels)) {

    // Make string with a list of the Profile name fields labels.
    if (count($profile_name_fields_labels) > 1) {
      $last_label = array_pop($profile_name_fields_labels);
      $profile_name_fields_labels = implode(', ', $profile_name_fields_labels);
      $profile_name_fields_labels .= " and, {$last_label}";
    }
    else {
      $profile_name_fields_labels = $profile_name_fields_labels[0];
    }

    // Add warning message.
    $form['social_profile_privacy_warning_message'] = [
      '#theme' => 'status_messages',
      '#message_list' => [
        'warning' => [
          [
            '#type' => 'html_tag',
            '#tag' => 'strong',
            '#value' => t('If visibility settings of the fields %labels will be changed, then once you submit this form, all the user profiles on the platform will be updated.', [
              '%labels' => $profile_name_fields_labels,
            ]),
          ],
        ],
      ],
      '#status_headings' => [
        'warning' => t('Attention!'),
      ],
      '#weight' => -1,
    ];
  }
  $form['#submit'][] = 'social_profile_privacy_admin_settings_form_submit';
}