address-hide-country.inc 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. // Hide the country element only if the whole field is required, otherwise
  19. // there will always be an additional None option.
  20. if ($context['mode'] == 'form' && (empty($context['instance']) || $context['instance']['required'])) {
  21. if (!empty($format['country']['#options']) && count($format['country']['#options']) == 1) {
  22. $format['country']['#access'] = FALSE;
  23. }
  24. }
  25. elseif ($context['mode'] == 'render') {
  26. // However, in render mode, the element does not have an #options list, so
  27. // we look instead in the field instance settings if given. If we find a
  28. // single country option and it matches the country of the current address,
  29. // go ahead and hide it.
  30. if (!empty($context['instance']['widget']['settings']['available_countries']) &&
  31. count($context['instance']['widget']['settings']['available_countries']) == 1) {
  32. if (isset($context['instance']['widget']['settings']['available_countries'][$address['country']])) {
  33. $format['country']['#access'] = FALSE;
  34. }
  35. }
  36. }
  37. }