2

I'm using a custom module and hook_form_alter to change a Views Exposed Filter from a text field to checkboxes. When altering to a select list, the module works fine. However, when I change the form type to "checkbox", the filter renders as a single checked checkbox without a label. I thought I had solved it earlier by simply removing the #size line, but after reverting the code and trying to fix it that way again, it's no longer fixing it. Any idea what I need to change?

Working select list code:

function dropdown_bartab_form_views_exposed_form_alter(&$form, &$form_state) {
  $field_id = 'field_venue_bar_tab';

  // Only alter forms with the necessary field
  if (isset($form[$field_id .'_value'])) {

// Build a query to get all node ids having the specified field
$query = new EntityFieldQuery();
$results = $query->entityCondition('entity_type', 'node')
                  ->fieldCondition($field_id, 'value', 'NULL', '!=')
                  ->fieldOrderBy($field_id, 'value', 'ASC')                   
                  ->execute();

// Attach the field values to the nodes
$nodes = $results['node'];
field_attach_load('node', $nodes, FIELD_LOAD_CURRENT, array('field_id' => $field_id));

// Add a default so the filter is optional
$options = array(
    '' => 'Any',
    '0' => 'Drinks',
    '1' => 'Food',
    );

// Alter the field
$form[$field_id .'_value']['#type'] = 'select';
$form[$field_id .'_value']['#options'] = $options;
$form[$field_id .'_value']['#size'] = 1;
  }
}
  • Hey Mrweiner, it's been a while since you've posted this, but does this work with Drupal 7? Also, is the $field_id the text box input ID? If so, this doesn't seem to do anything for me. Your help would be very appreciated! – WebMW Jul 20 '18 at 14:28
  • Heya! I was using D7 at the time so it is D7 code, and I'd wager that $field_id is the ID of the field itself, not the input box ID. So, the field name as it is on your content type. – Mrweiner Jul 20 '18 at 14:45
  • Thanks a million for the response! I just got it working as a select box, but it is super slow. Page loads and query times are now taking about 8X longer. Did you experience that issue? Also, when changing to "checkboxes" as suggested, no results can be found, even with the "Any" selected, (unlike the select box which seems to query fine, just SUPER SLOW to the point that this is unusable!) – WebMW Jul 20 '18 at 15:31
  • No problem. As for the – Mrweiner Jul 20 '18 at 15:36
  • As for the? (lol) Page loads / queries are taking about a minute each... have you experienced that? I really want to use this script but that's going to be a deal breaker if there is no fix :( – WebMW Jul 20 '18 at 15:51
2

You probably want to use #type checkboxES (uppercase only for emphasis ;)), which is a list of checkboxes. checkbox is just a single checkbox that can either be checked or not, it can't have multiple options, so that property is ignored.

|improve this answer|||||
  • That's what I thought too. But for some reason, replacing checkboxES gives me a WSOD. – Mrweiner Jul 9 '12 at 19:50
  • Well, WSOD just means that there was a PHP fatal error somewhere, see drupal.org/node/158043 for how to find out the actual error. Probably something as simple as a wrong property/value in the form element. – Berdir Jul 9 '12 at 19:56
  • It's returning "Fatal error: Cannot create references to/from string offsets nor overloaded objects in ..." I'm not sure how to interpret it. – Mrweiner Jul 9 '12 at 20:00
  • dpm() the form element and provide a screenshot, My guess would be that #default_value is a string, try changing it to an array. Look at the other properties for other wrong values. – Berdir Jul 9 '12 at 20:02
  • Sorry to be dense, but I've been lost on the dpm() thing whenever I read about it. From what I understand, I need devel enabled, right? But if it's white-screened, I wouldn't be able to access that anyway. – Mrweiner Jul 9 '12 at 20:10
0

So, I changed nothing, but it suddenly started working again. Must have been some strange bug.

|improve this answer|||||

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.