addressfield_views_handler_field_administrative_area.inc 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * Defines a field handler that can display the administrative area name
  4. * instead of the code.
  5. */
  6. class addressfield_views_handler_field_administrative_area extends views_handler_field {
  7. function query() {
  8. parent::query();
  9. $this->country_alias = $this->query->add_field($this->table_alias, $this->definition['field_name'] . '_country');
  10. }
  11. function option_definition() {
  12. $options = parent::option_definition();
  13. $options['display_name'] = array('default' => TRUE);
  14. return $options;
  15. }
  16. function options_form(&$form, &$form_state) {
  17. parent::options_form($form, $form_state);
  18. $form['display_name'] = array(
  19. '#type' => 'checkbox',
  20. '#title' => t('Display the name of administrative area instead of the code.'),
  21. '#default_value' => $this->options['display_name'],
  22. );
  23. }
  24. function get_value($values, $field = NULL) {
  25. $value = parent::get_value($values, $field);
  26. // If we have a value for the field, look for the administrative area name in the
  27. // Address Field options list array if specified.
  28. if (!empty($value) && !empty($this->options['display_name'])) {
  29. module_load_include('inc', 'addressfield', 'addressfield.administrative_areas');
  30. $country = $values->{$this->country_alias};
  31. $areas = addressfield_get_administrative_areas($country);
  32. if (!empty($areas[$value])) {
  33. $value = $areas[$value];
  34. }
  35. }
  36. return $value;
  37. }
  38. }