You are here

function social_profile_form_profile_profile_edit_form_alter in Open Social 8.3

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

Implements hook_form_FORM_ID_alter().

File

modules/social_features/social_profile/social_profile.module, line 90
The Social profile module.

Code

function social_profile_form_profile_profile_edit_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  $viewing_user = \Drupal::currentUser();

  /** @var \Drupal\profile\Entity\Profile $profile */
  $profile = $form_state
    ->getFormObject()
    ->getEntity();

  // Check for permission on custom edit profile tags, it's only for CM+ who can
  // actually edit a users profile and add profile tags there.
  if (!$viewing_user
    ->hasPermission('edit profile tags')) {
    $form['field_profile_profile_tag']['#access'] = FALSE;
  }

  // We hide the label and form field when there are no options. This is the
  // case by default. Since we provide an empty vocabulary.
  // We also check for a fieldset, used by our special widget which doesn't use
  // #options.
  if (empty($form['field_profile_profile_tag']['widget']['#options']) && $form['field_profile_profile_tag']['widget']['#type'] !== 'container') {
    unset($form['field_profile_profile_tag']);
  }

  // Remove the save and set default submit button on profile edit.
  if (isset($form['actions']['set_default'])) {
    unset($form['actions']['set_default']);
  }
  if (isset($form['field_profile_expertise']['widget']['target_id'])) {
    $form['field_profile_expertise']['widget']['target_id']['#selection_settings']['hide_id'] = TRUE;
  }
  if (isset($form['field_profile_interests']['widget']['target_id'])) {
    $form['field_profile_interests']['widget']['target_id']['#selection_settings']['hide_id'] = TRUE;
  }

  // Check if the Social Profile Fields module is on.
  if (\Drupal::moduleHandler()
    ->moduleExists('social_profile_fields')) {

    // Load the profile fields and check if at least one of them can be changed.
    if ($profile_fields = \Drupal::entityManager()
      ->getStorage('field_config')
      ->loadByProperties([
      'entity_type' => 'profile',
      'bundle' => 'profile',
    ])) {
      $empty_profile = TRUE;

      /** @var \Drupal\field\Entity\FieldConfig $field_config */
      foreach ($profile_fields as $field) {
        if (isset($form[$field
          ->get('field_name')]) && $form[$field
          ->get('field_name')]['#access'] === TRUE) {
          $empty_profile = FALSE;
        }
      }

      // There are no fields the user can edit here. Set an explanatory message
      // and disable the Save button.
      if ($empty_profile === TRUE) {
        $form['empty_profile'] = [
          '#type' => 'fieldset',
        ];

        // If a user is viewing their own profile, suggest they change settings.
        if ($viewing_user
          ->id() === $profile
          ->getOwnerId()) {
          $settings_link = Link::createFromRoute(t('account settings here'), 'entity.user.edit_form', [
            'user' => $profile
              ->getOwnerId(),
          ])
            ->toString();
          $form['empty_profile']['notice'] = [
            '#type' => 'html_tag',
            '#tag' => 'p',
            '#value' => t('There is no profile information you can change here. Change your @settings.', [
              '@settings' => $settings_link,
            ]),
          ];
        }
        else {
          $form['empty_profile']['notice'] = [
            '#type' => 'html_tag',
            '#tag' => 'p',
            '#value' => t('There is no profile information you can change here.'),
          ];
        }
        $form['actions']['submit']['#disabled'] = TRUE;
      }
    }
  }

  // Add custom submit handler just for redirect purposes. We don't want to
  // override the form::save in profile.
  $form['actions']['submit']['#submit'][] = '_social_profile_profile_edit_form_submit';

  // Add cancel button, so user can easily navigate form edit form to profile.
  $form['actions']['cancel'] = [
    '#type' => 'link',
    '#title' => t('Cancel'),
    '#url' => Url::fromRoute('view.user_information.user_information', [
      'user' => $profile
        ->getOwnerId(),
    ]),
  ];
}