addressfield.views.inc 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Implements hook_field_views_data().
  4. */
  5. function addressfield_field_views_data($field) {
  6. $data = field_views_field_default_views_data($field);
  7. // Add a filter handler for countries.
  8. $key = key($field['storage']['details']);
  9. foreach ($field['storage']['details'][$key] as $type => $tables) {
  10. foreach ($tables as $table_name => $columns) {
  11. if (!isset($columns['country'])) {
  12. continue;
  13. }
  14. $column_real_name = $columns['country'];
  15. if (!isset($data[$table_name][$column_real_name]['filter'])) {
  16. continue;
  17. }
  18. $data[$table_name][$column_real_name]['filter']['handler'] = 'addressfield_views_handler_filter_country';
  19. }
  20. }
  21. // Only expose these components as Views field handlers.
  22. $implemented = array(
  23. 'country' => 'addressfield_views_handler_field_country',
  24. 'administrative_area' => 'addressfield_views_handler_field_administrative_area',
  25. 'sub_administrative_area' => 'views_handler_field',
  26. 'dependent_locality' => 'views_handler_field',
  27. 'locality' => 'views_handler_field',
  28. 'postal_code' => 'views_handler_field',
  29. 'thoroughfare' => 'views_handler_field',
  30. 'premise' => 'views_handler_field',
  31. 'sub_premise' => 'views_handler_field',
  32. 'organisation_name' => 'views_handler_field',
  33. 'name_line' => 'views_handler_field',
  34. 'first_name' => 'views_handler_field',
  35. 'last_name' => 'views_handler_field',
  36. 'data' => 'views_handler_field_serialized',
  37. );
  38. // Get the translated field information.
  39. $properties = addressfield_data_property_info();
  40. // Iterate over addressfield defined tables.
  41. foreach ($data as &$table) {
  42. // Make sure the parent Views field (addressfield) is defined.
  43. if (isset($table[$field['field_name']]['field'])) {
  44. // Use the parent field definition as a template for component columns.
  45. $field_def = $table[$field['field_name']]['field'];
  46. // Remove 'additional fields' from the field definition. We don't
  47. // necessarily want all our sibling columns.
  48. unset($field_def['additional fields']);
  49. // Define the valid columns.
  50. $valid_columns = array();
  51. foreach ($implemented as $implement => $handler) {
  52. $column_name = $field['field_name'] . '_' . $implement;
  53. $valid_columns[$column_name] = $handler;
  54. }
  55. // Iterate over the addressfield components.
  56. foreach ($table as $column_name => &$column) {
  57. if (empty($column['field']) && isset($valid_columns[$column_name])) {
  58. // Assign the default component definition.
  59. $column['field'] = $field_def;
  60. $column['field']['real field'] = $column_name;
  61. $column['field']['handler'] = $valid_columns[$column_name];
  62. // Assign human-friendly field labels for addressfield components.
  63. $field_labels = field_views_field_label($field['field_name']);
  64. $field_label = array_shift($field_labels);
  65. $property = str_replace($field_def['field_name'] . '_', '', $column_name);
  66. if (!empty($properties[$property])) {
  67. $property_label = $properties[$property]['label'];
  68. $title = t('@field-label - @property-label', array(
  69. '@field-label' => $field_label,
  70. '@property-label' => $property_label,
  71. ));
  72. $column['title'] = $title;
  73. $column['title short'] = $title;
  74. }
  75. }
  76. }
  77. }
  78. }
  79. return $data;
  80. }