addressfield_tokens.theme.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @file
  4. * Theme Controllers.
  5. */
  6. /**
  7. * Implements theme_field();
  8. *
  9. * Themes an address according to the default renderer.
  10. */
  11. function theme_addressfield_formatter($vars) {
  12. $address = $vars['address'];
  13. if (isset($address['addressfield'])) {
  14. $address = $address['addressfield'];
  15. }
  16. $handlers = $vars['handlers'];
  17. if (empty($handlers)) {
  18. $handlers = array('address');
  19. }
  20. $out = addressfield_generate($address, $handlers, array('mode' => 'render'));
  21. return '<div class="addressfield">' . render($out) . '</div>';
  22. }
  23. /**
  24. * Implements theme_field();
  25. *
  26. * Themes an address field into "city state, country"
  27. */
  28. function theme_addressfield_formatter__citystate($vars) {
  29. $loc = $vars['address'];
  30. // Determine which location components to render
  31. $out = array();
  32. if (!empty($loc['locality'])) {
  33. $out[] = $loc['locality'];
  34. }
  35. if (!empty($loc['administrative_area'])) {
  36. $out[] = $loc['administrative_area'];
  37. }
  38. if ($loc['country'] != addressfield_tokens_default_country() && $country_name = _addressfield_tokens_country($loc['country'])) {
  39. $out[] = $country_name;
  40. }
  41. // If there's no location, render an alternate
  42. if (empty($out)) {
  43. return '';
  44. }
  45. // Render the location components
  46. $output = '<span class="addressfield-citystate">' . implode(', ', $out) . '</span>';
  47. return $output;
  48. }
  49. /**
  50. * Implements theme_field();
  51. *
  52. * Themes an address field into "name, street1, street2, city state zip country"
  53. */
  54. function theme_addressfield_formatter__linear($vars) {
  55. $loc = $vars['address'];
  56. // If single line name is empty, construct it from first and last name.
  57. if (empty($loc['name_line'])) {
  58. $parts = array();
  59. if (!empty($loc['first_name'])) {
  60. $parts[] = $loc['first_name'];
  61. }
  62. if (!empty($loc['last_name'])) {
  63. $parts[] = $loc['last_name'];
  64. }
  65. $loc['name_line'] = join(' ', $parts);
  66. }
  67. // Determine which location components to render
  68. $out = array();
  69. if (!empty($loc['name_line']) && $vars['name_line']) {
  70. $out[] = $loc['name_line'];
  71. }
  72. if (!empty($loc['organisation_name']) && $vars['organisation_name']) {
  73. $out[] = $loc['organisation_name'];
  74. }
  75. if (!empty($loc['thoroughfare'])) {
  76. $out[] = $loc['thoroughfare'];
  77. }
  78. if (!empty($loc['premise']) && $vars['premise']) {
  79. $out[] = $loc['premise'];
  80. }
  81. if (!empty($loc['locality'])) {
  82. $out[] = $loc['locality'];
  83. }
  84. if (!empty($loc['administrative_area'])) {
  85. $out[] = $loc['administrative_area'];
  86. }
  87. if (!empty($loc['postal_code'])) {
  88. $out[] = $loc['postal_code'];
  89. }
  90. if ($loc['country'] != addressfield_tokens_default_country() && $country_name = _addressfield_tokens_country($loc['country'])) {
  91. $out[] = $country_name;
  92. }
  93. // Render the location components
  94. $output = implode(', ', $out);
  95. return $output;
  96. }
  97. function theme_addressfield_formatter__components($vars) {
  98. $loc = $vars['address'];
  99. $components = $vars['components'];
  100. $separator = $vars['separator'];
  101. $out = array();
  102. foreach ($components as $key) {
  103. if (!empty($loc[$key])) {
  104. $out[$key] = $loc[$key];
  105. }
  106. elseif ($key == 'country_full' && !empty($loc['country'])) {
  107. $out[$key] = _addressfield_tokens_country($loc['country']);
  108. }
  109. elseif ($key == 'administrative_area_full' && !empty($loc['country']) && !empty($loc['administrative_area'])) {
  110. $out[$key] = addressfield_tokens_state($loc['country'], $loc['administrative_area']);
  111. }
  112. }
  113. return implode($separator, $out);
  114. }