You are here

function social_group_flexible_group_form_alter in Open Social 10.0.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module \social_group_flexible_group_form_alter()
  2. 10.3.x modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module \social_group_flexible_group_form_alter()
  3. 10.1.x modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module \social_group_flexible_group_form_alter()
  4. 10.2.x modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module \social_group_flexible_group_form_alter()

Implements hook_form_alter().

File

modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module, line 76
The Social Group Flexible Group module.

Code

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

  // Exposed Filter block on the all-groups overview and search.
  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') {

    // Update filter values so it matches the join methods in the popover.
    if (!empty($form['field_group_allowed_join_method'])) {
      if (array_key_exists('added', $form['field_group_allowed_join_method']['#options'])) {
        $form['field_group_allowed_join_method']['#options']['added'] = t('Invite only');
      }
      if (array_key_exists('direct', $form['field_group_allowed_join_method']['#options'])) {
        $form['field_group_allowed_join_method']['#options']['direct'] = t('Open to join');
      }
      if (array_key_exists('request', $form['field_group_allowed_join_method']['#options'])) {
        $form['field_group_allowed_join_method']['#options']['request'] = t('Request to join');
      }
    }

    // Add states so this is only available when flexible groups is checked.
    // Could be hidden when only flexible groups is enabled, so check that.
    // @todo remove this once everything is migrated to flexible groups.
    if (!empty($form['field_group_allowed_join_method']) && !empty($form['type']['#options']) && $form['type']['#type'] !== 'hidden') {
      $form['field_group_allowed_join_method']['#states'] = [
        'visible' => [
          ':input[name="type"]' => [
            'value' => 'flexible_group',
          ],
        ],
      ];
    }
  }

  // For adding or editing a flexible group, we alter the visibility fields.
  if ($form['#id'] === 'group-flexible-group-add-form' || $form['#id'] === 'group-flexible-group-edit-form') {

    // Change the group visibility on flexible groups.
    if (!empty($form['field_group_allowed_visibility'])) {
      if (!empty($form['field_group_allowed_visibility']['widget']['#title'])) {
        $form['field_group_allowed_visibility']['widget']['#title'] = t('Group content visibility options');
      }
      if (!empty($form['field_group_allowed_visibility']['widget']['#description'])) {
        $form['field_group_allowed_visibility']['widget']['#description'] = t('Choose the visibility options allowed for the group content.');
      }

      // When referencing select lists and radio buttons in remote conditions,
      // a 'value' condition must be used.
      $form['field_group_allowed_visibility']['#states'] = [
        'visible' => [
          ':input[name="field_flexible_group_visibility"]' => [
            [
              'value' => 'public',
            ],
            [
              'value' => 'community',
            ],
            [
              'value' => 'members',
            ],
          ],
        ],
      ];
    }

    // Change the allowed join method on flexible groups.
    if (!empty($form['field_group_allowed_join_method'])) {

      // First we reorder the elmements, if invite only is part of it
      // we always want to show this last.
      if (!empty($form['field_group_allowed_join_method']['widget']['#options'])) {
        if (array_key_exists('added', $form['field_group_allowed_join_method']['widget']['#options'])) {
          $option = $form['field_group_allowed_join_method']['widget']['#options']['added'];

          // Unset it.
          unset($form['field_group_allowed_join_method']['widget']['#options']['added']);

          // Add it at the end.
          $form['field_group_allowed_join_method']['widget']['#options']['added'] = $option;
        }
      }
      if (!empty($form['field_group_allowed_join_method']['widget']['#title'])) {
        $form['field_group_allowed_join_method']['widget']['#title'] = t('Join methods');
      }
      if (!empty($form['field_group_allowed_join_method']['widget']['#description'])) {
        $form['field_group_allowed_join_method']['widget']['#description'] = t('How can people join this group. Group managers can always add members directly, regardless of the chosen join method.');
      }

      // When referencing select lists and radio buttons in remote conditions,
      // a 'value' condition must be used.
      $form['field_group_allowed_join_method']['#states'] = [
        'visible' => [
          ':input[name="field_flexible_group_visibility"]' => [
            [
              'value' => 'public',
            ],
            [
              'value' => 'community',
            ],
            [
              'value' => 'members',
            ],
          ],
        ],
      ];
    }

    // Only when adding a flexible group, we need to predefine the
    // content visibility and join method based on the group visibility.
    if ($form['#id'] === 'group-flexible-group-add-form') {

      // Reset default_values on the add form only.
      // So we can use state management to check / select.
      if (!empty($form['field_group_allowed_join_method']['widget']['#default_value'])) {
        $form['field_group_allowed_join_method']['widget']['#default_value'] = [];
      }
      if (!empty($form['field_group_allowed_visibility']['widget']['#default_value'])) {
        $form['field_group_allowed_visibility']['widget']['#default_value'] = [];
      }
    }

    // On the edit form, if there is no value set for the group_visibility
    // it means it was an already existing flexible group and we need to add a
    // default value so people can save it.
    if ($form['#id'] === 'group-flexible-group-edit-form' && empty($form['field_flexible_group_visibility']['widget']['#default_value'])) {

      // The default is always community because we used to only have closed
      // groups (which is visible for the community) or public if the
      // content visibility has public as an option.
      $form['field_flexible_group_visibility']['widget']['#default_value'] = 'community';
      if (in_array('public', $form['field_group_allowed_visibility']['widget']['#default_value'])) {
        $form['field_flexible_group_visibility']['widget']['#default_value'] = 'public';
      }
    }

    // Disable type field if there are no terms added in 'Group type' vocab.
    // Also, check for settings by SM for enabling this field.
    $group_type_settings = \Drupal::config('social_group.settings')
      ->get('social_group_type_required');
    if ($group_type_settings && isset($form['field_group_type']) && !empty(\Drupal::entityTypeManager()
      ->getStorage('taxonomy_term')
      ->loadTree('group_type'))) {
      $form['field_group_type']['widget']['#required'] = TRUE;

      // Unset the group type label for none.
      if (isset($form['field_group_type']['widget']['#options']['_none'])) {
        unset($form['field_group_type']['widget']['#options']['_none']);
      }
    }
    else {
      $form['field_group_type']['#access'] = FALSE;
    }
    $form['#after_build'][] = 'social_group_flexible_group_flexible_group_add_after_build';
    $form['#attached']['library'][] = 'social_group_flexible_group/admin';
  }
}