You are here

public static function SocialGroupSelectorWidget::validateGroupSelection in Open Social 8.5

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::validateGroupSelection()
  2. 8 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::validateGroupSelection()
  3. 8.2 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::validateGroupSelection()
  4. 8.3 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::validateGroupSelection()
  5. 8.4 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::validateGroupSelection()
  6. 8.6 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::validateGroupSelection()
  7. 8.7 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::validateGroupSelection()
  8. 8.8 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::validateGroupSelection()
  9. 10.3.x modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::validateGroupSelection()
  10. 10.0.x modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::validateGroupSelection()
  11. 10.1.x modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::validateGroupSelection()
  12. 10.2.x modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::validateGroupSelection()

Validate the group selection and change the visibility settings.

Parameters

array $form: Form to process.

\Drupal\Core\Form\FormStateInterface $form_state: Form state to process.

Return value

\Drupal\Core\Ajax\AjaxResponse Response changing values of the visibility field and set status message.

File

modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php, line 207

Class

SocialGroupSelectorWidget
A widget to select a group when creating an entity in a group.

Namespace

Drupal\social_group\Plugin\Field\FieldWidget

Code

public static function validateGroupSelection(array $form, FormStateInterface $form_state) {
  $ajax_response = new AjaxResponse();
  $selected_visibility = $form_state
    ->getValue('field_content_visibility');
  if (!empty($selected_visibility)) {
    $selected_visibility = $selected_visibility['0']['value'];
  }
  if ($selected_groups = $form_state
    ->getValue('groups')) {
    foreach ($selected_groups as $selected_group_key => $selected_group) {
      $gid = $selected_group['target_id'];
      $group = Group::load($gid);
      $group_type_id = $group
        ->getGroupType()
        ->id();
      $allowed_visibility_options = social_group_get_allowed_visibility_options_per_group_type($group_type_id);

      // TODO Add support for multiple groups, for now just process 1 group.
      break;
    }
  }
  else {
    $default_visibility = $form_state
      ->getValue('default_visibility');
    $entity = $form_state
      ->getFormObject()
      ->getEntity();
    $allowed_visibility_options = social_group_get_allowed_visibility_options_per_group_type(NULL, NULL, $entity);
    $ajax_response
      ->addCommand(new InvokeCommand('#edit-field-content-visibility-' . $default_visibility, 'prop', [
      'checked',
      'checked',
    ]));
  }
  foreach ($allowed_visibility_options as $visibility => $allowed) {
    $ajax_response
      ->addCommand(new InvokeCommand('#edit-field-content-visibility-' . $visibility, 'addClass', [
      'js--animate-enabled-form-control',
    ]));
    if ($allowed === TRUE) {
      $ajax_response
        ->addCommand(new InvokeCommand('#edit-field-content-visibility-' . $visibility, 'removeAttr', [
        'disabled',
      ]));
      if (empty($default_visibility) || $visibility === $default_visibility) {
        $ajax_response
          ->addCommand(new InvokeCommand('#edit-field-content-visibility-' . $visibility, 'prop', [
          'checked',
          'checked',
        ]));
      }
    }
    else {
      if ($selected_visibility && $selected_visibility === $visibility) {
        $ajax_response
          ->addCommand(new InvokeCommand('#edit-field-content-visibility-' . $visibility, 'removeAttr', [
          'checked',
        ]));
      }
      $ajax_response
        ->addCommand(new InvokeCommand('#edit-field-content-visibility-' . $visibility, 'prop', [
        'disabled',
        'disabled',
      ]));
    }
    $ajax_response
      ->addCommand(new InvokeCommand('#edit-field-content-visibility-' . $visibility, 'change'));
  }
  $text = t('Changing the group may have impact on the <strong>visibility settings</strong>.');
  drupal_set_message($text, 'info');
  $alert = [
    '#type' => 'status_messages',
  ];
  $ajax_response
    ->addCommand(new HtmlCommand('#group-selection-result', $alert));
  return $ajax_response;
}