You are here

function socialbase_form_alter in Open Social 8

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

Implements hook_form_FORM_ID_alter().

File

themes/socialbase/includes/form.inc, line 220
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']);
  }

  // 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 = [
    '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',
    'group_open_group_add_form',
    'group_open_group_edit_form',
    'group_closed_group_add_form',
    'group_closed_group_edit_form',
    'group_public_group_add_form',
    'group_public_group_edit_form',
    'node_event_form',
    'node_topic_form',
    'group_content_open_group-group_membership_group-join_form',
    'group_content_open_group-group_membership_group-leave_form',
    'group_content_open_group-group_membership_add_form',
    'group_content_open_group-group_membership_edit_form',
    'group_content_open_group-group_membership_delete_form',
    'group_content_closed_group-group_membership_group-join_form',
    'group_content_closed_group-group_membership_group-leave_form',
    'group_content_closed_group-group_membership_add_form',
    'group_content_closed_group-group_membership_edit_form',
    'group_content_closed_group-group_membership_delete_form',
    'group_content_public_group-group_membership_group-join_form',
    'group_content_public_group-group_membership_group-leave_form',
    'group_content_public_group-group_membership_add_form',
    'group_content_public_group-group_membership_edit_form',
    'group_content_public_group-group_membership_delete_form',
    'group_open_group_delete_form',
    'group_closed_group_delete_form',
    'group_public_group_delete_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',
  ];
  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 = [
    'group_content_open_group-group_membership_group-join_form',
    'group_content_open_group-group_membership_add_form',
    'group_content_open_group-group_membership_edit_form',
    'group_content_closed_group-group_membership_group-join_form',
    'group_content_closed_group-group_membership_add_form',
    'group_content_closed_group-group_membership_edit_form',
    'social_user_login_form',
    'user_register_form',
    'user_pass_reset',
    'social_user_password_form',
  ];
  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';
    }
  }
  if (in_array($form_id, $add_card_to_forms)) {
    $form['#attributes']['class'][] = 'card';
    $form['actions']['#prefix'] = '</div></div>';
  }

  // List all the confirmation forms here. We need to close the card
  // before showing the actions.
  $confirm_forms = [
    'user_cancel_form',
    'post_delete_form',
    'post_photo_delete_form',
    'node_event_delete_form',
    'node_topic_delete_form',
    'group_open_group_delete_form',
    'group_closed_group_delete_form',
    'comment_comment_delete_form',
    'comment_post_comment_delete_form',
    'group_content_open_group-group_membership_delete_form',
    'group_content_open_group-group_membership_group-leave_form',
    'group_content_closed_group-group_membership_delete_form',
    'group_content_closed_group-group_membership_group-leave_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';
  }
}