0

I have a view with an exposed filter of organic group id's (ie: gid). I wanted to convert it to be a dropdown with the actual group name. I did so with a form alter. The filter works fine, however I'm having an issue with the filter when trying to view all groups. I tried adding 'ALL' to the dropdown, but that doesn't match a particular gid, so it doesn't work. How can I fix my code to allow content of all groups to show?

/**
 *  Implements hook_form_alter().
 */
function tma_views_form_alter(&$form, &$form_state, $form_id) {
  if ($form['#id'] === 'views-exposed-form-event-list-page') {
    $form['field_event_type_tid']['#type'] = 'hidden';
  }
  elseif ($form['#id'] === 'views-exposed-form-page-event-calendar-page') {
    $form['og_group_ref_target_id'] = array(
      '#type' => 'select',
      '#options' => tma_views_get_chapters_list(),
      '#default_value' => 'ALL',
    );
  } 
}

/*
 * Retrieves an associated array list of all Chapter nodes
 * return - a list of nids
 */
function tma_views_get_chapters_list() {
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'chapter');
  $results = $query->execute();
  foreach ($results['node'] as $node) {
    $nids[] = $node->nid;
  }
  $nodes = node_load_multiple($nids);
  $options['ALL'] = 'Select Chapter';
  foreach ($nodes as $node) {
    $options[$node->nid] = $node->title;
  }
  return $options;
}

EDIT: One more thing to note: when no filter is set at all (for the organic group filter), I get a drupal error message on the screen:

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

I obviously would like to be able to fix that as well.

0

It would seem to me that you would want to modify that filter in the Views UI. Is required checked? Also, a standard practice that I've seen for Drupal select is having all actually keyed with _none rather than ALL.

  • Actually part of the problem is (I forgot about this) is that the Organic Group filter doesn't come with a select list widget option in the first place. It only comes with a input box. This is why I needed the form_alter to turn it into a select this, which causes the issue I am describing. I tried setting it to _none, and setting it as required, but that didn't help. Any other ideas? – user1015214 Feb 20 '15 at 14:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.