/[drupal]/contributions/modules/cck/includes/cck.list.inc
ViewVC logotype

Contents of /contributions/modules/cck/includes/cck.list.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.7 - (show annotations) (download) (as text)
Sat Mar 7 18:34:21 2009 UTC (8 months, 3 weeks ago) by karens
Branch: MAIN
Changes since 1.6: +2 -2 lines
File MIME type: text/x-php
No such value as $field['allowed_values'], that's a cck setting.
1 <?php
2 // $Id: cck.list.inc,v 1.6 2009/02/03 21:11:58 karens Exp $
3
4 /**
5 * A custom function to return allowed values from PHP code
6 * or by exploding a text list.
7 */
8 function cck_list_allowed_values_php($field) {
9 $allowed_values = array();
10 $php = cck_field_get_setting('allowed_values_php', 'field', $field);
11 if (!empty($php)) {
12 ob_start();
13 $result = eval($php);
14 if (is_array($result)) {
15 $allowed_values = $result;
16 }
17 ob_end_clean();
18 }
19 else {
20 $allowed_values = cck_allowed_values($field);
21 }
22 return $allowed_values;
23 }
24
25 /**
26 * Implementation of hook_field_settings_form()
27 * on behalf of core List module.
28 */
29 function list_field_settings_form($field, $instance) {
30
31 // Alter the description for allowed values slightly depending
32 // on the type of widget.
33 $widget_type = $instance['widget']['type'];
34 $field_type = $field['type'];
35 $label = $instance['label'];
36 $description = '<p>' . t('Create a list of options as a list or a function name. These values will be the same for %field in all field types.', array('%field' => $label)) . '</p>';
37 if ($widget_type == 'options_onoff') {
38 $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>';
39 }
40 elseif ($widget_type == 'options_buttons') {
41 $description .= '<p>' . t("The 'checkboxes/radio buttons' widget will display checkboxes if the cardinality option is selected for this field, otherwise radios will be displayed.") . '</p>';
42 }
43
44 $form = array(
45 '#element_validate' => array('list_field_settings_form_validate'),
46 '#prefix' => '<div class="form-item"><label>' . t('Allowed values') . ':</label><div class="description">' . $description . '</div></div>',
47 '#field' => $field,
48 );
49
50 // Retrieve allowed_values and allowed_values_php, which are non-core
51 // settings handled by CCK.
52 $allowed_values = cck_field_get_setting('allowed_values', 'field', $field);
53 $allowed_values_php = cck_field_get_setting('allowed_values_php', 'field', $field);
54
55 // Get the right values for allowed_values_function, which is a core setting.
56 $defaults = field_info_field_settings($field['type']);
57 $settings = array_merge($defaults, $field['settings']);
58 $allowed_values_function = !empty($settings['allowed_values_function']) && $settings['allowed_values_function'] != 'cck_list_allowed_values_php' ? $settings['allowed_values_function'] : '';
59 $form['allowed_values'] = array(
60 '#type' => 'textarea',
61 '#title' => t('Allowed values list'),
62 '#default_value' => $allowed_values,
63 '#required' => FALSE,
64 '#rows' => 10,
65 '#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.<br />Allowed HTML tags in labels: @tags', array('%type' => $field['type'] == 'list_text' ? 'text' : 'numeric', '@tags' => _field_filter_xss_display_allowed_tags())),
66 );
67 $form['allowed_values_function'] = array(
68 '#type' => 'textfield',
69 '#title' => t('Allowed values function'),
70 '#default_value' => $allowed_values_function,
71 '#description' => t('The name of a function that will return the allowed values list. If this field is filled out, the array returned by this code will override the allowed values list above.'),
72 );
73 $form['advanced_options'] = array(
74 '#type' => 'fieldset',
75 '#title' => t('PHP code for allowed values'),
76 '#collapsible' => TRUE,
77 '#collapsed' => empty($allowed_values_php),
78 );
79 if (user_access('Use PHP input for field settings (dangerous - grant with care)')) {
80 $form['advanced_options']['allowed_values_php'] = array(
81 '#type' => 'textarea',
82 '#title' => t('Code'),
83 '#default_value' => $allowed_values_php,
84 '#rows' => 6,
85 '#description' => t("Advanced usage only: PHP code that returns a keyed array of allowed values. Should not include &lt;?php ?&gt; delimiters. If this field is filled out, the array returned by this code will override the allowed values list, and the allowed values function will be set to 'cck_list_allowed_values_php' to execute this custom code. <strong>It is strongly recommended that you move this code to a custom function in a custom module and simply identify the custom function in the box above!</strong>"),
86 );
87 }
88 else {
89 $form['advanced_options']['allowed_values_php'] = array(
90 '#type' => 'value',
91 '#value' => $allowed_values_php,
92 );
93 $form['advanced_options']['markup_allowed_values_php'] = array(
94 '#type' => 'item',
95 '#title' => t('Code'),
96 '#value' => !empty($allowed_values_php) ? '<code>'. check_plain($allowed_values_php) .'</code>' : t('&lt;none&gt;'),
97 '#description' => empty($allowed_values_php) ? t("You're not allowed to input PHP code.") : t('This PHP code was set by an administrator and will override the allowed values list and allowed values functions shown above.'),
98 );
99 }
100
101 return $form;
102 }
103
104 /**
105 * Handle Allowed values PHP code.
106 */
107 function list_field_settings_form_validate($form, &$form_state) {
108 $form_values = $form_state['values'];
109 $field = $form['#field'];
110
111 // Store allowed_values and allowed_values_php in the CCK table,
112 // they are not core settings. CCK stores them and will return the
113 // right value in response to the hook.
114 $option = $form_values['field']['settings']['advanced_options']['allowed_values_php'];
115 cck_field_set_setting('allowed_values_php', 'field', $option, $field);
116 $option = $form_values['field']['settings']['allowed_values'];
117 cck_field_set_setting('allowed_values', 'field', $option, $field);
118
119 // Set allowed_values_function, which is a core setting.
120 $new_values['allowed_values_function'] = $form_values['field']['settings']['allowed_values_function'];
121 if (empty($new_values['allowed_values_function'])) {
122 $new_values['allowed_values_function'] = 'cck_list_allowed_values_php';
123 }
124 form_set_value($form, $new_values, $form_state);
125 }
126
127 /**
128 * Create an array of the allowed values for this field.
129 *
130 * Explode a string with keys and labels separated
131 * with '|' and with each new value on its own line.
132 */
133 function cck_allowed_values($field) {
134 $allowed_values[$field['field_name']] = array();
135
136 $list = cck_field_get_setting('allowed_values', 'field', $field);
137 $list = explode("\n", $list);
138 $list = array_map('trim', $list);
139 $list = array_filter($list, 'strlen');
140 foreach ($list as $opt) {
141 // Sanitize the user input with a permissive filter.
142 $opt = field_filter_xss($opt);
143 if (strpos($opt, '|') !== FALSE) {
144 list($key, $value) = explode('|', $opt);
145 $allowed_values[$field['field_name']][$key] = (isset($value) && $value !=='') ? $value : $key;
146 }
147 else {
148 $allowed_values[$field['field_name']][$opt] = $opt;
149 }
150 }
151 return $allowed_values[$field['field_name']];
152 }

  ViewVC Help
Powered by ViewVC 1.1.2