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