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;
  }