| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
function list_theme() {
|
| 13 |
|
| 14 |
|
| 15 |
'arguments' => array('element' => NULL),
|
| 16 |
),
|
| 17 |
|
| 18 |
'arguments' => array('element' => NULL),
|
| 19 |
),
|
| 20 |
);
|
| 21 |
}
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
function list_field_info() {
|
| 27 |
|
| 28 |
|
| 29 |
'label' => t('List'),
|
| 30 |
'description' => t('This field stores numeric keys from key/value lists of allowed values where the key is a simple alias for the position of the value, i.e. 0|First option, 1|Second option, 2|Third option.'),
|
| 31 |
'settings' => array('allowed_values_function' => ''),
|
| 32 |
'default_widget' => 'options_select',
|
| 33 |
'default_formatter' => 'list_default',
|
| 34 |
),
|
| 35 |
|
| 36 |
'label' => t('Boolean'),
|
| 37 |
'description' => t('This field stores simple on/off or yes/no options.'),
|
| 38 |
'settings' => array('allowed_values_function' => ''),
|
| 39 |
'default_widget' => 'options_select',
|
| 40 |
'default_formatter' => 'list_default',
|
| 41 |
),
|
| 42 |
|
| 43 |
'label' => t('List (numeric)'),
|
| 44 |
'description' => t('This field stores keys from key/value lists of allowed numbers where the stored numeric key has significance and must be preserved, i.e. \'Lifetime in days\': 1|1 day, 7|1 week, 31|1 month.'),
|
| 45 |
'settings' => array('allowed_values_function' => ''),
|
| 46 |
'default_widget' => 'options_select',
|
| 47 |
'default_formatter' => 'list_default',
|
| 48 |
),
|
| 49 |
|
| 50 |
'label' => t('List (text)'),
|
| 51 |
'description' => t('This field stores keys from key/value lists of allowed values where the stored key has significance and must be a varchar, i.e. \'US States\': IL|Illinois, IA|Iowa, IN|Indiana'),
|
| 52 |
'settings' => array('allowed_values_function' => ''),
|
| 53 |
'default_widget' => 'options_select',
|
| 54 |
'default_formatter' => 'list_default',
|
| 55 |
),
|
| 56 |
);
|
| 57 |
}
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
function list_field_schema($field) {
|
| 63 |
switch ($field['type']) {
|
| 64 |
case 'list_text':
|
| 65 |
|
| 66 |
|
| 67 |
'type' => 'varchar',
|
| 68 |
'length' => 255,
|
| 69 |
'not null' => FALSE,
|
| 70 |
),
|
| 71 |
);
|
| 72 |
break;
|
| 73 |
case 'list_number':
|
| 74 |
|
| 75 |
|
| 76 |
'type' => 'float',
|
| 77 |
'unsigned' => TRUE,
|
| 78 |
'not null' => FALSE,
|
| 79 |
),
|
| 80 |
);
|
| 81 |
break;
|
| 82 |
default:
|
| 83 |
|
| 84 |
|
| 85 |
'type' => 'int',
|
| 86 |
'unsigned' => TRUE,
|
| 87 |
'not null' => FALSE,
|
| 88 |
),
|
| 89 |
);
|
| 90 |
break;
|
| 91 |
}
|
| 92 |
|
| 93 |
'columns' => $columns,
|
| 94 |
|
| 95 |
'value' => array('value'),
|
| 96 |
),
|
| 97 |
);
|
| 98 |
}
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
function list_field_validate($obj_type, $object, $field, $instance, $items, &$errors) {
|
| 107 |
$allowed_values = list_allowed_values($field);
|
| 108 |
foreach ($items as $delta => $item) {
|
| 109 |
if (!empty($item['value'])) {
|
| 110 |
if (count($allowed_values) && !array_key_exists($item['value'], $allowed_values)) {
|
| 111 |
$errors[$field['field_name']][$delta][] = array(
|
| 112 |
'error' => 'list_illegal_value',
|
| 113 |
'message' => t('%name: illegal value.', array('%name' => t($instance['label']))),
|
| 114 |
|
| 115 |
}
|
| 116 |
}
|
| 117 |
}
|
| 118 |
}
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
function list_field_is_empty($item, $field) {
|
| 124 |
if (empty($item['value']) && (string)$item['value'] !== '0') {
|
| 125 |
return TRUE;
|
| 126 |
}
|
| 127 |
return FALSE;
|
| 128 |
}
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
function list_field_formatter_info() {
|
| 134 |
|
| 135 |
|
| 136 |
'label' => t('Default'),
|
| 137 |
'field types' => array('list', 'list_boolean', 'list_text', 'list_number'),
|
| 138 |
),
|
| 139 |
|
| 140 |
'label' => t('Key'),
|
| 141 |
'field types' => array('list', 'list_boolean', 'list_text', 'list_number'),
|
| 142 |
),
|
| 143 |
);
|
| 144 |
}
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
function theme_field_formatter_list_default($element) {
|
| 150 |
$field = field_info_field($element['#field_name']);
|
| 151 |
if (($allowed_values = list_allowed_values($field)) && isset($allowed_values[$element['#item']['value']])) {
|
| 152 |
return $allowed_values[$element['#item']['value']];
|
| 153 |
}
|
| 154 |
|
| 155 |
return $element['#item']['safe'];
|
| 156 |
}
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
function theme_field_formatter_list_key($element) {
|
| 162 |
return $element['#item']['safe'];
|
| 163 |
}
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
function list_allowed_values($field) {
|
| 174 |
static $allowed_values;
|
| 175 |
|
| 176 |
if (isset($allowed_values[$field['field_name']])) {
|
| 177 |
return $allowed_values[$field['field_name']];
|
| 178 |
}
|
| 179 |
|
| 180 |
$allowed_values[$field['field_name']] = array();
|
| 181 |
|
| 182 |
if (isset($field['settings']['allowed_values_function'])) {
|
| 183 |
$function = $field['settings']['allowed_values_function'];
|
| 184 |
if (drupal_function_exists($function)) {
|
| 185 |
$allowed_values[$field['field_name']] = $function($field);
|
| 186 |
}
|
| 187 |
}
|
| 188 |
return $allowed_values[$field['field_name']];
|
| 189 |
}
|
| 190 |
|