I am building an events website - so I have a taxonomy term Venue - and this, in turn, has a field Address, which is an instance of addressfield.
I need to expose the Locality field of the address to users. I have this working fine, but it's displaying as a text field, since the original field is a text field. http://journalofmusic.com/listings/test
I want the field displayed to users as a dropdown of all possible locality fields.
I have taken a look at this thread, and this specific comment: http://drupal.org/node/1549250#comment-6653514
But I can't quite get it to work. I've updated the example code to refer to taxonomy_term, and not node, and the locality field...
Below is the code. Can anyone suggest what I'm doing wrong here?
Thanks!
<?php
function dd_locality_form_views_exposed_form_alter(&$form, &$form_state) {
$field_id = 'field_address'; // change for the field in question
// Only alter forms with the necessary field
if (isset($form[$field_id .'_locality'])) {
// Build a query to get all term ids having the specified field
$query = new EntityFieldQuery();
$results = $query->entityCondition('entity_type', 'taxonomy_term')
->fieldCondition($field_id, 'locality', '', '!=')
->fieldOrderBy($field_id, 'locality', 'ASC')
->execute();
// Attach the field values to the terms
$terms = $results['taxonomy_term'];
field_attach_load('taxonomy_term', $terms, 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($terms as $nid => $term) {
$value = $node->{$field_id}['und'][0]['locality'];
$options[$value] = $value;
}
// Alter the field
$form[$field_id .'_locality']['#type'] = 'select';
$form[$field_id .'_locality']['#options'] = $options;
$form[$field_id .'_locality']['#size'] = 1;
}
}
?>