You are here

function social_group_field_widget_form_alter in Open Social 8.9

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

Alter the visibility field within groups.

Implements hook_field_widget_form_alter().

File

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

Code

function social_group_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {

  /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
  $field_definition = $context['items']
    ->getFieldDefinition();

  // Unset the visibility field options which are not enabled.
  if ($field_definition
    ->getType() == 'entity_access_field') {
    $default_value = $context['items']
      ->getValue();
    if (isset($default_value[0]['value'])) {
      $element['#default_value'] = $default_value[0]['value'];
    }
    else {
      $default_visibility = \Drupal::configFactory()
        ->get('entity_access_by_field.settings')
        ->get('default_visibility');
      $element['#default_value'] = $default_visibility;
    }
    $entity = $form_state
      ->getFormObject()
      ->getEntity();
    $current_group = _social_group_get_current_group();
    if (!empty($current_group)) {
      $group_type = $current_group
        ->getGroupType();
      $group_type_id = $group_type
        ->id();

      // If it's an existing entity we dont need to set the default
      // rather follow the saved one.
      if (!$entity
        ->id()) {
        $element['#default_value'] = \Drupal::service('social_group.helper_service')
          ->getDefaultGroupVisibility($group_type_id);
      }
    }
    else {
      $group_type_id = NULL;

      // This is not a group so lets disable this visibility option.
      $element['group']['#disabled'] = TRUE;
    }
    $visibility_options = social_group_get_allowed_visibility_options_per_group_type($group_type_id, NULL, $entity, $current_group);
    foreach ($visibility_options as $visibility => $allowed) {
      $element[$visibility]['#disabled'] = !$allowed;
    }

    // This contains the group that is passed back from the form_state as
    // chosen by selecting a group in the $form['groups'] select widget.
    // With this we can ensure that on validation the content visibility
    // field is disabling the correct values based on the selected group.
    if (!empty($form_state
      ->getValue('groups'))) {
      $group_id = NULL;
      $group_value = $form_state
        ->getValue('groups');
      if (is_array($group_value)) {
        $group_id = $group_value[0]['target_id'];
      }
      if (is_string($group_value)) {
        $group_id = (int) $group_value;
      }

      // Load the correct group.
      $selected_groups = Group::loadMultiple([
        $group_id,
      ]);

      // Ensure we disable the correct visibility options.
      foreach ($selected_groups as $current_group) {
        if (!empty($current_group)) {
          $group_type = $current_group
            ->getGroupType();
          $group_type_id = $group_type
            ->id();
          $visibility_options = social_group_get_allowed_visibility_options_per_group_type($group_type_id, NULL, NULL, $current_group);
          foreach ($visibility_options as $visibility => $allowed) {
            $element[$visibility]['#disabled'] = !$allowed;
          }
        }
      }
    }
  }
}