74
modules/field/modules/options/options.api.php
Normal file
74
modules/field/modules/options/options.api.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Hooks provided by the Options module.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Returns the list of options to be displayed for a field.
|
||||
*
|
||||
* Field types willing to enable one or several of the widgets defined in
|
||||
* options.module (select, radios/checkboxes, on/off checkbox) need to
|
||||
* implement this hook to specify the list of options to display in the
|
||||
* widgets.
|
||||
*
|
||||
* @param $field
|
||||
* The field definition.
|
||||
* @param $instance
|
||||
* (optional) The instance definition. The hook might be called without an
|
||||
* $instance parameter in contexts where no specific instance can be targeted.
|
||||
* It is recommended to only use instance level properties to filter out
|
||||
* values from a list defined by field level properties.
|
||||
* @param $entity_type
|
||||
* The entity type the field is attached to.
|
||||
* @param $entity
|
||||
* The entity object the field is attached to, or NULL if no entity
|
||||
* exists (e.g. in field settings page).
|
||||
*
|
||||
* @return
|
||||
* The array of options for the field. Array keys are the values to be
|
||||
* stored, and should be of the data type (string, number...) expected by
|
||||
* the first 'column' for the field type. Array values are the labels to
|
||||
* display within the widgets. The labels should NOT be sanitized,
|
||||
* options.module takes care of sanitation according to the needs of each
|
||||
* widget. The HTML tags defined in _field_filter_xss_allowed_tags() are
|
||||
* allowed, other tags will be filtered.
|
||||
*/
|
||||
function hook_options_list($field, $instance, $entity_type, $entity) {
|
||||
// Sample structure.
|
||||
$options = array(
|
||||
0 => t('Zero'),
|
||||
1 => t('One'),
|
||||
2 => t('Two'),
|
||||
3 => t('Three'),
|
||||
);
|
||||
|
||||
// Sample structure with groups. Only one level of nesting is allowed. This
|
||||
// is only supported by the 'options_select' widget. Other widgets will
|
||||
// flatten the array.
|
||||
$options = array(
|
||||
t('First group') => array(
|
||||
0 => t('Zero'),
|
||||
),
|
||||
t('Second group') => array(
|
||||
1 => t('One'),
|
||||
2 => t('Two'),
|
||||
),
|
||||
3 => t('Three'),
|
||||
);
|
||||
|
||||
// In actual implementations, the array of options will most probably depend
|
||||
// on properties of the field. Example from taxonomy.module:
|
||||
$options = array();
|
||||
foreach ($field['settings']['allowed_values'] as $tree) {
|
||||
$terms = taxonomy_get_tree($tree['vid'], $tree['parent']);
|
||||
if ($terms) {
|
||||
foreach ($terms as $term) {
|
||||
$options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
13
modules/field/modules/options/options.info
Normal file
13
modules/field/modules/options/options.info
Normal file
@@ -0,0 +1,13 @@
|
||||
name = Options
|
||||
description = Defines selection, check box and radio button widgets for text and numeric fields.
|
||||
package = Core
|
||||
version = VERSION
|
||||
core = 7.x
|
||||
dependencies[] = field
|
||||
files[] = options.test
|
||||
|
||||
; Information added by drupal.org packaging script on 2012-08-01
|
||||
version = "7.15"
|
||||
project = "drupal"
|
||||
datestamp = "1343839327"
|
||||
|
417
modules/field/modules/options/options.module
Normal file
417
modules/field/modules/options/options.module
Normal file
@@ -0,0 +1,417 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Defines selection, check box and radio button widgets for text and numeric fields.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_help().
|
||||
*/
|
||||
function options_help($path, $arg) {
|
||||
switch ($path) {
|
||||
case 'admin/help#options':
|
||||
$output = '';
|
||||
$output .= '<h3>' . t('About') . '</h3>';
|
||||
$output .= '<p>' . t('The Options module defines checkbox, selection, and other input widgets for the Field module. See the <a href="@field-help">Field module help page</a> for more information about fields.', array('@field-help' => url('admin/help/field'))) . '</p>';
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_theme().
|
||||
*/
|
||||
function options_theme() {
|
||||
return array(
|
||||
'options_none' => array(
|
||||
'variables' => array('instance' => NULL, 'option' => NULL),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_widget_info().
|
||||
*
|
||||
* Field type modules willing to use those widgets should:
|
||||
* - Use hook_field_widget_info_alter() to append their field own types to the
|
||||
* list of types supported by the widgets,
|
||||
* - Implement hook_options_list() to provide the list of options.
|
||||
* See list.module.
|
||||
*/
|
||||
function options_field_widget_info() {
|
||||
return array(
|
||||
'options_select' => array(
|
||||
'label' => t('Select list'),
|
||||
'field types' => array(),
|
||||
'behaviors' => array(
|
||||
'multiple values' => FIELD_BEHAVIOR_CUSTOM,
|
||||
),
|
||||
),
|
||||
'options_buttons' => array(
|
||||
'label' => t('Check boxes/radio buttons'),
|
||||
'field types' => array(),
|
||||
'behaviors' => array(
|
||||
'multiple values' => FIELD_BEHAVIOR_CUSTOM,
|
||||
),
|
||||
),
|
||||
'options_onoff' => array(
|
||||
'label' => t('Single on/off checkbox'),
|
||||
'field types' => array(),
|
||||
'behaviors' => array(
|
||||
'multiple values' => FIELD_BEHAVIOR_CUSTOM,
|
||||
),
|
||||
'settings' => array('display_label' => 0),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_widget_form().
|
||||
*/
|
||||
function options_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
|
||||
// Abstract over the actual field columns, to allow different field types to
|
||||
// reuse those widgets.
|
||||
$value_key = key($field['columns']);
|
||||
|
||||
$type = str_replace('options_', '', $instance['widget']['type']);
|
||||
$multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
|
||||
$required = $element['#required'];
|
||||
$has_value = isset($items[0][$value_key]);
|
||||
$properties = _options_properties($type, $multiple, $required, $has_value);
|
||||
|
||||
$entity_type = $element['#entity_type'];
|
||||
$entity = $element['#entity'];
|
||||
|
||||
// Prepare the list of options.
|
||||
$options = _options_get_options($field, $instance, $properties, $entity_type, $entity);
|
||||
|
||||
// Put current field values in shape.
|
||||
$default_value = _options_storage_to_form($items, $options, $value_key, $properties);
|
||||
|
||||
switch ($type) {
|
||||
case 'select':
|
||||
$element += array(
|
||||
'#type' => 'select',
|
||||
'#default_value' => $default_value,
|
||||
// Do not display a 'multiple' select box if there is only one option.
|
||||
'#multiple' => $multiple && count($options) > 1,
|
||||
'#options' => $options,
|
||||
);
|
||||
break;
|
||||
|
||||
case 'buttons':
|
||||
// If required and there is one single option, preselect it.
|
||||
if ($required && count($options) == 1) {
|
||||
reset($options);
|
||||
$default_value = array(key($options));
|
||||
}
|
||||
|
||||
// If this is a single-value field, take the first default value, or
|
||||
// default to NULL so that the form element is properly recognized as
|
||||
// not having a default value.
|
||||
if (!$multiple) {
|
||||
$default_value = $default_value ? reset($default_value) : NULL;
|
||||
}
|
||||
|
||||
$element += array(
|
||||
'#type' => $multiple ? 'checkboxes' : 'radios',
|
||||
// Radio buttons need a scalar value.
|
||||
'#default_value' => $default_value,
|
||||
'#options' => $options,
|
||||
);
|
||||
break;
|
||||
|
||||
case 'onoff':
|
||||
$keys = array_keys($options);
|
||||
$off_value = array_shift($keys);
|
||||
$on_value = array_shift($keys);
|
||||
$element += array(
|
||||
'#type' => 'checkbox',
|
||||
'#default_value' => (isset($default_value[0]) && $default_value[0] == $on_value) ? 1 : 0,
|
||||
'#on_value' => $on_value,
|
||||
'#off_value' => $off_value,
|
||||
);
|
||||
// Override the title from the incoming $element.
|
||||
$element['#title'] = isset($options[$on_value]) ? $options[$on_value] : '';
|
||||
|
||||
if ($instance['widget']['settings']['display_label']) {
|
||||
$element['#title'] = $instance['label'];
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
$element += array(
|
||||
'#value_key' => $value_key,
|
||||
'#element_validate' => array('options_field_widget_validate'),
|
||||
'#properties' => $properties,
|
||||
);
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_widget_settings_form().
|
||||
*/
|
||||
function options_field_widget_settings_form($field, $instance) {
|
||||
$form = array();
|
||||
if ($instance['widget']['type'] == 'options_onoff') {
|
||||
$form['display_label'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Use field label instead of the "On value" as label'),
|
||||
'#default_value' => $instance['widget']['settings']['display_label'],
|
||||
'#weight' => -1,
|
||||
);
|
||||
}
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Form element validation handler for options element.
|
||||
*/
|
||||
function options_field_widget_validate($element, &$form_state) {
|
||||
if ($element['#required'] && $element['#value'] == '_none') {
|
||||
form_error($element, t('!name field is required.', array('!name' => $element['#title'])));
|
||||
}
|
||||
// Transpose selections from field => delta to delta => field, turning
|
||||
// multiple selected options into multiple parent elements.
|
||||
$items = _options_form_to_storage($element);
|
||||
form_set_value($element, $items, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Describes the preparation steps required by each widget.
|
||||
*/
|
||||
function _options_properties($type, $multiple, $required, $has_value) {
|
||||
$base = array(
|
||||
'filter_xss' => FALSE,
|
||||
'strip_tags' => FALSE,
|
||||
'empty_option' => FALSE,
|
||||
'optgroups' => FALSE,
|
||||
);
|
||||
|
||||
$properties = array();
|
||||
|
||||
switch ($type) {
|
||||
case 'select':
|
||||
$properties = array(
|
||||
// Select boxes do not support any HTML tag.
|
||||
'strip_tags' => TRUE,
|
||||
'optgroups' => TRUE,
|
||||
);
|
||||
if ($multiple) {
|
||||
// Multiple select: add a 'none' option for non-required fields.
|
||||
if (!$required) {
|
||||
$properties['empty_option'] = 'option_none';
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Single select: add a 'none' option for non-required fields,
|
||||
// and a 'select a value' option for required fields that do not come
|
||||
// with a value selected.
|
||||
if (!$required) {
|
||||
$properties['empty_option'] = 'option_none';
|
||||
}
|
||||
elseif (!$has_value) {
|
||||
$properties['empty_option'] = 'option_select';
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'buttons':
|
||||
$properties = array(
|
||||
'filter_xss' => TRUE,
|
||||
);
|
||||
// Add a 'none' option for non-required radio buttons.
|
||||
if (!$required && !$multiple) {
|
||||
$properties['empty_option'] = 'option_none';
|
||||
}
|
||||
break;
|
||||
|
||||
case 'onoff':
|
||||
$properties = array(
|
||||
'filter_xss' => TRUE,
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
||||
return $properties + $base;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects the options for a field.
|
||||
*/
|
||||
function _options_get_options($field, $instance, $properties, $entity_type, $entity) {
|
||||
// Get the list of options.
|
||||
$options = (array) module_invoke($field['module'], 'options_list', $field, $instance, $entity_type, $entity);
|
||||
|
||||
// Sanitize the options.
|
||||
_options_prepare_options($options, $properties);
|
||||
|
||||
if (!$properties['optgroups']) {
|
||||
$options = options_array_flatten($options);
|
||||
}
|
||||
|
||||
if ($properties['empty_option']) {
|
||||
$label = theme('options_none', array('instance' => $instance, 'option' => $properties['empty_option']));
|
||||
$options = array('_none' => $label) + $options;
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes the options.
|
||||
*
|
||||
* The function is recursive to support optgroups.
|
||||
*/
|
||||
function _options_prepare_options(&$options, $properties) {
|
||||
foreach ($options as $value => $label) {
|
||||
// Recurse for optgroups.
|
||||
if (is_array($label)) {
|
||||
_options_prepare_options($options[$value], $properties);
|
||||
}
|
||||
else {
|
||||
if ($properties['strip_tags']) {
|
||||
$options[$value] = strip_tags($label);
|
||||
}
|
||||
if ($properties['filter_xss']) {
|
||||
$options[$value] = field_filter_xss($label);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms stored field values into the format the widgets need.
|
||||
*/
|
||||
function _options_storage_to_form($items, $options, $column, $properties) {
|
||||
$items_transposed = options_array_transpose($items);
|
||||
$values = (isset($items_transposed[$column]) && is_array($items_transposed[$column])) ? $items_transposed[$column] : array();
|
||||
|
||||
// Discard values that are not in the current list of options. Flatten the
|
||||
// array if needed.
|
||||
if ($properties['optgroups']) {
|
||||
$options = options_array_flatten($options);
|
||||
}
|
||||
$values = array_values(array_intersect($values, array_keys($options)));
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms submitted form values into field storage format.
|
||||
*/
|
||||
function _options_form_to_storage($element) {
|
||||
$values = array_values((array) $element['#value']);
|
||||
$properties = $element['#properties'];
|
||||
|
||||
// On/off checkbox: transform '0 / 1' into the 'on / off' values.
|
||||
if ($element['#type'] == 'checkbox') {
|
||||
$values = array($values[0] ? $element['#on_value'] : $element['#off_value']);
|
||||
}
|
||||
|
||||
// Filter out the 'none' option. Use a strict comparison, because
|
||||
// 0 == 'any string'.
|
||||
if ($properties['empty_option']) {
|
||||
$index = array_search('_none', $values, TRUE);
|
||||
if ($index !== FALSE) {
|
||||
unset($values[$index]);
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure we populate at least an empty value.
|
||||
if (empty($values)) {
|
||||
$values = array(NULL);
|
||||
}
|
||||
|
||||
$result = options_array_transpose(array($element['#value_key'] => $values));
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Manipulates a 2D array to reverse rows and columns.
|
||||
*
|
||||
* The default data storage for fields is delta first, column names second.
|
||||
* This is sometimes inconvenient for field modules, so this function can be
|
||||
* used to present the data in an alternate format.
|
||||
*
|
||||
* @param $array
|
||||
* The array to be transposed. It must be at least two-dimensional, and
|
||||
* the subarrays must all have the same keys or behavior is undefined.
|
||||
* @return
|
||||
* The transposed array.
|
||||
*/
|
||||
function options_array_transpose($array) {
|
||||
$result = array();
|
||||
if (is_array($array)) {
|
||||
foreach ($array as $key1 => $value1) {
|
||||
if (is_array($value1)) {
|
||||
foreach ($value1 as $key2 => $value2) {
|
||||
if (!isset($result[$key2])) {
|
||||
$result[$key2] = array();
|
||||
}
|
||||
$result[$key2][$key1] = $value2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flattens an array of allowed values.
|
||||
*
|
||||
* @param $array
|
||||
* A single or multidimensional array.
|
||||
* @return
|
||||
* A flattened array.
|
||||
*/
|
||||
function options_array_flatten($array) {
|
||||
$result = array();
|
||||
if (is_array($array)) {
|
||||
foreach ($array as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
$result += options_array_flatten($value);
|
||||
}
|
||||
else {
|
||||
$result[$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_widget_error().
|
||||
*/
|
||||
function options_field_widget_error($element, $error, $form, &$form_state) {
|
||||
form_error($element, $error['message']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns HTML for the label for the empty value for options that are not required.
|
||||
*
|
||||
* The default theme will display N/A for a radio list and '- None -' for a select.
|
||||
*
|
||||
* @param $variables
|
||||
* An associative array containing:
|
||||
* - instance: An array representing the widget requesting the options.
|
||||
*
|
||||
* @ingroup themeable
|
||||
*/
|
||||
function theme_options_none($variables) {
|
||||
$instance = $variables['instance'];
|
||||
$option = $variables['option'];
|
||||
|
||||
$output = '';
|
||||
switch ($instance['widget']['type']) {
|
||||
case 'options_buttons':
|
||||
$output = t('N/A');
|
||||
break;
|
||||
|
||||
case 'options_select':
|
||||
$output = ($option == 'option_none' ? t('- None -') : t('- Select a value -'));
|
||||
break;
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
553
modules/field/modules/options/options.test
Normal file
553
modules/field/modules/options/options.test
Normal file
@@ -0,0 +1,553 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Tests for options.module.
|
||||
*/
|
||||
|
||||
class OptionsWidgetsTestCase extends FieldTestCase {
|
||||
public static function getInfo() {
|
||||
return array(
|
||||
'name' => 'Options widgets',
|
||||
'description' => "Test the Options widgets.",
|
||||
'group' => 'Field types'
|
||||
);
|
||||
}
|
||||
|
||||
function setUp() {
|
||||
parent::setUp('field_test', 'list_test');
|
||||
|
||||
// Field with cardinality 1.
|
||||
$this->card_1 = array(
|
||||
'field_name' => 'card_1',
|
||||
'type' => 'list_integer',
|
||||
'cardinality' => 1,
|
||||
'settings' => array(
|
||||
// Make sure that 0 works as an option.
|
||||
'allowed_values' => array(0 => 'Zero', 1 => 'One', 2 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>'),
|
||||
),
|
||||
);
|
||||
$this->card_1 = field_create_field($this->card_1);
|
||||
|
||||
// Field with cardinality 2.
|
||||
$this->card_2 = array(
|
||||
'field_name' => 'card_2',
|
||||
'type' => 'list_integer',
|
||||
'cardinality' => 2,
|
||||
'settings' => array(
|
||||
// Make sure that 0 works as an option.
|
||||
'allowed_values' => array(0 => 'Zero', 1 => 'One', 2 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>'),
|
||||
),
|
||||
);
|
||||
$this->card_2 = field_create_field($this->card_2);
|
||||
|
||||
// Boolean field.
|
||||
$this->bool = array(
|
||||
'field_name' => 'bool',
|
||||
'type' => 'list_boolean',
|
||||
'cardinality' => 1,
|
||||
'settings' => array(
|
||||
// Make sure that 0 works as a 'on' value'.
|
||||
'allowed_values' => array(1 => 'Zero', 0 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>'),
|
||||
),
|
||||
);
|
||||
$this->bool = field_create_field($this->bool);
|
||||
|
||||
// Create a web user.
|
||||
$this->web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content'));
|
||||
$this->drupalLogin($this->web_user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the 'options_buttons' widget (single select).
|
||||
*/
|
||||
function testRadioButtons() {
|
||||
// Create an instance of the 'single value' field.
|
||||
$instance = array(
|
||||
'field_name' => $this->card_1['field_name'],
|
||||
'entity_type' => 'test_entity',
|
||||
'bundle' => 'test_bundle',
|
||||
'widget' => array(
|
||||
'type' => 'options_buttons',
|
||||
),
|
||||
);
|
||||
$instance = field_create_instance($instance);
|
||||
$langcode = LANGUAGE_NONE;
|
||||
|
||||
// Create an entity.
|
||||
$entity_init = field_test_create_stub_entity();
|
||||
$entity = clone $entity_init;
|
||||
$entity->is_new = TRUE;
|
||||
field_test_entity_save($entity);
|
||||
|
||||
// With no field data, no buttons are checked.
|
||||
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
||||
$this->assertNoFieldChecked("edit-card-1-$langcode-0");
|
||||
$this->assertNoFieldChecked("edit-card-1-$langcode-1");
|
||||
$this->assertNoFieldChecked("edit-card-1-$langcode-2");
|
||||
$this->assertRaw('Some dangerous & unescaped <strong>markup</strong>', t('Option text was properly filtered.'));
|
||||
|
||||
// Select first option.
|
||||
$edit = array("card_1[$langcode]" => 0);
|
||||
$this->drupalPost(NULL, $edit, t('Save'));
|
||||
$this->assertFieldValues($entity_init, 'card_1', $langcode, array(0));
|
||||
|
||||
// Check that the selected button is checked.
|
||||
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
||||
$this->assertFieldChecked("edit-card-1-$langcode-0");
|
||||
$this->assertNoFieldChecked("edit-card-1-$langcode-1");
|
||||
$this->assertNoFieldChecked("edit-card-1-$langcode-2");
|
||||
|
||||
// Unselect option.
|
||||
$edit = array("card_1[$langcode]" => '_none');
|
||||
$this->drupalPost(NULL, $edit, t('Save'));
|
||||
$this->assertFieldValues($entity_init, 'card_1', $langcode, array());
|
||||
|
||||
// Check that required radios with one option is auto-selected.
|
||||
$this->card_1['settings']['allowed_values'] = array(99 => 'Only allowed value');
|
||||
field_update_field($this->card_1);
|
||||
$instance['required'] = TRUE;
|
||||
field_update_instance($instance);
|
||||
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
||||
$this->assertFieldChecked("edit-card-1-$langcode-99");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the 'options_buttons' widget (multiple select).
|
||||
*/
|
||||
function testCheckBoxes() {
|
||||
// Create an instance of the 'multiple values' field.
|
||||
$instance = array(
|
||||
'field_name' => $this->card_2['field_name'],
|
||||
'entity_type' => 'test_entity',
|
||||
'bundle' => 'test_bundle',
|
||||
'widget' => array(
|
||||
'type' => 'options_buttons',
|
||||
),
|
||||
);
|
||||
$instance = field_create_instance($instance);
|
||||
$langcode = LANGUAGE_NONE;
|
||||
|
||||
// Create an entity.
|
||||
$entity_init = field_test_create_stub_entity();
|
||||
$entity = clone $entity_init;
|
||||
$entity->is_new = TRUE;
|
||||
field_test_entity_save($entity);
|
||||
|
||||
// Display form: with no field data, nothing is checked.
|
||||
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
||||
$this->assertNoFieldChecked("edit-card-2-$langcode-0");
|
||||
$this->assertNoFieldChecked("edit-card-2-$langcode-1");
|
||||
$this->assertNoFieldChecked("edit-card-2-$langcode-2");
|
||||
$this->assertRaw('Some dangerous & unescaped <strong>markup</strong>', t('Option text was properly filtered.'));
|
||||
|
||||
// Submit form: select first and third options.
|
||||
$edit = array(
|
||||
"card_2[$langcode][0]" => TRUE,
|
||||
"card_2[$langcode][1]" => FALSE,
|
||||
"card_2[$langcode][2]" => TRUE,
|
||||
);
|
||||
$this->drupalPost(NULL, $edit, t('Save'));
|
||||
$this->assertFieldValues($entity_init, 'card_2', $langcode, array(0, 2));
|
||||
|
||||
// Display form: check that the right options are selected.
|
||||
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
||||
$this->assertFieldChecked("edit-card-2-$langcode-0");
|
||||
$this->assertNoFieldChecked("edit-card-2-$langcode-1");
|
||||
$this->assertFieldChecked("edit-card-2-$langcode-2");
|
||||
|
||||
// Submit form: select only first option.
|
||||
$edit = array(
|
||||
"card_2[$langcode][0]" => TRUE,
|
||||
"card_2[$langcode][1]" => FALSE,
|
||||
"card_2[$langcode][2]" => FALSE,
|
||||
);
|
||||
$this->drupalPost(NULL, $edit, t('Save'));
|
||||
$this->assertFieldValues($entity_init, 'card_2', $langcode, array(0));
|
||||
|
||||
// Display form: check that the right options are selected.
|
||||
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
||||
$this->assertFieldChecked("edit-card-2-$langcode-0");
|
||||
$this->assertNoFieldChecked("edit-card-2-$langcode-1");
|
||||
$this->assertNoFieldChecked("edit-card-2-$langcode-2");
|
||||
|
||||
// Submit form: select the three options while the field accepts only 2.
|
||||
$edit = array(
|
||||
"card_2[$langcode][0]" => TRUE,
|
||||
"card_2[$langcode][1]" => TRUE,
|
||||
"card_2[$langcode][2]" => TRUE,
|
||||
);
|
||||
$this->drupalPost(NULL, $edit, t('Save'));
|
||||
$this->assertText('this field cannot hold more than 2 values', t('Validation error was displayed.'));
|
||||
|
||||
// Submit form: uncheck all options.
|
||||
$edit = array(
|
||||
"card_2[$langcode][0]" => FALSE,
|
||||
"card_2[$langcode][1]" => FALSE,
|
||||
"card_2[$langcode][2]" => FALSE,
|
||||
);
|
||||
$this->drupalPost(NULL, $edit, t('Save'));
|
||||
// Check that the value was saved.
|
||||
$this->assertFieldValues($entity_init, 'card_2', $langcode, array());
|
||||
|
||||
// Required checkbox with one option is auto-selected.
|
||||
$this->card_2['settings']['allowed_values'] = array(99 => 'Only allowed value');
|
||||
field_update_field($this->card_2);
|
||||
$instance['required'] = TRUE;
|
||||
field_update_instance($instance);
|
||||
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
||||
$this->assertFieldChecked("edit-card-2-$langcode-99");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the 'options_select' widget (single select).
|
||||
*/
|
||||
function testSelectListSingle() {
|
||||
// Create an instance of the 'single value' field.
|
||||
$instance = array(
|
||||
'field_name' => $this->card_1['field_name'],
|
||||
'entity_type' => 'test_entity',
|
||||
'bundle' => 'test_bundle',
|
||||
'required' => TRUE,
|
||||
'widget' => array(
|
||||
'type' => 'options_select',
|
||||
),
|
||||
);
|
||||
$instance = field_create_instance($instance);
|
||||
$langcode = LANGUAGE_NONE;
|
||||
|
||||
// Create an entity.
|
||||
$entity_init = field_test_create_stub_entity();
|
||||
$entity = clone $entity_init;
|
||||
$entity->is_new = TRUE;
|
||||
field_test_entity_save($entity);
|
||||
|
||||
// Display form.
|
||||
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
||||
// A required field without any value has a "none" option.
|
||||
$this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', array(':id' => 'edit-card-1-' . $langcode, ':label' => t('- Select a value -'))), t('A required select list has a "Select a value" choice.'));
|
||||
|
||||
// With no field data, nothing is selected.
|
||||
$this->assertNoOptionSelected("edit-card-1-$langcode", '_none');
|
||||
$this->assertNoOptionSelected("edit-card-1-$langcode", 0);
|
||||
$this->assertNoOptionSelected("edit-card-1-$langcode", 1);
|
||||
$this->assertNoOptionSelected("edit-card-1-$langcode", 2);
|
||||
$this->assertRaw('Some dangerous & unescaped markup', t('Option text was properly filtered.'));
|
||||
|
||||
// Submit form: select invalid 'none' option.
|
||||
$edit = array("card_1[$langcode]" => '_none');
|
||||
$this->drupalPost(NULL, $edit, t('Save'));
|
||||
$this->assertRaw(t('!title field is required.', array('!title' => $instance['field_name'])), t('Cannot save a required field when selecting "none" from the select list.'));
|
||||
|
||||
// Submit form: select first option.
|
||||
$edit = array("card_1[$langcode]" => 0);
|
||||
$this->drupalPost(NULL, $edit, t('Save'));
|
||||
$this->assertFieldValues($entity_init, 'card_1', $langcode, array(0));
|
||||
|
||||
// Display form: check that the right options are selected.
|
||||
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
||||
// A required field with a value has no 'none' option.
|
||||
$this->assertFalse($this->xpath('//select[@id=:id]//option[@value="_none"]', array(':id' => 'edit-card-1-' . $langcode)), t('A required select list with an actual value has no "none" choice.'));
|
||||
$this->assertOptionSelected("edit-card-1-$langcode", 0);
|
||||
$this->assertNoOptionSelected("edit-card-1-$langcode", 1);
|
||||
$this->assertNoOptionSelected("edit-card-1-$langcode", 2);
|
||||
|
||||
// Make the field non required.
|
||||
$instance['required'] = FALSE;
|
||||
field_update_instance($instance);
|
||||
|
||||
// Display form.
|
||||
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
||||
// A non-required field has a 'none' option.
|
||||
$this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', array(':id' => 'edit-card-1-' . $langcode, ':label' => t('- None -'))), t('A non-required select list has a "None" choice.'));
|
||||
// Submit form: Unselect the option.
|
||||
$edit = array("card_1[$langcode]" => '_none');
|
||||
$this->drupalPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
|
||||
$this->assertFieldValues($entity_init, 'card_1', $langcode, array());
|
||||
|
||||
// Test optgroups.
|
||||
|
||||
$this->card_1['settings']['allowed_values'] = array();
|
||||
$this->card_1['settings']['allowed_values_function'] = 'list_test_allowed_values_callback';
|
||||
field_update_field($this->card_1);
|
||||
|
||||
// Display form: with no field data, nothing is selected
|
||||
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
||||
$this->assertNoOptionSelected("edit-card-1-$langcode", 0);
|
||||
$this->assertNoOptionSelected("edit-card-1-$langcode", 1);
|
||||
$this->assertNoOptionSelected("edit-card-1-$langcode", 2);
|
||||
$this->assertRaw('Some dangerous & unescaped markup', t('Option text was properly filtered.'));
|
||||
$this->assertRaw('Group 1', t('Option groups are displayed.'));
|
||||
|
||||
// Submit form: select first option.
|
||||
$edit = array("card_1[$langcode]" => 0);
|
||||
$this->drupalPost(NULL, $edit, t('Save'));
|
||||
$this->assertFieldValues($entity_init, 'card_1', $langcode, array(0));
|
||||
|
||||
// Display form: check that the right options are selected.
|
||||
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
||||
$this->assertOptionSelected("edit-card-1-$langcode", 0);
|
||||
$this->assertNoOptionSelected("edit-card-1-$langcode", 1);
|
||||
$this->assertNoOptionSelected("edit-card-1-$langcode", 2);
|
||||
|
||||
// Submit form: Unselect the option.
|
||||
$edit = array("card_1[$langcode]" => '_none');
|
||||
$this->drupalPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
|
||||
$this->assertFieldValues($entity_init, 'card_1', $langcode, array());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the 'options_select' widget (multiple select).
|
||||
*/
|
||||
function testSelectListMultiple() {
|
||||
// Create an instance of the 'multiple values' field.
|
||||
$instance = array(
|
||||
'field_name' => $this->card_2['field_name'],
|
||||
'entity_type' => 'test_entity',
|
||||
'bundle' => 'test_bundle',
|
||||
'widget' => array(
|
||||
'type' => 'options_select',
|
||||
),
|
||||
);
|
||||
$instance = field_create_instance($instance);
|
||||
$langcode = LANGUAGE_NONE;
|
||||
|
||||
// Create an entity.
|
||||
$entity_init = field_test_create_stub_entity();
|
||||
$entity = clone $entity_init;
|
||||
$entity->is_new = TRUE;
|
||||
field_test_entity_save($entity);
|
||||
|
||||
// Display form: with no field data, nothing is selected.
|
||||
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
||||
$this->assertNoOptionSelected("edit-card-2-$langcode", 0);
|
||||
$this->assertNoOptionSelected("edit-card-2-$langcode", 1);
|
||||
$this->assertNoOptionSelected("edit-card-2-$langcode", 2);
|
||||
$this->assertRaw('Some dangerous & unescaped markup', t('Option text was properly filtered.'));
|
||||
|
||||
// Submit form: select first and third options.
|
||||
$edit = array("card_2[$langcode][]" => array(0 => 0, 2 => 2));
|
||||
$this->drupalPost(NULL, $edit, t('Save'));
|
||||
$this->assertFieldValues($entity_init, 'card_2', $langcode, array(0, 2));
|
||||
|
||||
// Display form: check that the right options are selected.
|
||||
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
||||
$this->assertOptionSelected("edit-card-2-$langcode", 0);
|
||||
$this->assertNoOptionSelected("edit-card-2-$langcode", 1);
|
||||
$this->assertOptionSelected("edit-card-2-$langcode", 2);
|
||||
|
||||
// Submit form: select only first option.
|
||||
$edit = array("card_2[$langcode][]" => array(0 => 0));
|
||||
$this->drupalPost(NULL, $edit, t('Save'));
|
||||
$this->assertFieldValues($entity_init, 'card_2', $langcode, array(0));
|
||||
|
||||
// Display form: check that the right options are selected.
|
||||
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
||||
$this->assertOptionSelected("edit-card-2-$langcode", 0);
|
||||
$this->assertNoOptionSelected("edit-card-2-$langcode", 1);
|
||||
$this->assertNoOptionSelected("edit-card-2-$langcode", 2);
|
||||
|
||||
// Submit form: select the three options while the field accepts only 2.
|
||||
$edit = array("card_2[$langcode][]" => array(0 => 0, 1 => 1, 2 => 2));
|
||||
$this->drupalPost(NULL, $edit, t('Save'));
|
||||
$this->assertText('this field cannot hold more than 2 values', t('Validation error was displayed.'));
|
||||
|
||||
// Submit form: uncheck all options.
|
||||
$edit = array("card_2[$langcode][]" => array());
|
||||
$this->drupalPost(NULL, $edit, t('Save'));
|
||||
$this->assertFieldValues($entity_init, 'card_2', $langcode, array());
|
||||
|
||||
// Test the 'None' option.
|
||||
|
||||
// Check that the 'none' option has no efect if actual options are selected
|
||||
// as well.
|
||||
$edit = array("card_2[$langcode][]" => array('_none' => '_none', 0 => 0));
|
||||
$this->drupalPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
|
||||
$this->assertFieldValues($entity_init, 'card_2', $langcode, array(0));
|
||||
|
||||
// Check that selecting the 'none' option empties the field.
|
||||
$edit = array("card_2[$langcode][]" => array('_none' => '_none'));
|
||||
$this->drupalPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
|
||||
$this->assertFieldValues($entity_init, 'card_2', $langcode, array());
|
||||
|
||||
// A required select list does not have an empty key.
|
||||
$instance['required'] = TRUE;
|
||||
field_update_instance($instance);
|
||||
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
||||
$this->assertFalse($this->xpath('//select[@id=:id]//option[@value=""]', array(':id' => 'edit-card-2-' . $langcode)), t('A required select list does not have an empty key.'));
|
||||
|
||||
// We do not have to test that a required select list with one option is
|
||||
// auto-selected because the browser does it for us.
|
||||
|
||||
// Test optgroups.
|
||||
|
||||
// Use a callback function defining optgroups.
|
||||
$this->card_2['settings']['allowed_values'] = array();
|
||||
$this->card_2['settings']['allowed_values_function'] = 'list_test_allowed_values_callback';
|
||||
field_update_field($this->card_2);
|
||||
$instance['required'] = FALSE;
|
||||
field_update_instance($instance);
|
||||
|
||||
// Display form: with no field data, nothing is selected.
|
||||
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
||||
$this->assertNoOptionSelected("edit-card-2-$langcode", 0);
|
||||
$this->assertNoOptionSelected("edit-card-2-$langcode", 1);
|
||||
$this->assertNoOptionSelected("edit-card-2-$langcode", 2);
|
||||
$this->assertRaw('Some dangerous & unescaped markup', t('Option text was properly filtered.'));
|
||||
$this->assertRaw('Group 1', t('Option groups are displayed.'));
|
||||
|
||||
// Submit form: select first option.
|
||||
$edit = array("card_2[$langcode][]" => array(0 => 0));
|
||||
$this->drupalPost(NULL, $edit, t('Save'));
|
||||
$this->assertFieldValues($entity_init, 'card_2', $langcode, array(0));
|
||||
|
||||
// Display form: check that the right options are selected.
|
||||
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
||||
$this->assertOptionSelected("edit-card-2-$langcode", 0);
|
||||
$this->assertNoOptionSelected("edit-card-2-$langcode", 1);
|
||||
$this->assertNoOptionSelected("edit-card-2-$langcode", 2);
|
||||
|
||||
// Submit form: Unselect the option.
|
||||
$edit = array("card_2[$langcode][]" => array('_none' => '_none'));
|
||||
$this->drupalPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
|
||||
$this->assertFieldValues($entity_init, 'card_2', $langcode, array());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the 'options_onoff' widget.
|
||||
*/
|
||||
function testOnOffCheckbox() {
|
||||
// Create an instance of the 'boolean' field.
|
||||
$instance = array(
|
||||
'field_name' => $this->bool['field_name'],
|
||||
'entity_type' => 'test_entity',
|
||||
'bundle' => 'test_bundle',
|
||||
'widget' => array(
|
||||
'type' => 'options_onoff',
|
||||
),
|
||||
);
|
||||
$instance = field_create_instance($instance);
|
||||
$langcode = LANGUAGE_NONE;
|
||||
|
||||
// Create an entity.
|
||||
$entity_init = field_test_create_stub_entity();
|
||||
$entity = clone $entity_init;
|
||||
$entity->is_new = TRUE;
|
||||
field_test_entity_save($entity);
|
||||
|
||||
// Display form: with no field data, option is unchecked.
|
||||
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
||||
$this->assertNoFieldChecked("edit-bool-$langcode");
|
||||
$this->assertRaw('Some dangerous & unescaped <strong>markup</strong>', t('Option text was properly filtered.'));
|
||||
|
||||
// Submit form: check the option.
|
||||
$edit = array("bool[$langcode]" => TRUE);
|
||||
$this->drupalPost(NULL, $edit, t('Save'));
|
||||
$this->assertFieldValues($entity_init, 'bool', $langcode, array(0));
|
||||
|
||||
// Display form: check that the right options are selected.
|
||||
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
||||
$this->assertFieldChecked("edit-bool-$langcode");
|
||||
|
||||
// Submit form: uncheck the option.
|
||||
$edit = array("bool[$langcode]" => FALSE);
|
||||
$this->drupalPost(NULL, $edit, t('Save'));
|
||||
$this->assertFieldValues($entity_init, 'bool', $langcode, array(1));
|
||||
|
||||
// Display form: with 'off' value, option is unchecked.
|
||||
$this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
|
||||
$this->assertNoFieldChecked("edit-bool-$langcode");
|
||||
|
||||
// Create admin user.
|
||||
$admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer taxonomy'));
|
||||
$this->drupalLogin($admin_user);
|
||||
|
||||
// Create a test field instance.
|
||||
$fieldUpdate = $this->bool;
|
||||
$fieldUpdate['settings']['allowed_values'] = array(0 => 0, 1 => 'MyOnValue');
|
||||
field_update_field($fieldUpdate);
|
||||
$instance = array(
|
||||
'field_name' => $this->bool['field_name'],
|
||||
'entity_type' => 'node',
|
||||
'bundle' => 'page',
|
||||
'widget' => array(
|
||||
'type' => 'options_onoff',
|
||||
'module' => 'options',
|
||||
),
|
||||
);
|
||||
field_create_instance($instance);
|
||||
|
||||
// Go to the edit page and check if the default settings works as expected
|
||||
$fieldEditUrl = 'admin/structure/types/manage/page/fields/bool';
|
||||
$this->drupalGet($fieldEditUrl);
|
||||
|
||||
$this->assertText(
|
||||
'Use field label instead of the "On value" as label ',
|
||||
t('Display setting checkbox available.')
|
||||
);
|
||||
|
||||
$this->assertFieldByXPath(
|
||||
'*//label[@for="edit-' . $this->bool['field_name'] . '-und" and text()="MyOnValue "]',
|
||||
TRUE,
|
||||
t('Default case shows "On value"')
|
||||
);
|
||||
|
||||
// Enable setting
|
||||
$edit = array('instance[widget][settings][display_label]' => 1);
|
||||
// Save the new Settings
|
||||
$this->drupalPost($fieldEditUrl, $edit, t('Save settings'));
|
||||
|
||||
// Go again to the edit page and check if the setting
|
||||
// is stored and has the expected effect
|
||||
$this->drupalGet($fieldEditUrl);
|
||||
$this->assertText(
|
||||
'Use field label instead of the "On value" as label ',
|
||||
t('Display setting checkbox is available')
|
||||
);
|
||||
$this->assertFieldChecked(
|
||||
'edit-instance-widget-settings-display-label',
|
||||
t('Display settings checkbox checked')
|
||||
);
|
||||
$this->assertFieldByXPath(
|
||||
'*//label[@for="edit-' . $this->bool['field_name'] . '-und" and text()="' . $this->bool['field_name'] . ' "]',
|
||||
TRUE,
|
||||
t('Display label changes label of the checkbox')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test an options select on a list field with a dynamic allowed values function.
|
||||
*/
|
||||
class OptionsSelectDynamicValuesTestCase extends ListDynamicValuesTestCase {
|
||||
public static function getInfo() {
|
||||
return array(
|
||||
'name' => 'Options select dynamic values',
|
||||
'description' => 'Test an options select on a list field with a dynamic allowed values function.',
|
||||
'group' => 'Field types',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the 'options_select' widget (single select).
|
||||
*/
|
||||
function testSelectListDynamic() {
|
||||
// Create an entity.
|
||||
$this->entity->is_new = TRUE;
|
||||
field_test_entity_save($this->entity);
|
||||
// Create a web user.
|
||||
$web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content'));
|
||||
$this->drupalLogin($web_user);
|
||||
|
||||
// Display form.
|
||||
$this->drupalGet('test-entity/manage/' . $this->entity->ftid . '/edit');
|
||||
$options = $this->xpath('//select[@id="edit-test-list-und"]/option');
|
||||
$this->assertEqual(count($options), count($this->test) + 1);
|
||||
foreach ($options as $option) {
|
||||
$value = (string) $option['value'];
|
||||
if ($value != '_none') {
|
||||
$this->assertTrue(array_search($value, $this->test));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user