Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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;
  }
}
?>
share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

There is a small mistake in the code

$value = $node->{$field_id}['und'][0]['locality'];

should be

$value = $term->{$field_id}['und'][0]['locality'];
share|improve this answer
    
Doh! Thanks Shameem - that's working now. –  Simon Doyle Jan 18 '13 at 16:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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