6 * Defines list field types that can be used with the Options module.
10 * Implements hook_help().
12 function list_help($path, $arg) {
14 case
'admin/help#list':
16 $output .
= '<h3>' .
t('About') .
'</h3>';
17 $output .
= '<p>' .
t('The List module defines various fields for storing a list of items, for use with the Field module. Usually these items are entered through a select list, checkboxes, or radio buttons. See the <a href="@field-help">Field module help page</a> for more information about fields.', array('@field-help' => url('admin/help/field'))) .
'</p>';
23 * Implements hook_field_info().
25 function list_field_info() {
29 '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.'),
30 'settings' => array('allowed_values' => '', 'allowed_values_function' => ''),
31 'default_widget' => 'options_select',
32 'default_formatter' => 'list_default',
34 'list_boolean' => array(
35 'label' => t('Boolean'),
36 'description' => t('This field stores simple on/off or yes/no options.'),
37 'settings' => array('allowed_values' => '', 'allowed_values_function' => ''),
38 'default_widget' => 'options_buttons',
39 'default_formatter' => 'list_default',
41 'list_number' => array(
42 'label' => t('List (numeric)'),
43 '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.'),
44 'settings' => array('allowed_values' => '', 'allowed_values_function' => ''),
45 'default_widget' => 'options_select',
46 'default_formatter' => 'list_default',
49 'label' => t('List (text)'),
50 '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'),
51 'settings' => array('allowed_values' => '', 'allowed_values_function' => ''),
52 'default_widget' => 'options_select',
53 'default_formatter' => 'list_default',
59 * Implements hook_field_schema().
61 function list_field_schema($field) {
62 switch ($field['type']) {
92 'columns' => $columns,
94 'value' => array('value'),
100 * Implements hook_field_settings_form().
102 * @todo: If $has_data, add a form validate function to verify that the
103 * new allowed values do not exclude any keys for which data already
104 * exists in the databae (use field_attach_query()) to find out.
105 * Implement the validate function via hook_field_update_forbid() so
106 * list.module does not depend on form submission.
108 function list_field_settings_form($field, $instance, $has_data) {
109 $settings = $field['settings'];
111 $form['allowed_values'] = array(
112 '#type' => 'textarea',
113 '#title' => t('Allowed values list'),
114 '#default_value' => $settings['allowed_values'],
115 '#required' => FALSE
,
117 '#description' => '<p>' .
t('The possible values this field can contain. Enter one value per line, in the format key|label. The key is the value that will be stored in the database, and must be a %type value. The label is optional, and the key will be used as the label if no label is specified.', array('%type' => $field['type'] == 'list_text' ?
t('text') : t('numeric'))) .
'</p>',
118 '#element_validate' => array('list_allowed_values_setting_validate'),
119 '#list_field_type' => $field['type'],
120 '#access' => empty($settings['allowed_values_function']),
123 // Alter the description for allowed values depending on the widget type.
124 if ($instance['widget']['type'] == 'options_onoff') {
125 $form['allowed_values']['#description'] .
= '<p>' .
t("For a 'single on/off checkbox' widget, define the 'off' value first, then the 'on' value in the <strong>Allowed values</strong> section. Note that the checkbox will be labeled with the label of the 'on' value.") .
'</p>';
127 elseif ($instance['widget']['type'] == 'options_buttons') {
128 $form['allowed_values']['#description'] .
= '<p>' .
t("The 'checkboxes/radio buttons' widget will display checkboxes if the <em>Number of values</em> option is greater than 1 for this field, otherwise radios will be displayed.") .
'</p>';
130 $form['allowed_values']['#description'] .
= t('Allowed HTML tags in labels: @tags', array('@tags' => _field_filter_xss_display_allowed_tags()));
132 $form['allowed_values_function'] = array(
134 '#value' => $settings['allowed_values_function'],
136 $form['allowed_values_function_display'] = array(
138 '#title' => t('Allowed values list'),
139 '#markup' => t('The value of this field is being determined by the %function function and may not be changed.', array('%function' => $settings['allowed_values_function'])),
140 '#access' => !empty($settings['allowed_values_function']),
147 * Element validate callback; check that the entered values are valid.
149 function list_allowed_values_setting_validate($element, &$form_state) {
150 $values = list_extract_allowed_values($element['#value'], $element['#list_field_type'] == 'list');
151 $field_type = $element['#list_field_type'];
153 // Check that keys are valid for the field type.
154 foreach ($values as
$key => $value) {
155 if ($field_type == 'list_number' && !is_numeric($key)) {
156 form_error($element, t('Allowed values list: each key must be a valid integer or decimal.'));
159 elseif ($field_type == 'list_text' && drupal_strlen($key) > 255) {
160 form_error($element, t('Allowed values list: each key must be a string at most 255 characters long.'));
163 elseif ($field_type == 'list' && !preg_match('/^-?\d+$/', $key)) {
164 form_error($element, t('Allowed values list: keys must be integers.'));
167 elseif ($field_type == 'list_boolean' && !in_array($key, array('0', '1'))) {
168 form_error($element, t('Allowed values list: keys must be either 0 or 1.'));
173 // Check that boolean fields get two values.
174 if ($field_type == 'list_boolean' && count($values) != 2) {
175 form_error($element, t('Allowed values list: two values are required.'));
180 * Implements hook_field_update_field().
182 function list_field_update_field($field, $prior_field, $has_data) {
183 drupal_static_reset('list_allowed_values');
187 * Returns the set of allowed values for a list field.
189 * The strings are not safe for output. Keys and values of the array should be
190 * sanitized through field_filter_xss() before being displayed.
193 * The field definition.
196 * The array of allowed values. Keys of the array are the raw stored values
197 * (integer or text), values of the array are the display aliases.
199 function list_allowed_values($field) {
200 $allowed_values = &drupal_static(__FUNCTION__
, array());
202 if (!isset($allowed_values[$field['id']])) {
205 $function = $field['settings']['allowed_values_function'];
206 if (!empty($function) && function_exists($function)) {
207 $values = $function($field);
209 elseif (!empty($field['settings']['allowed_values'])) {
210 $position_keys = $field['type'] == 'list';
211 $values = list_extract_allowed_values($field['settings']['allowed_values'], $position_keys);
214 $allowed_values[$field['id']] = $values;
217 return $allowed_values[$field['id']];
221 * Generates an array of values from a string.
223 * Explode a string with keys and labels separated with '|' and with each new
224 * value on its own line.
226 * @param $string_values
227 * The list of choices as a string, in the format expected by the
228 * 'allowed_values' setting:
229 * - Values are separated by a carriage return.
230 * - Each value is in the format "value|label" or "value".
231 * @param $position_keys
232 * Boolean value indicating whether to generate keys based on the position of
233 * the value if a key is not manually specified, effectively generating
234 * integer-based keys. This should only be TRUE for fields that have a type of
235 * "list". Otherwise the value will be used as the key if not specified.
237 function list_extract_allowed_values($string_values, $position_keys = FALSE
) {
240 $list = explode("\n", $string_values);
241 $list = array_map('trim', $list);
242 $list = array_filter($list, 'strlen');
243 foreach ($list as
$key => $value) {
244 // Check for a manually specified key.
245 if (strpos($value, '|') !== FALSE
) {
246 list($key, $value) = explode('|', $value);
248 // Otherwise see if we need to use the value as the key. The "list" type
249 // will automatically convert non-keyed lines to integers.
250 elseif (!$position_keys) {
253 $values[$key] = (isset($value) && $value !== '') ?
$value : $key;
260 * Implements hook_field_validate().
262 * Possible error codes:
263 * - 'list_illegal_value': The value is not part of the list of allowed values.
265 function list_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
266 $allowed_values = list_allowed_values($field);
267 foreach ($items as
$delta => $item) {
268 if (!empty($item['value'])) {
269 if (count($allowed_values) && !array_key_exists($item['value'], $allowed_values)) {
270 $errors[$field['field_name']][$langcode][$delta][] = array(
271 'error' => 'list_illegal_value',
272 'message' => t('%name: illegal value.', array('%name' => t($instance['label']))),
280 * Implements hook_field_is_empty().
282 function list_field_is_empty($item, $field) {
283 if (empty($item['value']) && (string)$item['value'] !== '0') {
290 * Implements hook_field_widget_info_alter().
292 * The List module does not implement widgets of its own, but reuses the
293 * widgets defined in options.module.
295 * @see list_options_list().
297 function list_field_widget_info_alter(&$info) {
299 'options_select' => array('list', 'list_text', 'list_number'),
300 'options_buttons' => array('list', 'list_text', 'list_number', 'list_boolean'),
301 'options_onoff' => array('list_boolean'),
304 foreach ($widgets as
$widget => $field_types) {
305 $info[$widget]['field types'] = array_merge($info[$widget]['field types'], $field_types);
310 * Implements hook_options_list().
312 function list_options_list($field) {
313 return list_allowed_values($field);
317 * Implements hook_field_formatter_info().
319 function list_field_formatter_info() {
321 'list_default' => array(
322 'label' => t('Default'),
323 'field types' => array('list', 'list_boolean', 'list_text', 'list_number'),
327 'field types' => array('list', 'list_boolean', 'list_text', 'list_number'),
333 * Implements hook_field_formatter_view().
335 function list_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
338 switch ($display['type']) {
340 $allowed_values = list_allowed_values($field);
341 foreach ($items as
$delta => $item) {
342 if (isset($allowed_values[$item['value']])) {
343 $output = field_filter_xss($allowed_values[$item['value']]);
346 // If no match was found in allowed values, fall back to the key.
347 $output = field_filter_xss($item['value']);
349 $element[$delta] = array('#markup' => $output);
354 foreach ($items as
$delta => $item) {
355 $element[$delta] = array('#markup' => field_filter_xss($item['value']));