addressfield_tokens.module 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. <?php
  2. /**
  3. * @file
  4. * Main components.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function addressfield_tokens_menu() {
  10. $items = array();
  11. $items['admin/config/regional/address'] = array(
  12. 'title' => 'Addresses',
  13. 'description' => 'Settings for address fields and tokens',
  14. 'page callback' => 'drupal_get_form',
  15. 'page arguments' => array('addressfield_tokens_admin_form'),
  16. 'access arguments' => array('administer site configuration'),
  17. 'file' => 'addressfield_tokens.admin.inc',
  18. 'type' => MENU_NORMAL_ITEM,
  19. );
  20. return $items;
  21. }
  22. /**
  23. * Implements hook_theme().
  24. */
  25. function addressfield_tokens_theme($existing, $type, $theme, $path) {
  26. $theme = array(
  27. 'addressfield_formatter' => array(
  28. 'variables' => array('address' => NULL, 'handlers' => array('address')),
  29. 'file' => 'addressfield_tokens.theme.inc',
  30. ),
  31. 'addressfield_formatter__citystate' => array(
  32. 'variables' => array('address' => NULL),
  33. 'file' => 'addressfield_tokens.theme.inc',
  34. ),
  35. 'addressfield_formatter__linear' => array(
  36. 'variables' => array(
  37. 'address' => NULL,
  38. 'premise' => TRUE,
  39. 'organisation_name' => TRUE,
  40. 'name_line' => TRUE,
  41. ),
  42. 'file' => 'addressfield_tokens.theme.inc',
  43. ),
  44. 'addressfield_formatter__components' => array(
  45. 'variables' => array(
  46. 'address' => NULL,
  47. 'components' => array(
  48. 'thoroughfare',
  49. 'premise',
  50. 'locality',
  51. 'administrative_area',
  52. 'postal_code',
  53. 'country',
  54. ),
  55. 'separator' => ', ',
  56. ),
  57. 'file' => 'addressfield_tokens.theme.inc',
  58. ),
  59. );
  60. return $theme;
  61. }
  62. /**
  63. * Implements hook_field_formatter_info().
  64. */
  65. function addressfield_tokens_field_formatter_info() {
  66. return array(
  67. 'addressfield_citystate' => array(
  68. 'label' => t('City/State'),
  69. 'field types' => array('addressfield'),
  70. ),
  71. 'addressfield_linear' => array(
  72. 'label' => t('One line'),
  73. 'field types' => array('addressfield'),
  74. ),
  75. 'addressfield_state' => array(
  76. 'label' => t('State'),
  77. 'field types' => array('addressfield'),
  78. ),
  79. 'addressfield_country' => array(
  80. 'label' => t('Country'),
  81. 'field types' => array('addressfield'),
  82. ),
  83. 'addressfield_components' => array(
  84. 'label' => t('Address components'),
  85. 'field types' => array('addressfield'),
  86. 'settings' => array(
  87. 'components' => array(
  88. 'thoroughfare',
  89. 'premise',
  90. 'locality',
  91. 'administrative_area',
  92. 'postal_code',
  93. 'country',
  94. ),
  95. 'separator' => ', ',
  96. ),
  97. ),
  98. );
  99. }
  100. /**
  101. * Implements hook_field_formatter_settings_form().
  102. */
  103. function addressfield_tokens_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  104. $display = $instance['display'][$view_mode];
  105. $settings = $display['settings'];
  106. $element = array();
  107. if ($display['type'] == 'addressfield_components') {
  108. $element['components'] = array(
  109. '#type' => 'select',
  110. '#title' => t('Components to render'),
  111. '#multiple' => TRUE,
  112. '#rows' => 10,
  113. '#options' => addressfield_tokens_components(),
  114. '#default_value' => $settings['components'],
  115. '#required' => TRUE,
  116. );
  117. $element['separator'] = array(
  118. '#type' => 'textfield',
  119. '#title' => t('Separator'),
  120. '#description' => t('The separator to use between components. Use br tag for a line break.'),
  121. '#default_value' => $settings['separator'],
  122. );
  123. }
  124. return $element;
  125. }
  126. /**
  127. * Implements hook_field_formatter_settings_summary().
  128. */
  129. function addressfield_tokens_field_formatter_settings_summary($field, $instance, $view_mode) {
  130. $display = $instance['display'][$view_mode];
  131. $settings = $display['settings'];
  132. $summary = '';
  133. if ($display['type'] == 'addressfield_components') {
  134. $comps = array_intersect_key(addressfield_tokens_components(), array_flip($settings['components']));
  135. $sep = str_replace('\n', '<br/>', $settings['separator']);
  136. $summary = filter_xss(implode($sep, $comps));
  137. }
  138. return $summary;
  139. }
  140. /**
  141. * Implements hook_field_formatter_view().
  142. */
  143. function addressfield_tokens_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  144. $element = array();
  145. switch ($display['type']) {
  146. case 'addressfield_citystate':
  147. $theme = array('addressfield_formatter__citystate', 'addressfield_formatter');
  148. foreach ($items as $delta => $item) {
  149. if (!empty($item['country'])) {
  150. array_unshift($theme, 'addressfield_formatter__citystate__' . $item['country']);
  151. }
  152. $element[$delta] = array(
  153. '#theme' => $theme,
  154. '#address' => array_map('filter_xss', $item),
  155. );
  156. }
  157. break;
  158. case 'addressfield_linear':
  159. $theme = array('addressfield_formatter__linear', 'addressfield_formatter');
  160. foreach ($items as $delta => $item) {
  161. if (!empty($item['country'])) {
  162. array_unshift($theme, 'addressfield_formatter__linear__' . $item['country']);
  163. }
  164. $element[$delta] = array(
  165. '#theme' => $theme,
  166. '#address' => array_map('filter_xss', $item),
  167. );
  168. }
  169. break;
  170. case 'addressfield_country':
  171. foreach ($items as $delta => $item) {
  172. if (!empty($item['country'])) {
  173. $country = _addressfield_tokens_country($item['country']);
  174. $element[$delta] = array(
  175. '#type' => 'markup',
  176. '#markup' => filter_xss($country),
  177. '#prefix' => '<span class="addressfield-country">',
  178. '#suffix' => '</span>',
  179. );
  180. }
  181. }
  182. break;
  183. case 'addressfield_state':
  184. foreach ($items as $delta => $item) {
  185. if (!empty($item['country']) && !empty($item['administrative_area'])) {
  186. $state = addressfield_tokens_state($item['country'], $item['administrative_area']);
  187. $element[$delta] = array(
  188. '#type' => 'markup',
  189. '#markup' => filter_xss($state),
  190. '#prefix' => '<span class="addressfield-state">',
  191. '#suffix' => '</span>',
  192. );
  193. }
  194. }
  195. break;
  196. case 'addressfield_components':
  197. $theme = array('addressfield_formatter__components', 'addressfield_formatter');
  198. $settings = $display['settings'];
  199. foreach ($items as $delta => $item) {
  200. if (!empty($item['country'])) {
  201. array_unshift($theme, 'addressfield_formatter__components__' . $item['country']);
  202. }
  203. $element[$delta] = array(
  204. '#theme' => $theme,
  205. '#address' => array_map('filter_xss', $item),
  206. '#components' => $settings['components'],
  207. '#separator' => filter_xss($settings['separator'], array('br')),
  208. );
  209. }
  210. break;
  211. }
  212. return $element;
  213. }
  214. /**
  215. * Returns the country that has been configured as the default country.
  216. */
  217. function addressfield_tokens_default_country() {
  218. return variable_get('addressfield_tokens_default_country', 'US');
  219. }
  220. /**
  221. * Returns the names that have been configured for each address field.
  222. */
  223. function addressfield_tokens_property_names() {
  224. $names = variable_get('addressfield_tokens_property_names', array());
  225. if (empty($names)) {
  226. $props = addressfield_data_property_info();
  227. foreach ($props as $name => $prop) {
  228. $names[$name] = $prop['label'];
  229. }
  230. }
  231. return $names;
  232. }
  233. /**
  234. * Generates token components.
  235. *
  236. * @return mixed
  237. * Array of components.
  238. */
  239. function addressfield_tokens_components() {
  240. $comps = &drupal_static(__FUNCTION__, array());
  241. if (empty($comps)) {
  242. $names = addressfield_tokens_property_names();
  243. $fields = array(
  244. 'first_name',
  245. 'last_name',
  246. 'name_line',
  247. 'organisation_name',
  248. 'thoroughfare',
  249. 'premise',
  250. 'locality',
  251. 'dependent_locality',
  252. 'administrative_area',
  253. 'sub_administrative_area',
  254. 'postal_code',
  255. 'country',
  256. );
  257. foreach ($fields as $key) {
  258. $comps[$key] = $names[$key];
  259. if (in_array($key, array('administrative_area', 'country'))) {
  260. $comps[$key . '_full'] = t('@name (full)', array(
  261. '@name' => $names[$key],
  262. ));
  263. }
  264. }
  265. }
  266. return $comps;
  267. }
  268. /**
  269. * Gets the list of countries from the locale settings in Drupal.
  270. *
  271. * @return array
  272. * An array of countries. The keys are the 2-letter
  273. * abbreviations and the values are the full country names.
  274. */
  275. function _addressfield_tokens_countries() {
  276. $countries = &drupal_static(__FUNCTION__);
  277. if (empty($countries)) {
  278. require_once DRUPAL_ROOT . '/includes/locale.inc';
  279. $countries = country_get_list();
  280. }
  281. return $countries;
  282. }
  283. /**
  284. * Gets the name of the country with the given abbreviation.
  285. *
  286. * @param string $country
  287. * The 2-letter abbreviation of the country.
  288. *
  289. * @return string
  290. * The name of the country, or FALSE.
  291. */
  292. function _addressfield_tokens_country($country) {
  293. $countries = _addressfield_tokens_countries();
  294. // Country abbreviations will always be two uppercase letters.
  295. $country = drupal_strtoupper($country);
  296. if (!empty($country) && isset($countries[$country])) {
  297. return check_plain($countries[$country]);
  298. }
  299. return check_plain($country);
  300. }
  301. /**
  302. * Gets the abbreviation of the country with the given name.
  303. *
  304. * @param string $country
  305. * The name of the country.
  306. *
  307. * @return string
  308. * The 2-letter abbreviation of the country, or FALSE.
  309. */
  310. function _addressfield_tokens_country_abbr($country) {
  311. $countries = array_flip(array_map('strtolower', _addressfield_tokens_countries()));
  312. if (isset($countries[strtolower($country)])) {
  313. return check_plain($countries[strtolower($country)]);
  314. }
  315. return check_plain($country);
  316. }
  317. /**
  318. * Gets the list of states in the given country.
  319. *
  320. * @param string $country
  321. * The 2-letter abbreviation of the country.
  322. *
  323. * @return array
  324. * An array of countries. The keys are the 2-letter
  325. * abbreviations and the values are the full country names.
  326. */
  327. function addressfield_tokens_states($country) {
  328. global $language;
  329. $langcode = $language->language;
  330. $states = &drupal_static(__FUNCTION__);
  331. $country = drupal_strtoupper($country);
  332. if (!isset($states[$country])) {
  333. $cache = cache_get('addressfield_tokens_states:' . $langcode);
  334. if ($cache) {
  335. $states = $cache->data;
  336. }
  337. }
  338. if (!isset($states[$country])) {
  339. $format = addressfield_generate(array('country' => $country), array('address'), array('mode' => 'render'));
  340. if (isset($format['locality_block']['administrative_area']['#options'])) {
  341. $states[$country] = $format['locality_block']['administrative_area']['#options'];
  342. }
  343. else {
  344. $states[$country] = array();
  345. }
  346. cache_set('addressfield_tokens_states:' . $langcode, $states);
  347. }
  348. return $states[$country];
  349. }
  350. /**
  351. * Gets the name of the state with the given abbreviation.
  352. *
  353. * @param string $country
  354. * The 2-letter abbreviation of the country.
  355. * @param string $state
  356. * The 2-letter abbreviation of the state.
  357. *
  358. * @return string
  359. * The name of the state, or FALSE.
  360. */
  361. function addressfield_tokens_state($country, $state) {
  362. $states = addressfield_tokens_states($country);
  363. // State abbreviations will usually be two uppercase letters.
  364. $state_upper = drupal_strtoupper($state);
  365. if (!empty($state_upper) && !empty($states[$state_upper])) {
  366. return check_plain($states[$state]);
  367. }
  368. return check_plain($state);
  369. }
  370. /**
  371. * Implements hook_webform_component_info().
  372. */
  373. function addressfield_tokens_webform_component_info() {
  374. $components = array();
  375. $components['addressfield'] = array(
  376. 'label' => t('Address'),
  377. 'description' => t('Address field.'),
  378. 'features' => array(
  379. // Add content to CSV downloads. Defaults to TRUE.
  380. 'csv' => TRUE,
  381. // Show this component in e-mailed submissions. Defaults to TRUE.
  382. 'email' => TRUE,
  383. // Allow this component to be used as an e-mail FROM or TO address.
  384. // Defaults to FALSE.
  385. 'email_address' => FALSE,
  386. // Allow this component to be used as an e-mail SUBJECT or FROM name.
  387. // Defaults to FALSE.
  388. 'email_name' => FALSE,
  389. // This component may be toggled as required or not. Defaults to TRUE.
  390. 'required' => TRUE,
  391. // This component has a title that can be toggled as displayed or not.
  392. 'title_display' => TRUE,
  393. // This component has a title that can be displayed inline.
  394. 'title_inline' => FALSE,
  395. // If this component can be used as a conditional SOURCE. All components
  396. // may always be displayed conditionally, regardless of this setting.
  397. // Defaults to TRUE.
  398. 'conditional' => FALSE,
  399. // If this component allows other components to be grouped within it
  400. // (like a fieldset or tabs). Defaults to FALSE.
  401. 'group' => FALSE,
  402. // If this component can be used for SPAM analysis, usually with Mollom.
  403. 'spam_analysis' => FALSE,
  404. // If this component saves a file that can be used as an e-mail
  405. // attachment. Defaults to FALSE.
  406. 'attachment' => FALSE,
  407. ),
  408. 'file' => 'addressfield_tokens.components.inc',
  409. );
  410. return $components;
  411. }
  412. /**
  413. * Retrieves node components based on Node ID.
  414. * @param int $nid
  415. * Node ID.
  416. *
  417. * @return mixed
  418. * Components.
  419. */
  420. function _addressfield_tokens_webform_components($nid) {
  421. $components = &drupal_static(__FUNCTION__, array());
  422. if (!isset($components[$nid])) {
  423. $components[$nid] = db_select('webform_component')
  424. ->fields('webform_component')
  425. ->condition('type', 'addressfield')
  426. ->condition('nid', $nid)
  427. ->execute()
  428. ->fetchAllAssoc('cid', PDO::FETCH_ASSOC);
  429. }
  430. return $components[$nid];
  431. }
  432. /**
  433. * Implements hook_webform_submission_load().
  434. * Commented out to solve issue https://www.drupal.org/project/addressfield_tokens/issues/2674752
  435. *
  436. function addressfield_tokens_webform_submission_load(&$submissions) {
  437. $submissions_reset = reset($submissions);
  438. $nid = $submissions_reset->nid;
  439. $components = _addressfield_tokens_webform_components($nid);
  440. foreach ($submissions as $sid => $submission) {
  441. foreach ($components as $cid => $component) {
  442. if (!empty($submission->data[$cid])) {
  443. $parents = array($cid);
  444. if (!empty($submission->data[$cid]['value'])) {
  445. $parents[] = 'value';
  446. }
  447. $addresses = array();
  448. foreach (drupal_array_get_nested_value($submission->data, $parents) as $delta => $data) {
  449. $data = empty($data) ? array() : unserialize($data);
  450. $addresses[$delta] = $data;
  451. }
  452. if (count($addresses)) {
  453. drupal_array_set_nested_value($submission->data, $parents, $addresses);
  454. }
  455. }
  456. }
  457. }
  458. }
  459. **/
  460. /**
  461. * Implements hook_webform_validator_alter().
  462. */
  463. function addressfield_tokens_webform_validator_alter(&$validators) {
  464. $validators['unique']['component_types'][] = 'addressfield';
  465. $validators['oneoftwo']['component_types'][] = 'addressfield';
  466. $validators['oneofseveral']['component_types'][] = 'addressfield';
  467. }
  468. /**
  469. * Implements hook_webform_hints_element_alter().
  470. */
  471. function addressfield_tokens_webform_hints_element_alter(&$element, &$required_label)
  472. {
  473. if ($element['#type'] == 'addressfield_container' || $element['#type'] == 'fieldset') {
  474. $addressfield_tokens_fields_info = addressfield_tokens_field_formatter_info();
  475. foreach ($addressfield_tokens_fields_info['addressfield_components']['settings']['components'] as $component) {
  476. if (isset($element[$component])) {
  477. if (isset($element[$component]['#options'])) {
  478. $element[$component]['#empty_option'] = '- ' . $element[$component]['#title'] . $required_label . ' -';
  479. }
  480. $element[$component]['#attributes']['placeholder'] = $element[$component]['#title'] . $required_label;
  481. $element[$component]['#title_display'] = 'invisible';
  482. }
  483. }
  484. }
  485. }