function social_group_form_alter in Open Social 8.9
Same name and namespace in other branches
- 8 modules/social_features/social_group/social_group.module \social_group_form_alter()
- 8.2 modules/social_features/social_group/social_group.module \social_group_form_alter()
- 8.3 modules/social_features/social_group/social_group.module \social_group_form_alter()
- 8.4 modules/social_features/social_group/social_group.module \social_group_form_alter()
- 8.5 modules/social_features/social_group/social_group.module \social_group_form_alter()
- 8.6 modules/social_features/social_group/social_group.module \social_group_form_alter()
- 8.7 modules/social_features/social_group/social_group.module \social_group_form_alter()
- 8.8 modules/social_features/social_group/social_group.module \social_group_form_alter()
- 10.3.x modules/social_features/social_group/social_group.module \social_group_form_alter()
- 10.0.x modules/social_features/social_group/social_group.module \social_group_form_alter()
- 10.1.x modules/social_features/social_group/social_group.module \social_group_form_alter()
- 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 551 - The Social group module.
Code
function social_group_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$social_group_types = [
'open_group',
'closed_group',
'public_group',
];
\Drupal::moduleHandler()
->alter('social_group_types', $social_group_types);
$join_forms = [];
$leave_forms = [];
$membership_add_forms = [];
$membership_edit_forms = [];
$membership_delete_forms = [];
$group_forms = [];
foreach ($social_group_types as $social_group_type) {
$join_forms[] = "group_content_{$social_group_type}-group_membership_group-join_form";
$leave_forms[] = "group_content_{$social_group_type}-group_membership_group-leave_form";
$membership_add_forms[] = "group_content_{$social_group_type}-group_membership_add_form";
$membership_edit_forms[] = "group_content_{$social_group_type}-group_membership_edit_form";
$membership_delete_forms[] = "group_content_{$social_group_type}-group_membership_delete_form";
$group_forms['edit'][] = "group_{$social_group_type}_edit_form";
$group_forms['add'][] = "group_{$social_group_type}_add_form";
$group_forms['delete'][] = "group_{$social_group_type}_delete_form";
}
$action_forms = array_merge($join_forms, $leave_forms, $membership_delete_forms, $membership_add_forms);
$membership_forms = array_merge($membership_add_forms, $membership_edit_forms);
// Perform alterations on joining / leaving groups.
if (in_array($form_id, $membership_delete_forms)) {
$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, $join_forms)) {
$markup = t('By submitting this form you will become a member of the group. As a member of the group you will begin to receive notifications about changes and interactions within the group. Your profile will also be included in group membership.');
// When the user is accepting an invite, change the text to something more
// meaningful.
if (\Drupal::routeMatch()
->getRouteName() === 'ginvite.invitation.accept') {
$markup = t('Are you sure to accept the invitation and join this group? You can leave this group at any time.');
}
$form['help'] = [
'#type' => 'item',
'#markup' => $markup,
];
$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';
}
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('Find people by name');
$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';
}
// Change the form when adding members directly in groups.
if (in_array($form_id, $membership_add_forms, TRUE) && \Drupal::routeMatch()
->getRouteName() !== 'grequest.group_request_membership_approve') {
// Lets add the new select 2 widget to add members to a group.
$form['entity_id']['widget'][0]['target_id'] = [
'#title' => t('Find people by name or email address'),
'#type' => 'select2',
'#multiple' => TRUE,
'#tags' => TRUE,
'#autocomplete' => TRUE,
'#select2' => [
'placeholder' => t('Jane Doe'),
'tokenSeparators' => [
',',
';',
],
],
'#selection_handler' => 'social',
'#target_type' => 'user',
'#element_validate' => [
'_social_group_unique_members',
],
];
// Group roles and the automated URL alias don't make sense here.
if (isset($form['group_roles'])) {
unset($form['group_roles']);
}
if (isset($form['path'])) {
unset($form['path']);
}
}
// 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';
}
}
if (in_array($form_id, $group_forms['add']) || in_array($form_id, $group_forms['edit']) || in_array($form_id, $group_forms['delete'])) {
// Add custom submit handler just for redirect purposes.
$form['actions']['submit']['#submit'][] = '_social_group_edit_submit_redirect';
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');
// Hide default title from Address field.
if (isset($form['field_group_address'])) {
$form['field_group_address']['widget'][0]['#title'] = '';
}
}
if (in_array($form_id, $group_forms['edit'])) {
$social_group_form = SocialGroupAddForm::create(\Drupal::getContainer());
$group_type_element = $social_group_form
->getGroupTypeElement(TRUE);
// Get the current group.
$group = _social_group_get_current_group();
// Set the default value in the form.
$group_type_element['widget']['#default_value'] = $group
->getGroupType()
->id();
// If user doesn't have permission to change group types disable it.
// Or if group types can't be edited due to visibility issues.
if (!social_group_group_type_permission_check()) {
$group_type_element['#disabled'] = TRUE;
}
else {
$group_type_element['#prefix'] = '<div id="group-type-result"></div>';
$group_type_element['widget']['#ajax'] = [
'callback' => '_social_group_inform_group_type_selection',
'effect' => 'fade',
'event' => 'change',
];
}
$form['group_type'] = $group_type_element;
// Disable all group types that can't be edited. Because they don't have
// a visibility.
foreach (array_keys($form['group_type']['widget']['#options']) as $type) {
if (\Drupal::service('social_group.helper_service')
->getDefaultGroupVisibility($type) === NULL) {
$form['group_type']['widget'][$type] = [
'#disabled' => TRUE,
];
}
}
$form['#fieldgroups']['group_settings']->children[] = 'group_type';
$form['#group_children']['group_type'] = 'group_settings';
$form['actions']['submit']['#submit'][] = '_social_group_type_edit_submit';
}
if (in_array($form_id, $group_forms['delete'])) {
// Add custom submit handler to delete all content of the group.
$group = _social_group_get_current_group();
$form['description']['#markup'] = t('Are you sure you want to delete your group "@group" along with all of the posts, events and topics inside this group? This action cannot be undone.', [
'@group' => $group
->label(),
]);
$form['actions']['cancel'] = [
'#type' => 'submit',
'#value' => t('Cancel'),
'#submit' => [
'_social_group_cancel_join_leave_form',
],
'#limit_validation_errors' => [],
];
array_unshift($form['actions']['submit']['#submit'], '_social_group_delete_group');
}
}
if (in_array($form_id, [
'group_flexible_group_edit_form',
'group_flexible_group_add_form',
])) {
$join_method_default_value = 'added';
// Ensure we have a better descriptive label.
if (array_key_exists('added', $form['field_group_allowed_join_method']['widget']['#options'])) {
$form['field_group_allowed_join_method']['widget']['#options']['added'] = t('Invite-only - users can only join this group if they are added/invited by group managers');
}
if (array_key_exists('direct', $form['field_group_allowed_join_method']['widget']['#options'])) {
$form['field_group_allowed_join_method']['widget']['#options']['direct'] = t('Open to join - users can join this group without approval');
}
// If directly exists it's becoming the default.
if (in_array('direct', $form['field_group_allowed_join_method']['widget']['#default_value'])) {
$join_method_default_value = 'direct';
}
elseif (in_array('request', $form['field_group_allowed_join_method']['widget']['#default_value'])) {
$join_method_default_value = 'request';
}
$form['field_group_allowed_join_method']['widget']['#type'] = 'radios';
$form['field_group_allowed_join_method']['widget']['#default_value'] = $join_method_default_value;
}
// Exposed Filter block on the all-groups overview.
if ($form['#id'] === 'views-exposed-form-newest-groups-page-all-groups' || $form['#id'] === 'views-exposed-form-search-groups-page-no-value' || $form['#id'] === 'views-exposed-form-search-groups-page') {
$account = \Drupal::currentUser();
if (!empty($form['type']['#options'])) {
foreach ($form['type']['#options'] as $type => $label) {
// All / Any we can skip they are optional translatable options
// and not group types.
if ($label instanceof TranslatableMarkup) {
continue;
}
if (!social_group_can_view_groups_of_type($type, $account)) {
unset($form['type']['#options'][$type]);
}
}
}
}
}