Simpletest Coverage - modules/field/modules/list/list.module

1 <?php
2 // $Id: list.module,v 1.7 2009/08/01 06:03:12 dries Exp $
3
4 /**
5 * @file
6 * Defines list field types that can be used with the Options module.
7 */
8
9 /**
10 * Implement hook_theme().
11 */
12 function list_theme() {
13 return array(
14 'field_formatter_list_default' => array(
15 'arguments' => array('element' => NULL),
16 ),
17 'field_formatter_list_key' => array(
18 'arguments' => array('element' => NULL),
19 ),
20 );
21 }
22
23 /**
24 * Implement hook_field_info().
25 */
26 function list_field_info() {
27 return array(
28 'list' => array(
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 'list_boolean' => array(
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 'list_number' => array(
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 'list_text' => array(
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 * Implement hook_field_schema().
61 */
62 function list_field_schema($field) {
63 switch ($field['type']) {
64 case 'list_text':
65 $columns = array(
66 'value' => array(
67 'type' => 'varchar',
68 'length' => 255,
69 'not null' => FALSE,
70 ),
71 );
72 break;
73 case 'list_number':
74 $columns = array(
75 'value' => array(
76 'type' => 'float',
77 'unsigned' => TRUE,
78 'not null' => FALSE,
79 ),
80 );
81 break;
82 default:
83 $columns = array(
84 'value' => array(
85 'type' => 'int',
86 'unsigned' => TRUE,
87 'not null' => FALSE,
88 ),
89 );
90 break;
91 }
92 return array(
93 'columns' => $columns,
94 'indexes' => array(
95 'value' => array('value'),
96 ),
97 );
98 }
99
100 /**
101 * Implement hook_field_validate().
102 *
103 * Possible error codes:
104 * - 'list_illegal_value': The value is not part of the list of allowed values.
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 * Implement hook_field_is_empty().
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 * Implement hook_field_formatter_info().
132 */
133 function list_field_formatter_info() {
134 return array(
135 'list_default' => array(
136 'label' => t('Default'),
137 'field types' => array('list', 'list_boolean', 'list_text', 'list_number'),
138 ),
139 'list_key' => array(
140 'label' => t('Key'),
141 'field types' => array('list', 'list_boolean', 'list_text', 'list_number'),
142 ),
143 );
144 }
145
146 /**
147 * Theme function for 'default' list field formatter.
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 // If no match was found in allowed values, fall back to the key.
155 return $element['#item']['safe'];
156 }
157
158 /**
159 * Theme function for 'key' list field formatter.
160 */
161 function theme_field_formatter_list_key($element) {
162 return $element['#item']['safe'];
163 }
164
165 /**
166 * Create an array of the allowed values for this field.
167 *
168 * Call the allowed_values_function to retrieve the allowed
169 * values array.
170 *
171 * TODO Rework this to create a method of selecting plugable allowed values lists.
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

Legend

Missed
lines code that were not excersized during program execution.
Covered
lines code were excersized during program execution.
Comment/non executable
Comment or non-executable line of code.
Dead
lines of code that according to xdebug could not be executed. This is counted as coverage code because in almost all cases it is code that runnable.