You are here

function social_group_form_alter in Open Social 8

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

Implements hook_form_alter().

File

modules/social_features/social_group/social_group.module, line 389
The Social group module.

Code

function social_group_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $action_forms = [
    '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_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_delete_form',
  ];

  // Perform alterations on joining / leaving groups.
  if (in_array($form_id, [
    'group_content_open_group-group_membership_delete_form',
    'group_content_closed_group-group_membership_delete_form',
  ])) {
    $form['actions']['submit']['#submit'][] = '_social_membership_delete_form_submit';
    list($name, $group) = _social_group_get_group_labels();

    // Give a better title, description and submit button value.
    $form['#title'] = t('Remove %name from %group', [
      '%name' => $name,
      '%group' => $group,
    ]);
    $form['actions']['submit']['#value'] = t('Remove');
    $form['description']['#markup'] = t('Are you sure you want to remove %name from %group?', [
      '%name' => $name,
      '%group' => $group,
    ]);
  }

  // Set some helpful text on the group join form now it's there.
  if (in_array($form_id, [
    'group_content_closed_group-group_membership_group-join_form',
    'group_content_open_group-group_membership_group-join_form',
  ])) {
    $form['help'] = [
      '#type' => 'item',
      '#markup' => t('By submitting this form you will become a member of the group. Please fill out any available fields to complete your membership information.'),
    ];
    $form['path']['#type'] = 'hidden';
  }

  // Perform alterations on joining / leaving groups.
  if (in_array($form_id, $action_forms)) {

    // Add cancel option on join and leave form.
    $form['actions']['cancel'] = [
      '#type' => 'submit',
      '#value' => t('Cancel'),
      '#submit' => [
        '_social_group_cancel_join_leave_form',
      ],
      '#limit_validation_errors' => [],
    ];
    $form['actions']['submit']['#submit'][] = '_social_group_action_form_submit';
  }
  $membership_forms = [
    'group_content_open_group-group_membership_edit_form',
    'group_content_open_group-group_membership_add_form',
    'group_content_closed_group-group_membership_add_form',
    'group_content_closed_group-group_membership_edit_form',
  ];
  if (in_array($form_id, $membership_forms) && Drupal::currentUser()
    ->id() != 1) {

    // Change titles on membership forms.
    $form['entity_id']['widget'][0]['target_id']['#title'] = t('Select a member');
    $form['group_roles']['widget']['#title'] = t('Group roles');

    // Remove the 'group_admin' role in a generic way
    // for all (future) group types.
    foreach ($form['group_roles']['widget']['#options'] as $key => $value) {

      // Hide the submission for the Group Admin role.
      if (strpos($key, 'group_admin') != FALSE) {
        unset($form['group_roles']['widget']['#options'][$key]);
      }
    }
    $form['path']['#type'] = 'hidden';
    if ('group_content_open_group-group_membership_edit_form' == $form_id) {

      // Remove the user selection autocomplete on editing group membership.
      $form['entity_id']['#access'] = FALSE;

      // Add redirect to Group Membership page.
      $form['actions']['submit']['#submit'][] = '_social_group_membership_edit_form_submit';
    }
  }

  // Check if form is group content create form.
  if (isset($form['#entity_type']) && $form['#entity_type'] === 'node') {
    $group = _social_group_get_current_group();
    if (!empty($group)) {

      // Add custom submit handler just for redirect purposes.
      // We don't want to override the form::save in group.
      $form['actions']['submit']['#submit'][] = '_social_group_node_form_submit';
    }
  }
  $group_forms = [
    'edit' => [
      'group_open_group_edit_form',
      'group_closed_group_edit_form',
    ],
    'add' => [
      'group_open_group_add_form',
      'group_closed_group_add_form',
    ],
  ];
  if (in_array($form_id, $group_forms['add']) || in_array($form_id, $group_forms['edit'])) {
    $form['path']['#type'] = 'hidden';
    $form['actions']['submit']['#value'] = t('Save');
    if (in_array($form_id, $group_forms['edit'])) {
      $social_group_form = SocialGroupAddForm::create(\Drupal::getContainer());
      $group_type_element = $social_group_form
        ->getGroupTypeElement();

      // Get the current group.
      $group = _social_group_get_current_group();

      // Set the default value in the form.
      $group_type_element['#default_value'] = $group
        ->getGroupType()
        ->id();
      $group_type_element['#disabled'] = TRUE;
      $form['group_type'] = $group_type_element;
      $form['#group_children']['group_type'] = 'group_content';
    }
  }
  switch ($form_id) {
    case 'group_content_public_group-group_membership_group-join_form':
    case 'group_content_public_group-group_membership_group-leave_form':
    case 'group_content_public_group-group_membership_add_form':
    case 'group_content_public_group-group_membership_edit_form':
      $form['actions']['cancel'] = [
        '#type' => 'submit',
        '#value' => t('Cancel'),
        '#submit' => [
          '_social_group_cancel_join_leave_form',
        ],
        '#limit_validation_errors' => [],
      ];
      $form['actions']['submit']['#submit'][] = '_social_group_action_form_submit';
      if (isset($form['path'])) {
        $form['path']['#access'] = FALSE;
      }
      if (\Drupal::currentUser()
        ->id() !== 1 && isset($form['group_roles']['widget']['#options']['public_group-group_admin'])) {
        unset($form['group_roles']['widget']['#options']['public_group-group_admin']);
      }
      break;
    case 'group_public_group_edit_form':
    case 'group_public_group_add_form':
      $form['path']['#type'] = 'hidden';
      $form['actions']['submit']['#value'] = t('Save');
      break;
  }
}