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