You are here

function socialbase_form_alter in Open Social 8.8

Same name and namespace in other branches
  1. 8.9 themes/socialbase/includes/form.inc \socialbase_form_alter()
  2. 8 themes/socialbase/includes/form.inc \socialbase_form_alter()
  3. 8.2 themes/socialbase/includes/form.inc \socialbase_form_alter()
  4. 8.3 themes/socialbase/includes/form.inc \socialbase_form_alter()
  5. 8.4 themes/socialbase/includes/form.inc \socialbase_form_alter()
  6. 8.5 themes/socialbase/includes/form.inc \socialbase_form_alter()
  7. 8.6 themes/socialbase/includes/form.inc \socialbase_form_alter()
  8. 8.7 themes/socialbase/includes/form.inc \socialbase_form_alter()

Implements hook_form_alter().

File

themes/socialbase/includes/form.inc, line 237
The form inc file for the Social base theme.

Code

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

  // Client side validation is not consistent accross devices. Disable for now.
  if (!isset($form['#attributes']['novalidate'])) {
    $form['#attributes']['novalidate'] = 'novalidate';
  }

  // In the code above we disable HTML5 native validation,
  // for the following forms enable validating.
  // These forms only have required attributes.
  $validate_forms = [
    'private_message_add_form',
    'comment_post_comment_form',
    'social_post_entity_form',
  ];
  if (in_array($form_id, $validate_forms)) {
    unset($form['#attributes']['novalidate']);
  }
  $social_group_types = [
    'open_group',
    'closed_group',
    'public_group',
  ];
  \Drupal::moduleHandler()
    ->alter('social_group_types', $social_group_types);
  $group_add_card_to_forms = [];
  $group_confirm_forms = [];
  $group_default_forms = [];
  $content_reporting_forms = [];
  foreach ($social_group_types as $social_group_type) {
    $group_add_card_to_forms[] = "group_content_{$social_group_type}-group_membership_group-join_form";
    $group_add_card_to_forms[] = "group_content_{$social_group_type}-group_membership_add_form";
    $group_add_card_to_forms[] = "group_content_{$social_group_type}-group_membership_edit_form";
    $group_confirm_forms[] = "group_content_{$social_group_type}-group_membership_group-leave_form";
    $group_confirm_forms[] = "group_content_{$social_group_type}-group_membership_delete_form";
    $group_confirm_forms[] = "group_{$social_group_type}_delete_form";
    $group_default_forms[] = "group_{$social_group_type}_add_form";
    $group_default_forms[] = "group_{$social_group_type}_edit_form";
  }

  // Add card classes to all the social_content_reporting forms if enabled.
  try {

    // Get all 'report_' flags.
    $report_types = \Drupal::service('social_content_report.content_report_service')
      ->getReportFlagTypes();
    foreach ($report_types as $report_type) {
      $content_reporting_forms[] = 'flagging_' . $report_type . '_add_form';
      $content_reporting_forms[] = 'flagging_' . $report_type . '_edit_form';
    }
  } catch (ServiceNotFoundException $e) {
  }

  // These forms have a container with class form-action at the bottom and
  // we style it in a way that the primary/submit button is aligned right
  // Also we define the style of the buttons based on their function.
  $default_forms = array_merge($group_add_card_to_forms, $group_confirm_forms, $group_default_forms, $content_reporting_forms, [
    'user_form',
    'user_pass_reset',
    'social_user_login_form',
    'user_register_form',
    'profile_profile_add_form',
    'profile_profile_edit_form',
    'social_user_password_form',
    'user_cancel_form',
    'social_group_add',
    'node_event_form',
    'node_topic_form',
    'comment_comment_delete_form',
    'comment_post_comment_delete_form',
    'comment_comment_delete_form',
    'post_delete_form',
    'post_photo_delete_form',
    'node_event_delete_form',
    'node_topic_delete_form',
    'profile_profile_delete_form',
    'private_message_thread_delete_form',
    'data_policy_edit_form',
    'data_policy_data_policy_agreement',
    'data_policy_data_policy_revision_edit',
    'event_an_enroll_form',
    'block_content_custom_content_list_form',
    'block_content_custom_content_list_edit_form',
  ]);
  if (in_array($form_id, $default_forms)) {
    $form['#attributes']['class'][] = 'form--default';
    if (isset($form['actions']['submit'])) {
      $form['actions']['submit']['#button_type'] = 'primary';
      $form['actions']['submit']['#button_level'] = 'raised';
    }
    if (isset($form['actions']['delete'])) {

      // Some `delete` buttons are not inputs but links.
      if (isset($form['actions']['delete']['#type']) && $form['actions']['delete']['#type'] == 'link') {
        $form['actions']['delete']['#attributes']['class'][] = 'btn btn-flat';
      }
      else {
        $form['actions']['delete']['#button_type'] = 'flat';
      }
    }
    if (isset($form['actions']['cancel'])) {

      // Some `cancel` buttons are not inputs but links.
      if (isset($form['actions']['cancel']['#type']) && $form['actions']['cancel']['#type'] == 'link') {
        $form['actions']['cancel']['#attributes']['class'][] = 'btn btn-flat';
      }
      else {
        $form['actions']['cancel']['#button_type'] = 'flat';
      }
    }
  }

  // Here we list all the forms that need to be wrapped in cards.
  $add_card_to_forms = array_merge($group_add_card_to_forms, [
    'event_an_enroll_form',
    'data_policy_edit_form',
    'data_policy_data_policy_agreement',
    'data_policy_data_policy_revision_edit',
    'views_bulk_operations_configure_action',
    'views_bulk_operations_confirm_action',
  ]);
  if (in_array($form_id, $add_card_to_forms)) {
    $form['#attributes']['class'][] = 'card';
    $form['actions']['#prefix'] = '</div></div>';
  }

  // The content reporting form also needs a card display except when rendered
  // in a dialog.
  if (in_array($form_id, $content_reporting_forms) && !\Drupal::request()
    ->isXmlHttpRequest()) {
    $form['#attributes']['class'][] = 'card';
    $form['actions']['#prefix'] = '</div></div>';
  }
  if (\Drupal::routeMatch()
    ->getRouteName() === 'private_message.private_message_create' && $form_id == 'private_message_add_form') {
    $form['#attributes']['class'][] = 'card';
    $form['#attributes']['class'][] = 'form--default';
    $form['actions']['#prefix'] = '</div></div>';
    if (isset($form['actions']['submit'])) {
      $form['actions']['submit']['#button_type'] = 'primary';
      $form['actions']['submit']['#button_level'] = 'raised';
    }
  }

  // List all the confirmation forms here. We need to close the card
  // before showing the actions.
  $confirm_forms = array_merge($group_confirm_forms, [
    'user_cancel_form',
    'post_delete_form',
    'post_photo_delete_form',
    'node_event_delete_form',
    'node_topic_delete_form',
    'comment_comment_delete_form',
    'comment_post_comment_delete_form',
    '_auth_login_notice_form',
    'private_message_thread_delete_form',
    'profile_profile_delete_form',
  ]);
  if (in_array($form_id, $confirm_forms)) {
    $form['description']['#prefix'] = '<div class="clearfix">';
    $form['description']['#suffix'] = '</div></div></div>';
  }

  // Add extra classes to.
  if ($form_id == 'enroll_action_form') {
    $form['enroll_for_this_event']['#button_type'] = 'accent';
    $form['enroll_for_this_event']['#attributes']['class'][] = 'btn-lg';
  }

  // Ensure the sign-up or login link is displayed as a nested card section.
  // Bootstrap already converted the fieldset itself to a card.
  if ($form_id === 'social_user_login_form') {
    $form['username_login']['sign-up-link']['#prefix'] = '<div class="card__nested-section">';
    $form['username_login']['sign-up-link']['#suffix'] = '</div>';
  }
  elseif ($form_id === 'user_register_form') {
    $form['account']['login-link']['#prefix'] = '<div class="card__nested-section">';
    $form['account']['login-link']['#suffix'] = '</div>';
  }
  elseif ($form_id === 'social_user_password_form') {
    $form['forgot']['sign-up-link']['#prefix'] = '<div class="card__nested-section">';
    $form['forgot']['sign-up-link']['#suffix'] = '</div>';
  }
}