I am using Drupal 8. I have a view that shows some public content and some group content. I try to add a filter on group name as select list. I would like to have in the select list an option to get all content and one options by current user's group.

So I made a hook form alter

function btw_features_form_alter(&$form, &$form_state, $form_id) {

    if ($form_id == "views_exposed_form" && $form['#id'] == "views-exposed-form-all-events-page") {
      $form['filter-user-group']['#type'] = 'select';
      $form['filter-user-group']['#size'] = null;

      $groups = array('All' => t(''));

      $user= \Drupal::currentUser();
      $grp_membership_service = \Drupal::service('group.membership_loader');
      $grps = $grp_membership_service->loadByUser($user);
      foreach ($grps as $grp) {
        $grpLabel = $grp->getGroup()->label();
        $groups[$grpLabel] = t($grpLabel);

      $form['filter-user-group']['#default_value'] = $form['filter-user-group']['#options']['All'];

I encountered the folowing error :

An illegal choice has been detected. Please contact the site administrator.

What i made wrong ?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.