Projet

Général

Profil

Paste
Télécharger (1,45 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / addressfield / plugins / format / address-hide-country.inc @ dc45a079

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
  // When building the format for a form, we expect the country element to have
22
  // an #options list. If it does, and there is only one option, hide the field
23
  // by setting #access to FALSE.
24
  if ($context['mode'] == 'form') {
25
    if (!empty($format['country']['#options']) && count($format['country']['#options']) == 1) {
26
      $format['country']['#access'] =  FALSE;
27
    }
28
  }
29
  elseif ($context['mode'] == 'render') {
30
    // However, in render mode, the element does not have an #options list, so
31
    // we look instead in the field instance settings if given. If we find a
32
    // single country option and it matches the country of the current address,
33
    // go ahead and hide it.
34
    if (!empty($context['instance']['widget']['settings']['available_countries']) &&
35
      count($context['instance']['widget']['settings']['available_countries']) == 1) {
36
      if (isset($context['instance']['widget']['settings']['available_countries'][$address['country']])) {
37
        $format['country']['#access'] = FALSE;
38
      }
39
    }
40
  }
41
}