address-hide-country.inc 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. /**
  3. * @file
  4. * Hide the country when only one country is available.
  5. */
  6. $plugin = array(
  7. 'title' => t('Hide the country when only one is available'),
  8. 'format callback' => 'addressfield_format_address_hide_country',
  9. 'type' => 'address',
  10. 'weight' => -80,
  11. );
  12. /**
  13. * Format callback.
  14. *
  15. * @see CALLBACK_addressfield_format_callback()
  16. */
  17. function addressfield_format_address_hide_country(&$format, $address, $context = array()) {
  18. // When building the format for a form, we expect the country element to have
  19. // an #options list. If it does, and there is only one option, hide the field
  20. // by setting #access to FALSE.
  21. if ($context['mode'] == 'form') {
  22. if (!empty($format['country']['#options']) && count($format['country']['#options']) == 1) {
  23. $format['country']['#access'] = FALSE;
  24. }
  25. }
  26. elseif ($context['mode'] == 'render') {
  27. // However, in render mode, the element does not have an #options list, so
  28. // we look instead in the field instance settings if given. If we find a
  29. // single country option and it matches the country of the current address,
  30. // go ahead and hide it.
  31. if (!empty($context['instance']['widget']['settings']['available_countries']) &&
  32. count($context['instance']['widget']['settings']['available_countries']) == 1) {
  33. if (isset($context['instance']['widget']['settings']['available_countries'][$address['country']])) {
  34. $format['country']['#access'] = FALSE;
  35. }
  36. }
  37. }
  38. }