array( 'arguments' => array('element' => NULL), ), 'field_formatter_list_key' => array( 'arguments' => array('element' => NULL), ), ); } /** * Implement hook_field_info(). */ function list_field_info() { return array( 'list' => array( 'label' => t('List'), '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.'), 'settings' => array('allowed_values' => '', 'allowed_values_function' => ''), 'default_widget' => 'options_select', 'default_formatter' => 'list_default', ), 'list_boolean' => array( 'label' => t('Boolean'), 'description' => t('This field stores simple on/off or yes/no options.'), 'settings' => array('allowed_values' => '', 'allowed_values_function' => ''), 'default_widget' => 'options_select', 'default_formatter' => 'list_default', ), 'list_number' => array( 'label' => t('List (numeric)'), '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.'), 'settings' => array('allowed_values' => '', 'allowed_values_function' => ''), 'default_widget' => 'options_select', 'default_formatter' => 'list_default', ), 'list_text' => array( 'label' => t('List (text)'), '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'), 'settings' => array('allowed_values' => '', 'allowed_values_function' => ''), 'default_widget' => 'options_select', 'default_formatter' => 'list_default', ), ); } /** * Implement hook_field_schema(). */ function list_field_schema($field) { switch ($field['type']) { case 'list_text': $columns = array( 'value' => array( 'type' => 'varchar', 'length' => 255, 'not null' => FALSE, ), ); break; case 'list_number': $columns = array( 'value' => array( 'type' => 'float', 'unsigned' => TRUE, 'not null' => FALSE, ), ); break; default: $columns = array( 'value' => array( 'type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE, ), ); break; } return array( 'columns' => $columns, 'indexes' => array( 'value' => array('value'), ), ); } /** * Implement hook_field_settings_form(). */ function list_field_settings_form($field, $instance) { $settings = $field['settings']; $form['allowed_values'] = array( '#type' => 'textarea', '#title' => t('Allowed values list'), '#default_value' => $settings['allowed_values'], '#required' => FALSE, '#rows' => 10, '#description' => '

' . 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'))) . '

', '#element_validate' => array('list_allowed_values_validate'), '#list_field_type' => $field['type'], '#access' => empty($settings['allowed_values_function']), ); // Alter the description for allowed values depending on the widget type. if ($instance['widget']['type'] == 'options_onoff') { $form['allowed_values']['#description'] .= '

' . t("For a 'single on/off checkbox' widget, define the 'off' value first, then the 'on' value in the Allowed values section. Note that the checkbox will be labeled with the label of the 'on' value.") . '

'; } elseif ($instance['widget']['type'] == 'options_buttons') { $form['allowed_values']['#description'] .= '

' . t("The 'checkboxes/radio buttons' widget will display checkboxes if the Number of values option is greater than 1 for this field, otherwise radios will be displayed.") . '

'; } $form['allowed_values']['#description'] .= t('Allowed HTML tags in labels: @tags', array('@tags' => _field_filter_xss_display_allowed_tags())); $form['allowed_values_function'] = array( '#type' => 'value', '#value' => $settings['allowed_values_function'], ); $form['allowed_values_function_display'] = array( '#type' => 'item', '#title' => t('Allowed values list'), '#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'])), '#access' => !empty($settings['allowed_values_function']), ); return $form; } /** * Create an array of allowed values for this field. */ function list_allowed_values($field) { $allowed_values = drupal_static(__FUNCTION__, array()); if (isset($allowed_values[$field['field_name']])) { return $allowed_values[$field['field_name']]; } $allowed_values[$field['field_name']] = array(); $function = $field['settings']['allowed_values_function']; if (!empty($function) && function_exists($function)) { $allowed_values[$field['field_name']] = $function($field); } elseif (!empty($field['settings']['allowed_values'])) { $allowed_values[$field['field_name']] = list_allowed_values_list($field['settings']['allowed_values'], $field['type'] == 'list'); } return $allowed_values[$field['field_name']]; } /** * Create an array of the allowed values for this field. * * Explode a string with keys and labels separated with '|' and with each new * value on its own line. * * @param $string_values * The list of choices as a string. * @param $position_keys * Boolean value indicating whether to generate keys based on the position of * the value if a key is not manually specified, effectively generating * integer-based keys. This should only be TRUE for fields that have a type of * "list". Otherwise the value will be used as the key if not specified. */ function list_allowed_values_list($string_values, $position_keys = FALSE) { $allowed_values = array(); $list = explode("\n", $string_values); $list = array_map('trim', $list); $list = array_filter($list, 'strlen'); foreach ($list as $key => $value) { // Sanitize the user input with a permissive filter. $value = field_filter_xss($value); // Check for a manually specified key. if (strpos($value, '|') !== FALSE) { list($key, $value) = explode('|', $value); } // Otherwise see if we need to use the value as the key. The "list" type // will automatically convert non-keyed lines to integers. elseif (!$position_keys) { $key = $value; } $allowed_values[$key] = (isset($value) && $value !== '') ? $value : $key; } return $allowed_values; } /** * Element validate callback; check that the entered values are valid. */ function list_allowed_values_validate($element, &$form_state) { $values = list_allowed_values_list($element['#value'], $element['#list_field_type'] == 'list'); $field_type = $element['#list_field_type']; foreach ($values as $key => $value) { if ($field_type == 'list_number' && !is_numeric($key)) { form_error($element, t('The entered available values are not valid. Each key must be a valid integer or decimal.')); break; } elseif ($field_type == 'list_text' && strlen($key) > 255) { form_error($element, t('The entered available values are not valid. Each key must be a string less than 255 characters.')); break; } elseif ($field_type == 'list' && (!preg_match('/^-?\d+$/', $key))) { form_error($element, t('The entered available values are not valid. All specified keys must be integers.')); break; } } } /** * Implement hook_field_validate(). * * Possible error codes: * - 'list_illegal_value': The value is not part of the list of allowed values. */ function list_field_validate($obj_type, $object, $field, $instance, $langcode, $items, &$errors) { $allowed_values = list_allowed_values($field); foreach ($items as $delta => $item) { if (!empty($item['value'])) { if (count($allowed_values) && !array_key_exists($item['value'], $allowed_values)) { $errors[$field['field_name']][$langcode][$delta][] = array( 'error' => 'list_illegal_value', 'message' => t('%name: illegal value.', array('%name' => t($instance['label']))), ); } } } } /** * Implement hook_field_is_empty(). */ function list_field_is_empty($item, $field) { if (empty($item['value']) && (string)$item['value'] !== '0') { return TRUE; } return FALSE; } /** * Implement hook_field_formatter_info(). */ function list_field_formatter_info() { return array( 'list_default' => array( 'label' => t('Default'), 'field types' => array('list', 'list_boolean', 'list_text', 'list_number'), ), 'list_key' => array( 'label' => t('Key'), 'field types' => array('list', 'list_boolean', 'list_text', 'list_number'), ), ); } /** * Theme function for 'default' list field formatter. */ function theme_field_formatter_list_default($element) { $field = field_info_field($element['#field_name']); if (($allowed_values = list_allowed_values($field)) && isset($allowed_values[$element['#item']['value']])) { return $allowed_values[$element['#item']['value']]; } // If no match was found in allowed values, fall back to the key. return $element['#item']['safe']; } /** * Theme function for 'key' list field formatter. */ function theme_field_formatter_list_key($element) { return $element['#item']['safe']; }