- Patch #913528 by chx, Berdir, catch, tstoeckler, yched, dereine: create new boolean field 'Cannot create references to/from string offsets nor overloaded objects'.

merge-requests/26/head
Dries Buytaert 2010-10-11 19:57:00 +00:00
parent 918e818964
commit ebaa90dc6b
2 changed files with 42 additions and 16 deletions

View File

@ -84,9 +84,8 @@ function list_field_settings_form($field, $instance, $has_data) {
$off_value = array_shift($values);
$on_value = array_shift($values);
$form['allowed_values'] = array(
'#type' => 'markup',
'#type' => 'value',
'#description' => '',
'#input' => TRUE,
'#value_callback' => 'list_boolean_allowed_values_callback',
'#access' => empty($settings['allowed_values_function']),
);
@ -96,6 +95,9 @@ function list_field_settings_form($field, $instance, $has_data) {
'#default_value' => $on_value,
'#required' => FALSE,
'#description' => t('If left empty, "1" will be used.'),
// Change #parents to make sure the element is not saved into field
// settings.
'#parents' => array('on'),
);
$form['allowed_values']['off'] = array(
'#type' => 'textfield',
@ -103,7 +105,14 @@ function list_field_settings_form($field, $instance, $has_data) {
'#default_value' => $off_value,
'#required' => FALSE,
'#description' => t('If left empty, "0" will be used.'),
// Change #parents to make sure the element is not saved into field
// settings.
'#parents' => array('off'),
);
// Link the allowed value to the on / off elements to prepare for the rare
// case of an alter changing #parents.
$form['allowed_values']['#on_parents'] = &$form['allowed_values']['on']['#parents'];
$form['allowed_values']['#off_parents'] = &$form['allowed_values']['off']['#parents'];
}
// Alter the description for allowed values depending on the widget type.
@ -156,13 +165,10 @@ function list_allowed_values_setting_validate($element, &$form_state) {
/**
* Form element #value_callback: assembles the allowed values for 'boolean' fields.
*/
function list_boolean_allowed_values_callback($element, $edit = FALSE) {
if ($edit !== FALSE) {
$on = $edit['on'];
$off = $edit['off'];
$edit = "0|$off\n1|$on";
return $edit;
}
function list_boolean_allowed_values_callback($element, $input, $form_state) {
$on = drupal_array_get_nested_value($form_state['input'], $element['#on_parents']);
$off = drupal_array_get_nested_value($form_state['input'], $element['#off_parents']);
return "0|$off\n1|$on";
}
/**

View File

@ -134,26 +134,46 @@ class ListFieldUITestCase extends FieldTestCase {
function testAllowedValues() {
$element_name = "field[settings][allowed_values]";
//Test 'List' field type.
// Test 'List' field type.
$admin_path = $this->createListFieldAndEdit('list');
//Check that non-integer keys are rejected.
// Check that non-integer keys are rejected.
$edit = array($element_name => "1.1|one\n");
$this->drupalPost($admin_path, $edit, t('Save settings'));
$this->assertText("keys must be integers", t('Form validation failed.'));
// Test 'List (number)' field type.
$admin_path = $this->createListFieldAndEdit('list_number');
//Check that non-numeric keys are rejected.
// Check that non-numeric keys are rejected.
$edit = array($element_name => "1|one\nB|two");
$this->drupalPost($admin_path, $edit, t('Save settings'));
$this->assertText("each key must be a valid integer or decimal", t('Form validation failed.'));
//Test 'List (text)' field type.
// Test 'List (text)' field type.
$admin_path = $this->createListFieldAndEdit('list_text');
//Check that over long keys are rejected.
// Check that overly long keys are rejected.
$edit = array($element_name => "1|one\n" . $this->randomName(256) . "|two");
$this->drupalPost($admin_path, $edit, t('Save settings'));
$this->assertText("each key must be a string at most 255 characters long", t('Form validation failed.'));
// Test 'Boolean' field type.
$admin_path = $this->createListFieldAndEdit('list_boolean');
// Check that the seperate 'On' and 'Off' form fields work.
$on = $this->randomName();
$off = $this->randomName();
$edit = array(
'on' => $on,
'off' => $off,
);
$this->drupalPost($admin_path, $edit, t('Save settings'));
$this->assertText("Saved test_list_boolean configuration.", t("The 'On' and 'Off' form fields work for boolean fields."));
// Test the allowed_values on the field settings form.
$this->drupalGet($admin_path);
$this->assertFieldByName('on', $on, t("The 'On' value is stored correctly."));
$this->assertFieldByName('off', $off, t("The 'Off' value is stored correctly."));
$field = field_info_field($this->field['field_name']);
$this->assertEqual($field['settings']['allowed_values'], "0|$off\n1|$on", t('The allowed value is correct'));
$this->assertFalse(isset($field['settings']['on']), t('The on value is not saved into settings'));
$this->assertFalse(isset($field['settings']['off']), t('The off value is not saved into settings'));
}
/**
@ -162,7 +182,7 @@ class ListFieldUITestCase extends FieldTestCase {
* @param string $type
* 'list', 'list_boolean', 'list_number', or 'list_text'
*/
private function createListFieldAndEdit($type) {
protected function createListFieldAndEdit($type) {
// Create a test field and instance.
$field_name = 'test_' . $type;
$field = array(
@ -170,6 +190,7 @@ class ListFieldUITestCase extends FieldTestCase {
'type' => $type,
);
field_create_field($field);
$this->field = $field;
$instance = array(
'field_name' => $field_name,
'entity_type' => 'node',
@ -182,4 +203,3 @@ class ListFieldUITestCase extends FieldTestCase {
}
}