This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
countries.module
142 lines (123 loc) · 3.7 KB
/
countries.module
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* @file
* Defines the field and entity information for countries.
*/
use Drupal\Component\Utility\Html;
/**
* Flag to return all countries.
*/
const COUNTRIES_ALL = 0;
/**
* Flag to return enabled countries only.
*/
const COUNTRIES_ENABLED = 1;
/**
* Flag to return disabled countries only.
*/
const COUNTRIES_DISABLED = 2;
/**
* Internal flag for views_handler_filter_countries_list class.
*/
const COUNTRIES_VIEWS_WIDGET_FIELD = 1;
/**
* Internal flag for views_handler_filter_countries_list class.
*/
const COUNTRIES_VIEWS_WIDGET_CUSTOM = 2;
/**
* Implements hook_theme().
*/
function countries_theme() {
return [
'countries_number' => [],
];
}
/**
* Implements hook_field_widget_info_alter().
*/
function countries_field_widget_info_alter(array &$info) {
if (isset($info['options_buttons'])) {
$info['options_buttons']['field_types'][] = 'country';
$info['options_select']['field_types'][] = 'country';
}
}
/**
* Themes ISO numeric-3 codes to a '0' padded string three characters long.
*/
function theme_countries_number($variables) {
if (isset($variables['country']) && !empty($variables['country']->numcode)) {
return sprintf("%03s", $variables['country']->numcode);
}
return '';
}
/**
* Implements hook_token_info().
*/
function countries_token_info() {
$types['country'] = [
'name' => t('Countries'),
'description' => t('Tokens related to individual countries.'),
'needs-data' => 'country',
];
$types['default-country'] = [
'name' => t('Default country'),
'description' => t('Tokens related to the sites default country.'),
'type' => 'country',
];
$core_properties = ['name', 'official_name', 'iso2', 'iso3', 'numcode'];
$country['name'] = [
'name' => $core_properties['name'],
'description' => t('The name of the country.'),
];
$country['official-name'] = [
'name' => $core_properties['official_name'],
'description' => t('The official name of the country.'),
];
$country['iso2'] = [
'name' => $core_properties['iso2'],
'description' => t('The @property of the country.', ['@property' => $core_properties['iso2']]),
];
$country['iso3'] = [
'name' => $core_properties['iso3'],
'description' => t('The @property of the country.', ['@property' => $core_properties['iso3']]),
];
$country['numcode'] = [
'name' => $core_properties['numcode'],
'description' => t('The @property of the country.', ['@property' => $core_properties['numcode']]),
];
$country['continent'] = [
'name' => $core_properties['continent'],
'description' => t('The @property of the country.', ['@property' => $core_properties['continent']]),
];
$country['continent-code'] = [
'name' => t('Continent code'),
'description' => t('The Continent code of the country.'),
];
$country['enabled'] = [
'name' => $core_properties['enabled'],
'description' => t('The @property of the country.', ['@property' => $core_properties['continent']]),
];
return [
'types' => $types,
'tokens' => ['country' => $country],
];
}
/**
* Implements hook_tokens().
*/
function countries_tokens($type, $tokens, array $data = array(), array $options = array()) {
$sanitize = !empty($options['sanitize']);
$replacements = [];
if ($type == 'country' && !empty($data['country'])) {
/** @var \Drupal\countries\CountryInterface $country */
$country = $data['country'];
foreach ($tokens as $name => $original) {
$value = $country->get(str_replace('-', '_', $name));
$replacements[$original] = $sanitize && $value ? Html::escape($value) : $value;
}
}
if ($type == 'default-country') {
// @todo Add default country support in countries_configuration module.
}
return $replacements;
}