Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By jagadeesh_developer on
Hi,
I tried to implement the following code as module, but iam unable to get result to my filter dropdown
dd_district.info
name = Dropdown District
description = Provides dropdown filter for District field
core = 7.x
files[] = dd_district.module
dd_district.module
function dd_district_form_views_exposed_form_alter(&$form, &$form_state) {
$field_id = 'field_district'; // change for the field in question
// 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', '', '!=')
->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('' => '---');
// Buld the options array based on the query results, overwriting duplicate entries
foreach($nodes as $nid => $node) {
$value = $node->{$field_id}['und'][0]['value'];
$options[$value] = $value;
}
// Alter the field
$form[$field_id .'_value']['#type'] = 'select';
$form[$field_id .'_value']['#options'] = $options;
$form[$field_id .'_value']['#size'] = 1;
}
}