->fields($record)
->execute();
}
+
+/**
+ * @addtogroup updates-7.x-to-8.x
+ * @{
+ */
+
+/**
+ * Reassign all list.module fields to be controlled by options.module.
+ */
+function field_update_8001() {
+ db_update('field_config')
+ ->fields(array(
+ 'module' => 'options',
+ ))
+ ->condition('module', 'list')
+ ->execute();
+}
+
+/**
+ * @} End of "addtogroup updates-7.x-to-8.x".
+ * The next series of updates should start at 9000.
+ */
}
function setUp() {
- parent::setUp(array('node', 'field_test', 'list'));
+ parent::setUp(array('node', 'field_test', 'options'));
$web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content'));
$this->drupalLogin($web_user);
+++ /dev/null
-name = List
-description = Defines list field types. Use with Options to create selection lists.
-package = Core
-version = VERSION
-core = 8.x
-dependencies[] = field
-dependencies[] = options
-files[] = tests/list.test
-files[] = tests/list_dynamic.test
+++ /dev/null
-<?php
-
-/**
- * @file
- * Defines list field types that can be used with the Options module.
- */
-
-use Drupal\field\FieldUpdateForbiddenException;
-use Drupal\entity\EntityFieldQuery;
-
-/**
- * Implements hook_help().
- */
-function list_help($path, $arg) {
- switch ($path) {
- case 'admin/help#list':
- $output = '';
- $output .= '<h3>' . t('About') . '</h3>';
- $output .= '<p>' . t('The List module defines various fields for storing a list of items, for use with the Field module. Usually these items are entered through a select list, checkboxes, or radio buttons. 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_field_info().
- */
-function list_field_info() {
- return array(
- 'list_integer' => array(
- 'label' => t('List (integer)'),
- 'description' => t("This field stores integer values from a list of allowed 'value => label' pairs, i.e. 'Lifetime in days': 1 => 1 day, 7 => 1 week, 31 => 1 month."),
- 'settings' => array('allowed_values' => array(), 'allowed_values_function' => ''),
- 'default_widget' => 'options_select',
- 'default_formatter' => 'list_default',
- ),
- 'list_float' => array(
- 'label' => t('List (float)'),
- 'description' => t("This field stores float values from a list of allowed 'value => label' pairs, i.e. 'Fraction': 0 => 0, .25 => 1/4, .75 => 3/4, 1 => 1."),
- 'settings' => array('allowed_values' => array(), 'allowed_values_function' => ''),
- 'default_widget' => 'options_select',
- 'default_formatter' => 'list_default',
- ),
- 'list_text' => array(
- 'label' => t('List (text)'),
- 'description' => t("This field stores text values from a list of allowed 'value => label' pairs, i.e. 'US States': IL => Illinois, IA => Iowa, IN => Indiana."),
- 'settings' => array('allowed_values' => array(), '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' => array(), 'allowed_values_function' => ''),
- 'default_widget' => 'options_buttons',
- 'default_formatter' => 'list_default',
- ),
- );
-}
-
-/**
- * Implements hook_field_settings_form().
- */
-function list_field_settings_form($field, $instance, $has_data) {
- $settings = $field['settings'];
-
- switch ($field['type']) {
- case 'list_integer':
- case 'list_float':
- case 'list_text':
- $form['allowed_values'] = array(
- '#type' => 'textarea',
- '#title' => t('Allowed values list'),
- '#default_value' => list_allowed_values_string($settings['allowed_values']),
- '#rows' => 10,
- '#element_validate' => array('list_allowed_values_setting_validate'),
- '#field_has_data' => $has_data,
- '#field' => $field,
- '#field_type' => $field['type'],
- '#access' => empty($settings['allowed_values_function']),
- );
-
- $description = '<p>' . t('The possible values this field can contain. Enter one value per line, in the format key|label.');
- if ($field['type'] == 'list_integer' || $field['type'] == 'list_float') {
- $description .= '<br/>' . t('The key is the stored value, and must be numeric. The label will be used in displayed values and edit forms.');
- $description .= '<br/>' . t('The label is optional: if a line contains a single number, it will be used as key and label.');
- $description .= '<br/>' . t('Lists of labels are also accepted (one label per line), only if the field does not hold any values yet. Numeric keys will be automatically generated from the positions in the list.');
- }
- else {
- $description .= '<br/>' . t('The key is the stored value. The label will be used in displayed values and edit forms.');
- $description .= '<br/>' . t('The label is optional: if a line contains a single string, it will be used as key and label.');
- }
- $description .= '</p>';
- $form['allowed_values']['#description'] = $description;
-
- break;
-
- case 'list_boolean':
- $values = $settings['allowed_values'];
- $off_value = array_shift($values);
- $on_value = array_shift($values);
-
- $form['allowed_values'] = array(
- '#type' => 'value',
- '#description' => '',
- '#value_callback' => 'list_boolean_allowed_values_callback',
- '#access' => empty($settings['allowed_values_function']),
- );
- $form['allowed_values']['on'] = array(
- '#type' => 'textfield',
- '#title' => t('On value'),
- '#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',
- '#title' => t('Off value'),
- '#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'];
-
- break;
- }
-
- // Alter the description for allowed values depending on the widget type.
- if ($instance['widget']['type'] == 'options_onoff') {
- $form['allowed_values']['#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>';
- }
- elseif ($instance['widget']['type'] == 'options_buttons') {
- $form['allowed_values']['#description'] .= '<p>' . t("The 'checkboxes/radio buttons' widget will display checkboxes if the <em>Number of values</em> option is greater than 1 for this field, otherwise radios will be displayed.") . '</p>';
- }
- $form['allowed_values']['#description'] .= '<p>' . t('Allowed HTML tags in labels: @tags', array('@tags' => _field_filter_xss_display_allowed_tags())) . '</p>';
-
- $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;
-}
-
-/**
- * Element validate callback; check that the entered values are valid.
- */
-function list_allowed_values_setting_validate($element, &$form_state) {
- $field = $element['#field'];
- $has_data = $element['#field_has_data'];
- $field_type = $field['type'];
- $generate_keys = ($field_type == 'list_integer' || $field_type == 'list_float') && !$has_data;
-
- $values = list_extract_allowed_values($element['#value'], $field['type'], $generate_keys);
-
- if (!is_array($values)) {
- form_error($element, t('Allowed values list: invalid input.'));
- }
- else {
- // Check that keys are valid for the field type.
- foreach ($values as $key => $value) {
- if ($field_type == 'list_integer' && !preg_match('/^-?\d+$/', $key)) {
- form_error($element, t('Allowed values list: keys must be integers.'));
- break;
- }
- if ($field_type == 'list_float' && !is_numeric($key)) {
- form_error($element, t('Allowed values list: each key must be a valid integer or decimal.'));
- break;
- }
- elseif ($field_type == 'list_text' && drupal_strlen($key) > 255) {
- form_error($element, t('Allowed values list: each key must be a string at most 255 characters long.'));
- break;
- }
- }
-
- // Prevent removing values currently in use.
- if ($has_data) {
- $lost_keys = array_diff(array_keys($field['settings']['allowed_values']), array_keys($values));
- if (_list_values_in_use($field, $lost_keys)) {
- form_error($element, t('Allowed values list: some values are being removed while currently in use.'));
- }
- }
-
- form_set_value($element, $values, $form_state);
- }
-}
-
-/**
-* Form element #value_callback: assembles the allowed values for 'boolean' fields.
-*/
-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 array($off, $on);
-}
-
-/**
- * Implements hook_field_update_field().
- */
-function list_field_update_field($field, $prior_field, $has_data) {
- drupal_static_reset('list_allowed_values');
-}
-
-/**
- * Returns the array of allowed values for a list field.
- *
- * The strings are not safe for output. Keys and values of the array should be
- * sanitized through field_filter_xss() before being displayed.
- *
- * @param $field
- * The field definition.
- * @param $instance
- * (optional) A field instance array. Defaults to NULL.
- * @param $entity_type
- * (optional) The type of entity; e.g. 'node' or 'user'. Defaults to NULL.
- * @param $entity
- * (optional) The entity object. Defaults to NULL.
- *
- * @return
- * The array of allowed values. Keys of the array are the raw stored values
- * (number or text), values of the array are the display labels.
- */
-function list_allowed_values($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
- $allowed_values = &drupal_static(__FUNCTION__, array());
-
- if (!isset($allowed_values[$field['id']])) {
- $function = $field['settings']['allowed_values_function'];
- // If $cacheable is FALSE, then the allowed values are not statically
- // cached. See list_test_dynamic_values_callback() for an example of
- // generating dynamic and uncached values.
- $cacheable = TRUE;
- if (!empty($function)) {
- $values = $function($field, $instance, $entity_type, $entity, $cacheable);
- }
- else {
- $values = $field['settings']['allowed_values'];
- }
-
- if ($cacheable) {
- $allowed_values[$field['id']] = $values;
- }
- else {
- return $values;
- }
- }
-
- return $allowed_values[$field['id']];
-}
-
-/**
- * Parses a string of 'allowed values' into an array.
- *
- * @param $string
- * The list of allowed values in string format described in
- * list_allowed_values_string().
- * @param $field_type
- * The field type. Either 'list_number' or 'list_text'.
- * @param $generate_keys
- * Boolean value indicating whether to generate keys based on the position of
- * the value if a key is not manually specified, and if the value cannot be
- * used as a key. This should only be TRUE for fields of type 'list_number'.
- *
- * @return
- * The array of extracted key/value pairs, or NULL if the string is invalid.
- *
- * @see list_allowed_values_string()
- */
-function list_extract_allowed_values($string, $field_type, $generate_keys) {
- $values = array();
-
- $list = explode("\n", $string);
- $list = array_map('trim', $list);
- $list = array_filter($list, 'strlen');
-
- $generated_keys = $explicit_keys = FALSE;
- foreach ($list as $position => $text) {
- $value = $key = FALSE;
-
- // Check for an explicit key.
- $matches = array();
- if (preg_match('/(.*)\|(.*)/', $text, $matches)) {
- $key = $matches[1];
- $value = $matches[2];
- $explicit_keys = TRUE;
- }
- // Otherwise see if we can use the value as the key. Detecting true integer
- // strings takes a little trick.
- elseif ($field_type == 'list_text'
- || ($field_type == 'list_float' && is_numeric($text))
- || ($field_type == 'list_integer' && is_numeric($text) && (float) $text == intval($text))) {
- $key = $value = $text;
- $explicit_keys = TRUE;
- }
- // Otherwise see if we can generate a key from the position.
- elseif ($generate_keys) {
- $key = (string) $position;
- $value = $text;
- $generated_keys = TRUE;
- }
- else {
- return;
- }
-
- // Float keys are represented as strings and need to be disambiguated
- // ('.5' is '0.5').
- if ($field_type == 'list_float' && is_numeric($key)) {
- $key = (string) (float) $key;
- }
-
- $values[$key] = $value;
- }
-
- // We generate keys only if the list contains no explicit key at all.
- if ($explicit_keys && $generated_keys) {
- return;
- }
-
- return $values;
-}
-
-/**
- * Generates a string representation of an array of 'allowed values'.
- *
- * This string format is suitable for edition in a textarea.
- *
- * @param $values
- * An array of values, where array keys are values and array values are
- * labels.
- *
- * @return
- * The string representation of the $values array:
- * - Values are separated by a carriage return.
- * - Each value is in the format "value|label" or "value".
- */
-function list_allowed_values_string($values) {
- $lines = array();
- foreach ($values as $key => $value) {
- $lines[] = "$key|$value";
- }
- return implode("\n", $lines);
-}
-
-/**
- * Implements hook_field_update_forbid().
- */
-function list_field_update_forbid($field, $prior_field, $has_data) {
- if ($field['module'] == 'list' && $has_data) {
- // Forbid any update that removes allowed values with actual data.
- $lost_keys = array_diff(array_keys($prior_field['settings']['allowed_values']), array_keys($field['settings']['allowed_values']));
- if (_list_values_in_use($field, $lost_keys)) {
- throw new FieldUpdateForbiddenException(t('A list field (@field_name) with existing data cannot have its keys changed.', array('@field_name' => $field['field_name'])));
- }
- }
-}
-
-/**
- * Checks if a list of values are being used in actual field values.
- */
-function _list_values_in_use($field, $values) {
- if ($values) {
- $query = new EntityFieldQuery();
- $found = $query
- ->fieldCondition($field['field_name'], 'value', $values)
- ->range(0, 1)
- ->execute();
- return !empty($found);
- }
-
- return FALSE;
-}
-
-/**
- * Implements hook_field_validate().
- *
- * Possible error codes:
- * - 'list_illegal_value': The value is not part of the list of allowed values.
- */
-function list_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
- $allowed_values = list_allowed_values($field, $instance, $entity_type, $entity);
- foreach ($items as $delta => $item) {
- if (!empty($item['value'])) {
- if (!empty($allowed_values) && !isset($allowed_values[$item['value']])) {
- $errors[$field['field_name']][$langcode][$delta][] = array(
- 'error' => 'list_illegal_value',
- 'message' => t('%name: illegal value.', array('%name' => $instance['label'])),
- );
- }
- }
- }
-}
-
-/**
- * Implements hook_field_is_empty().
- */
-function list_field_is_empty($item, $field) {
- if (empty($item['value']) && (string) $item['value'] !== '0') {
- return TRUE;
- }
- return FALSE;
-}
-
-/**
- * Implements hook_field_widget_info_alter().
- *
- * The List module does not implement widgets of its own, but reuses the
- * widgets defined in options.module.
- *
- * @see list_options_list()
- */
-function list_field_widget_info_alter(&$info) {
- $widgets = array(
- 'options_select' => array('list_integer', 'list_float', 'list_text'),
- 'options_buttons' => array('list_integer', 'list_float', 'list_text', 'list_boolean'),
- 'options_onoff' => array('list_boolean'),
- );
-
- foreach ($widgets as $widget => $field_types) {
- $info[$widget]['field types'] = array_merge($info[$widget]['field types'], $field_types);
- }
-}
-
-/**
- * Implements hook_options_list().
- */
-function list_options_list($field, $instance, $entity_type, $entity) {
- return list_allowed_values($field, $instance, $entity_type, $entity);
-}
-
-/**
- * Implements hook_field_formatter_info().
- */
-function list_field_formatter_info() {
- return array(
- 'list_default' => array(
- 'label' => t('Default'),
- 'field types' => array('list_integer', 'list_float', 'list_text', 'list_boolean'),
- ),
- 'list_key' => array(
- 'label' => t('Key'),
- 'field types' => array('list_integer', 'list_float', 'list_text', 'list_boolean'),
- ),
- );
-}
-
-/**
- * Implements hook_field_formatter_view().
- */
-function list_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
- $element = array();
-
- switch ($display['type']) {
- case 'list_default':
- $allowed_values = list_allowed_values($field, $instance, $entity_type, $entity);
- foreach ($items as $delta => $item) {
- if (isset($allowed_values[$item['value']])) {
- $output = field_filter_xss($allowed_values[$item['value']]);
- }
- else {
- // If no match was found in allowed values, fall back to the key.
- $output = field_filter_xss($item['value']);
- }
- $element[$delta] = array('#markup' => $output);
- }
- break;
-
- case 'list_key':
- foreach ($items as $delta => $item) {
- $element[$delta] = array('#markup' => field_filter_xss($item['value']));
- }
- break;
- }
-
- return $element;
-}
+++ /dev/null
-<?php
-
-/**
- * @file
- * Tests for list.module.
- */
-
-use Drupal\field\FieldException;
-use Drupal\field\FieldValidationException;
-use Drupal\field\Tests\FieldTestBase;
-
-/**
- * Tests for the 'List' field types.
- */
-class ListFieldTestBase extends FieldTestBase {
- public static function getInfo() {
- return array(
- 'name' => 'List field',
- 'description' => 'Test the List field type.',
- 'group' => 'Field types',
- );
- }
-
- function setUp() {
- parent::setUp(array('list', 'field_test'));
-
- $this->field_name = 'test_list';
- $this->field = array(
- 'field_name' => $this->field_name,
- 'type' => 'list_integer',
- 'cardinality' => 1,
- 'settings' => array(
- 'allowed_values' => array(1 => 'One', 2 => 'Two', 3 => 'Three'),
- ),
- );
- $this->field = field_create_field($this->field);
-
- $this->instance = array(
- 'field_name' => $this->field_name,
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- 'widget' => array(
- 'type' => 'options_buttons',
- ),
- );
- $this->instance = field_create_instance($this->instance);
- }
-
- /**
- * Test that allowed values can be updated.
- */
- function testUpdateAllowedValues() {
- $langcode = LANGUAGE_NOT_SPECIFIED;
-
- // All three options appear.
- $entity = field_test_create_stub_entity();
- $form = drupal_get_form('field_test_entity_form', $entity);
- $this->assertTrue(!empty($form[$this->field_name][$langcode][1]), t('Option 1 exists'));
- $this->assertTrue(!empty($form[$this->field_name][$langcode][2]), t('Option 2 exists'));
- $this->assertTrue(!empty($form[$this->field_name][$langcode][3]), t('Option 3 exists'));
-
- // Use one of the values in an actual entity, and check that this value
- // cannot be removed from the list.
- $entity = field_test_create_stub_entity();
- $entity->{$this->field_name}[$langcode][0] = array('value' => 1);
- field_test_entity_save($entity);
- $this->field['settings']['allowed_values'] = array(2 => 'Two');
- try {
- field_update_field($this->field);
- $this->fail(t('Cannot update a list field to not include keys with existing data.'));
- }
- catch (FieldException $e) {
- $this->pass(t('Cannot update a list field to not include keys with existing data.'));
- }
- // Empty the value, so that we can actually remove the option.
- $entity->{$this->field_name}[$langcode] = array();
- field_test_entity_save($entity);
-
- // Removed options do not appear.
- $this->field['settings']['allowed_values'] = array(2 => 'Two');
- field_update_field($this->field);
- $entity = field_test_create_stub_entity();
- $form = drupal_get_form('field_test_entity_form', $entity);
- $this->assertTrue(empty($form[$this->field_name][$langcode][1]), t('Option 1 does not exist'));
- $this->assertTrue(!empty($form[$this->field_name][$langcode][2]), t('Option 2 exists'));
- $this->assertTrue(empty($form[$this->field_name][$langcode][3]), t('Option 3 does not exist'));
-
- // Completely new options appear.
- $this->field['settings']['allowed_values'] = array(10 => 'Update', 20 => 'Twenty');
- field_update_field($this->field);
- $form = drupal_get_form('field_test_entity_form', $entity);
- $this->assertTrue(empty($form[$this->field_name][$langcode][1]), t('Option 1 does not exist'));
- $this->assertTrue(empty($form[$this->field_name][$langcode][2]), t('Option 2 does not exist'));
- $this->assertTrue(empty($form[$this->field_name][$langcode][3]), t('Option 3 does not exist'));
- $this->assertTrue(!empty($form[$this->field_name][$langcode][10]), t('Option 10 exists'));
- $this->assertTrue(!empty($form[$this->field_name][$langcode][20]), t('Option 20 exists'));
-
- // Options are reset when a new field with the same name is created.
- field_delete_field($this->field_name);
- unset($this->field['id']);
- $this->field['settings']['allowed_values'] = array(1 => 'One', 2 => 'Two', 3 => 'Three');
- $this->field = field_create_field($this->field);
- $this->instance = array(
- 'field_name' => $this->field_name,
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- 'widget' => array(
- 'type' => 'options_buttons',
- ),
- );
- $this->instance = field_create_instance($this->instance);
- $entity = field_test_create_stub_entity();
- $form = drupal_get_form('field_test_entity_form', $entity);
- $this->assertTrue(!empty($form[$this->field_name][$langcode][1]), t('Option 1 exists'));
- $this->assertTrue(!empty($form[$this->field_name][$langcode][2]), t('Option 2 exists'));
- $this->assertTrue(!empty($form[$this->field_name][$langcode][3]), t('Option 3 exists'));
- }
-}
-
-/**
- * Tests the List field allowed values function.
- */
-class ListDynamicValuesValidationTestCase extends ListDynamicValuesTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'List field dynamic values',
- 'description' => 'Test the List field allowed values function.',
- 'group' => 'Field types',
- );
- }
-
- /**
- * Test that allowed values function gets the entity.
- */
- function testDynamicAllowedValues() {
- // Verify that the test passes against every value we had.
- foreach ($this->test as $key => $value) {
- $this->entity->test_list[LANGUAGE_NOT_SPECIFIED][0]['value'] = $value;
- try {
- field_attach_validate('test_entity', $this->entity);
- $this->pass("$key should pass");
- }
- catch (FieldValidationException $e) {
- // This will display as an exception, no need for a separate error.
- throw($e);
- }
- }
- // Now verify that the test does not pass against anything else.
- foreach ($this->test as $key => $value) {
- $this->entity->test_list[LANGUAGE_NOT_SPECIFIED][0]['value'] = is_numeric($value) ? (100 - $value) : ('X' . $value);
- $pass = FALSE;
- try {
- field_attach_validate('test_entity', $this->entity);
- }
- catch (FieldValidationException $e) {
- $pass = TRUE;
- }
- $this->assertTrue($pass, $key . ' should not pass');
- }
- }
-}
-
-/**
- * List module UI tests.
- */
-class ListFieldUITestCase extends FieldTestBase {
- public static function getInfo() {
- return array(
- 'name' => 'List field UI',
- 'description' => 'Test the List field UI functionality.',
- 'group' => 'Field types',
- );
- }
-
- function setUp() {
- parent::setUp(array('list', 'field_test', 'taxonomy', 'field_ui'));
-
- // Create test user.
- $admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer taxonomy'));
- $this->drupalLogin($admin_user);
-
- // Create content type, with underscores.
- $type_name = 'test_' . strtolower($this->randomName());
- $type = $this->drupalCreateContentType(array('name' => $type_name, 'type' => $type_name));
- $this->type = $type->type;
- }
-
- /**
- * List (integer) : test 'allowed values' input.
- */
- function testListAllowedValuesInteger() {
- $this->field_name = 'field_list_integer';
- $this->createListField('list_integer');
-
- // Flat list of textual values.
- $string = "Zero\nOne";
- $array = array('0' => 'Zero', '1' => 'One');
- $this->assertAllowedValuesInput($string, $array, t('Unkeyed lists are accepted.'));
- // Explicit integer keys.
- $string = "0|Zero\n2|Two";
- $array = array('0' => 'Zero', '2' => 'Two');
- $this->assertAllowedValuesInput($string, $array, t('Integer keys are accepted.'));
- // Check that values can be added and removed.
- $string = "0|Zero\n1|One";
- $array = array('0' => 'Zero', '1' => 'One');
- $this->assertAllowedValuesInput($string, $array, t('Values can be added and removed.'));
- // Non-integer keys.
- $this->assertAllowedValuesInput("1.1|One", 'keys must be integers', t('Non integer keys are rejected.'));
- $this->assertAllowedValuesInput("abc|abc", 'keys must be integers', t('Non integer keys are rejected.'));
- // Mixed list of keyed and unkeyed values.
- $this->assertAllowedValuesInput("Zero\n1|One", 'invalid input', t('Mixed lists are rejected.'));
-
- // Create a node with actual data for the field.
- $settings = array(
- 'type' => $this->type,
- $this->field_name => array(LANGUAGE_NOT_SPECIFIED => array(array('value' => 1))),
- );
- $node = $this->drupalCreateNode($settings);
-
- // Check that a flat list of values is rejected once the field has data.
- $this->assertAllowedValuesInput( "Zero\nOne", 'invalid input', t('Unkeyed lists are rejected once the field has data.'));
-
- // Check that values can be added but values in use cannot be removed.
- $string = "0|Zero\n1|One\n2|Two";
- $array = array('0' => 'Zero', '1' => 'One', '2' => 'Two');
- $this->assertAllowedValuesInput($string, $array, t('Values can be added.'));
- $string = "0|Zero\n1|One";
- $array = array('0' => 'Zero', '1' => 'One');
- $this->assertAllowedValuesInput($string, $array, t('Values not in use can be removed.'));
- $this->assertAllowedValuesInput("0|Zero", 'some values are being removed while currently in use', t('Values in use cannot be removed.'));
-
- // Delete the node, remove the value.
- node_delete($node->nid);
- $string = "0|Zero";
- $array = array('0' => 'Zero');
- $this->assertAllowedValuesInput($string, $array, t('Values not in use can be removed.'));
- }
-
- /**
- * List (float) : test 'allowed values' input.
- */
- function testListAllowedValuesFloat() {
- $this->field_name = 'field_list_float';
- $this->createListField('list_float');
-
- // Flat list of textual values.
- $string = "Zero\nOne";
- $array = array('0' => 'Zero', '1' => 'One');
- $this->assertAllowedValuesInput($string, $array, t('Unkeyed lists are accepted.'));
- // Explicit numeric keys.
- $string = "0|Zero\n.5|Point five";
- $array = array('0' => 'Zero', '0.5' => 'Point five');
- $this->assertAllowedValuesInput($string, $array, t('Integer keys are accepted.'));
- // Check that values can be added and removed.
- $string = "0|Zero\n.5|Point five\n1.0|One";
- $array = array('0' => 'Zero', '0.5' => 'Point five', '1' => 'One');
- $this->assertAllowedValuesInput($string, $array, t('Values can be added and removed.'));
- // Non-numeric keys.
- $this->assertAllowedValuesInput("abc|abc\n", 'each key must be a valid integer or decimal', t('Non numeric keys are rejected.'));
- // Mixed list of keyed and unkeyed values.
- $this->assertAllowedValuesInput("Zero\n1|One\n", 'invalid input', t('Mixed lists are rejected.'));
-
- // Create a node with actual data for the field.
- $settings = array(
- 'type' => $this->type,
- $this->field_name => array(LANGUAGE_NOT_SPECIFIED => array(array('value' => .5))),
- );
- $node = $this->drupalCreateNode($settings);
-
- // Check that a flat list of values is rejected once the field has data.
- $this->assertAllowedValuesInput("Zero\nOne", 'invalid input', t('Unkeyed lists are rejected once the field has data.'));
-
- // Check that values can be added but values in use cannot be removed.
- $string = "0|Zero\n.5|Point five\n2|Two";
- $array = array('0' => 'Zero', '0.5' => 'Point five', '2' => 'Two');
- $this->assertAllowedValuesInput($string, $array, t('Values can be added.'));
- $string = "0|Zero\n.5|Point five";
- $array = array('0' => 'Zero', '0.5' => 'Point five');
- $this->assertAllowedValuesInput($string, $array, t('Values not in use can be removed.'));
- $this->assertAllowedValuesInput("0|Zero", 'some values are being removed while currently in use', t('Values in use cannot be removed.'));
-
- // Delete the node, remove the value.
- node_delete($node->nid);
- $string = "0|Zero";
- $array = array('0' => 'Zero');
- $this->assertAllowedValuesInput($string, $array, t('Values not in use can be removed.'));
- }
-
- /**
- * List (text) : test 'allowed values' input.
- */
- function testListAllowedValuesText() {
- $this->field_name = 'field_list_text';
- $this->createListField('list_text');
-
- // Flat list of textual values.
- $string = "Zero\nOne";
- $array = array('Zero' => 'Zero', 'One' => 'One');
- $this->assertAllowedValuesInput($string, $array, t('Unkeyed lists are accepted.'));
- // Explicit keys.
- $string = "zero|Zero\none|One";
- $array = array('zero' => 'Zero', 'one' => 'One');
- $this->assertAllowedValuesInput($string, $array, t('Explicit keys are accepted.'));
- // Check that values can be added and removed.
- $string = "zero|Zero\ntwo|Two";
- $array = array('zero' => 'Zero', 'two' => 'Two');
- $this->assertAllowedValuesInput($string, $array, t('Values can be added and removed.'));
- // Mixed list of keyed and unkeyed values.
- $string = "zero|Zero\nOne\n";
- $array = array('zero' => 'Zero', 'One' => 'One');
- $this->assertAllowedValuesInput($string, $array, t('Mixed lists are accepted.'));
- // Overly long keys.
- $this->assertAllowedValuesInput("zero|Zero\n" . $this->randomName(256) . "|One", 'each key must be a string at most 255 characters long', t('Overly long keys are rejected.'));
-
- // Create a node with actual data for the field.
- $settings = array(
- 'type' => $this->type,
- $this->field_name => array(LANGUAGE_NOT_SPECIFIED => array(array('value' => 'One'))),
- );
- $node = $this->drupalCreateNode($settings);
-
- // Check that flat lists of values are still accepted once the field has
- // data.
- $string = "Zero\nOne";
- $array = array('Zero' => 'Zero', 'One' => 'One');
- $this->assertAllowedValuesInput($string, $array, t('Unkeyed lists are still accepted once the field has data.'));
-
- // Check that values can be added but values in use cannot be removed.
- $string = "Zero\nOne\nTwo";
- $array = array('Zero' => 'Zero', 'One' => 'One', 'Two' => 'Two');
- $this->assertAllowedValuesInput($string, $array, t('Values can be added.'));
- $string = "Zero\nOne";
- $array = array('Zero' => 'Zero', 'One' => 'One');
- $this->assertAllowedValuesInput($string, $array, t('Values not in use can be removed.'));
- $this->assertAllowedValuesInput("Zero", 'some values are being removed while currently in use', t('Values in use cannot be removed.'));
-
- // Delete the node, remove the value.
- node_delete($node->nid);
- $string = "Zero";
- $array = array('Zero' => 'Zero');
- $this->assertAllowedValuesInput($string, $array, t('Values not in use can be removed.'));
- }
-
- /**
- * List (boolen) : test 'On/Off' values input.
- */
- function testListAllowedValuesBoolean() {
- $this->field_name = 'field_list_boolean';
- $this->createListField('list_boolean');
-
- // Check that the separate 'On' and 'Off' form fields work.
- $on = $this->randomName();
- $off = $this->randomName();
- $allowed_values = array(1 => $on, 0 => $off);
- $edit = array(
- 'on' => $on,
- 'off' => $off,
- );
- $this->drupalPost($this->admin_path, $edit, t('Save settings'));
- $this->assertText("Saved field_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($this->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_name);
- $this->assertEqual($field['settings']['allowed_values'], $allowed_values, 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'));
- }
-
- /**
- * Helper function to create list field of a given type.
- *
- * @param string $type
- * 'list_integer', 'list_float', 'list_text' or 'list_boolean'
- */
- protected function createListField($type) {
- // Create a test field and instance.
- $field = array(
- 'field_name' => $this->field_name,
- 'type' => $type,
- );
- field_create_field($field);
- $instance = array(
- 'field_name' => $this->field_name,
- 'entity_type' => 'node',
- 'bundle' => $this->type,
- );
- field_create_instance($instance);
-
- $this->admin_path = 'admin/structure/types/manage/' . $this->type . '/fields/' . $this->field_name;
- }
-
- /**
- * Tests a string input for the 'allowed values' form element.
- *
- * @param $input_string
- * The input string, in the pipe-linefeed format expected by the form
- * element.
- * @param $result
- * Either an expected resulting array in
- * $field['settings']['allowed_values'], or an expected error message.
- * @param $message
- * Message to display.
- */
- function assertAllowedValuesInput($input_string, $result, $message) {
- $edit = array('field[settings][allowed_values]' => $input_string);
- $this->drupalPost($this->admin_path, $edit, t('Save settings'));
-
- if (is_string($result)) {
- $this->assertText($result, $message);
- }
- else {
- field_info_cache_clear();
- $field = field_info_field($this->field_name);
- $this->assertIdentical($field['settings']['allowed_values'], $result, $message);
- }
- }
-}
+++ /dev/null
-name = "List test"
-description = "Support module for the List module tests."
-core = 8.x
-package = Testing
-version = VERSION
-hidden = TRUE
/**
* @file
- * Tests for list.module.
+ * Definition of Drupal\options\Tests\OptionsDynamicValuesTest.
*/
+namespace Drupal\options\Tests;
+
use Drupal\field\Tests\FieldTestBase;
/**
- * Sets up a List field for testing allowed values functions.
+ * Sets up a Options field for testing allowed values functions.
*/
-class ListDynamicValuesTestCase extends FieldTestBase {
+class OptionsDynamicValuesTest extends FieldTestBase {
function setUp() {
- parent::setUp(array('list', 'field_test', 'list_test'));
+ parent::setUp(array('options', 'field_test', 'options_test'));
- $this->field_name = 'test_list';
+ $this->field_name = 'test_options';
$this->field = array(
'field_name' => $this->field_name,
'type' => 'list_text',
'cardinality' => 1,
'settings' => array(
- 'allowed_values_function' => 'list_test_dynamic_values_callback',
+ 'allowed_values_function' => 'options_test_dynamic_values_callback',
),
);
$this->field = field_create_field($this->field);
$this->test = array(
'id' => mt_rand(1, 10),
// Make sure this does not equal the ID so that
- // list_test_dynamic_values_callback() always returns 4 values.
+ // options_test_dynamic_values_callback() always returns 4 values.
'vid' => mt_rand(20, 30),
'bundle' => 'test_bundle',
'label' => $this->randomName(),
--- /dev/null
+<?php
+
+/**
+ * @file
+ * Definition of Drupal\options\Tests\OptionsDynamicValuesValidationTest.
+ */
+
+namespace Drupal\options\Tests;
+
+use Drupal\field\FieldValidationException;
+
+/**
+ * Tests the Options field allowed values function.
+ */
+class OptionsDynamicValuesValidationTest extends OptionsDynamicValuesTest {
+ public static function getInfo() {
+ return array(
+ 'name' => 'Options field dynamic values',
+ 'description' => 'Test the Options field allowed values function.',
+ 'group' => 'Field types',
+ );
+ }
+
+ /**
+ * Test that allowed values function gets the entity.
+ */
+ function testDynamicAllowedValues() {
+ // Verify that the test passes against every value we had.
+ foreach ($this->test as $key => $value) {
+ $this->entity->test_options[LANGUAGE_NOT_SPECIFIED][0]['value'] = $value;
+ try {
+ field_attach_validate('test_entity', $this->entity);
+ $this->pass("$key should pass");
+ }
+ catch (FieldValidationException $e) {
+ // This will display as an exception, no need for a separate error.
+ throw($e);
+ }
+ }
+ // Now verify that the test does not pass against anything else.
+ foreach ($this->test as $key => $value) {
+ $this->entity->test_options[LANGUAGE_NOT_SPECIFIED][0]['value'] = is_numeric($value) ? (100 - $value) : ('X' . $value);
+ $pass = FALSE;
+ try {
+ field_attach_validate('test_entity', $this->entity);
+ }
+ catch (FieldValidationException $e) {
+ $pass = TRUE;
+ }
+ $this->assertTrue($pass, $key . ' should not pass');
+ }
+ }
+}
--- /dev/null
+<?php
+
+/**
+ * @file
+ * Definition of Drupal\options\Tests\OptionsFieldTest.
+ */
+
+namespace Drupal\options\Tests;
+
+use Drupal\field\FieldException;
+use Drupal\field\Tests\FieldTestBase;
+
+/**
+ * Tests for the 'Options' field types.
+ */
+class OptionsFieldTest extends FieldTestBase {
+ public static function getInfo() {
+ return array(
+ 'name' => 'Options field',
+ 'description' => 'Test the Options field type.',
+ 'group' => 'Field types',
+ );
+ }
+
+ function setUp() {
+ parent::setUp(array('options', 'field_test'));
+
+ $this->field_name = 'test_options';
+ $this->field = array(
+ 'field_name' => $this->field_name,
+ 'type' => 'list_integer',
+ 'cardinality' => 1,
+ 'settings' => array(
+ 'allowed_values' => array(1 => 'One', 2 => 'Two', 3 => 'Three'),
+ ),
+ );
+ $this->field = field_create_field($this->field);
+
+ $this->instance = array(
+ 'field_name' => $this->field_name,
+ 'entity_type' => 'test_entity',
+ 'bundle' => 'test_bundle',
+ 'widget' => array(
+ 'type' => 'options_buttons',
+ ),
+ );
+ $this->instance = field_create_instance($this->instance);
+ }
+
+ /**
+ * Test that allowed values can be updated.
+ */
+ function testUpdateAllowedValues() {
+ $langcode = LANGUAGE_NOT_SPECIFIED;
+
+ // All three options appear.
+ $entity = field_test_create_stub_entity();
+ $form = drupal_get_form('field_test_entity_form', $entity);
+ $this->assertTrue(!empty($form[$this->field_name][$langcode][1]), t('Option 1 exists'));
+ $this->assertTrue(!empty($form[$this->field_name][$langcode][2]), t('Option 2 exists'));
+ $this->assertTrue(!empty($form[$this->field_name][$langcode][3]), t('Option 3 exists'));
+
+ // Use one of the values in an actual entity, and check that this value
+ // cannot be removed from the list.
+ $entity = field_test_create_stub_entity();
+ $entity->{$this->field_name}[$langcode][0] = array('value' => 1);
+ field_test_entity_save($entity);
+ $this->field['settings']['allowed_values'] = array(2 => 'Two');
+ try {
+ field_update_field($this->field);
+ $this->fail(t('Cannot update a list field to not include keys with existing data.'));
+ }
+ catch (FieldException $e) {
+ $this->pass(t('Cannot update a list field to not include keys with existing data.'));
+ }
+ // Empty the value, so that we can actually remove the option.
+ $entity->{$this->field_name}[$langcode] = array();
+ field_test_entity_save($entity);
+
+ // Removed options do not appear.
+ $this->field['settings']['allowed_values'] = array(2 => 'Two');
+ field_update_field($this->field);
+ $entity = field_test_create_stub_entity();
+ $form = drupal_get_form('field_test_entity_form', $entity);
+ $this->assertTrue(empty($form[$this->field_name][$langcode][1]), t('Option 1 does not exist'));
+ $this->assertTrue(!empty($form[$this->field_name][$langcode][2]), t('Option 2 exists'));
+ $this->assertTrue(empty($form[$this->field_name][$langcode][3]), t('Option 3 does not exist'));
+
+ // Completely new options appear.
+ $this->field['settings']['allowed_values'] = array(10 => 'Update', 20 => 'Twenty');
+ field_update_field($this->field);
+ $form = drupal_get_form('field_test_entity_form', $entity);
+ $this->assertTrue(empty($form[$this->field_name][$langcode][1]), t('Option 1 does not exist'));
+ $this->assertTrue(empty($form[$this->field_name][$langcode][2]), t('Option 2 does not exist'));
+ $this->assertTrue(empty($form[$this->field_name][$langcode][3]), t('Option 3 does not exist'));
+ $this->assertTrue(!empty($form[$this->field_name][$langcode][10]), t('Option 10 exists'));
+ $this->assertTrue(!empty($form[$this->field_name][$langcode][20]), t('Option 20 exists'));
+
+ // Options are reset when a new field with the same name is created.
+ field_delete_field($this->field_name);
+ unset($this->field['id']);
+ $this->field['settings']['allowed_values'] = array(1 => 'One', 2 => 'Two', 3 => 'Three');
+ $this->field = field_create_field($this->field);
+ $this->instance = array(
+ 'field_name' => $this->field_name,
+ 'entity_type' => 'test_entity',
+ 'bundle' => 'test_bundle',
+ 'widget' => array(
+ 'type' => 'options_buttons',
+ ),
+ );
+ $this->instance = field_create_instance($this->instance);
+ $entity = field_test_create_stub_entity();
+ $form = drupal_get_form('field_test_entity_form', $entity);
+ $this->assertTrue(!empty($form[$this->field_name][$langcode][1]), t('Option 1 exists'));
+ $this->assertTrue(!empty($form[$this->field_name][$langcode][2]), t('Option 2 exists'));
+ $this->assertTrue(!empty($form[$this->field_name][$langcode][3]), t('Option 3 exists'));
+ }
+}
--- /dev/null
+<?php
+
+/**
+ * @file
+ * Definition of Drupal\options\Tests\OptionsFieldUITest.
+ */
+
+namespace Drupal\options\Tests;
+
+use Drupal\field\Tests\FieldTestBase;
+
+/**
+ * Options module UI tests.
+ */
+class OptionsFieldUITest extends FieldTestBase {
+ public static function getInfo() {
+ return array(
+ 'name' => 'Options field UI',
+ 'description' => 'Test the Options field UI functionality.',
+ 'group' => 'Field types',
+ );
+ }
+
+ function setUp() {
+ parent::setUp(array('options', 'field_test', 'taxonomy', 'field_ui'));
+
+ // Create test user.
+ $admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer taxonomy'));
+ $this->drupalLogin($admin_user);
+
+ // Create content type, with underscores.
+ $type_name = 'test_' . strtolower($this->randomName());
+ $type = $this->drupalCreateContentType(array('name' => $type_name, 'type' => $type_name));
+ $this->type = $type->type;
+ }
+
+ /**
+ * Options (integer) : test 'allowed values' input.
+ */
+ function testOptionsAllowedValuesInteger() {
+ $this->field_name = 'field_options_integer';
+ $this->createOptionsField('list_integer');
+
+ // Flat list of textual values.
+ $string = "Zero\nOne";
+ $array = array('0' => 'Zero', '1' => 'One');
+ $this->assertAllowedValuesInput($string, $array, t('Unkeyed lists are accepted.'));
+ // Explicit integer keys.
+ $string = "0|Zero\n2|Two";
+ $array = array('0' => 'Zero', '2' => 'Two');
+ $this->assertAllowedValuesInput($string, $array, t('Integer keys are accepted.'));
+ // Check that values can be added and removed.
+ $string = "0|Zero\n1|One";
+ $array = array('0' => 'Zero', '1' => 'One');
+ $this->assertAllowedValuesInput($string, $array, t('Values can be added and removed.'));
+ // Non-integer keys.
+ $this->assertAllowedValuesInput("1.1|One", 'keys must be integers', t('Non integer keys are rejected.'));
+ $this->assertAllowedValuesInput("abc|abc", 'keys must be integers', t('Non integer keys are rejected.'));
+ // Mixed list of keyed and unkeyed values.
+ $this->assertAllowedValuesInput("Zero\n1|One", 'invalid input', t('Mixed lists are rejected.'));
+
+ // Create a node with actual data for the field.
+ $settings = array(
+ 'type' => $this->type,
+ $this->field_name => array(LANGUAGE_NOT_SPECIFIED => array(array('value' => 1))),
+ );
+ $node = $this->drupalCreateNode($settings);
+
+ // Check that a flat list of values is rejected once the field has data.
+ $this->assertAllowedValuesInput( "Zero\nOne", 'invalid input', t('Unkeyed lists are rejected once the field has data.'));
+
+ // Check that values can be added but values in use cannot be removed.
+ $string = "0|Zero\n1|One\n2|Two";
+ $array = array('0' => 'Zero', '1' => 'One', '2' => 'Two');
+ $this->assertAllowedValuesInput($string, $array, t('Values can be added.'));
+ $string = "0|Zero\n1|One";
+ $array = array('0' => 'Zero', '1' => 'One');
+ $this->assertAllowedValuesInput($string, $array, t('Values not in use can be removed.'));
+ $this->assertAllowedValuesInput("0|Zero", 'some values are being removed while currently in use', t('Values in use cannot be removed.'));
+
+ // Delete the node, remove the value.
+ node_delete($node->nid);
+ $string = "0|Zero";
+ $array = array('0' => 'Zero');
+ $this->assertAllowedValuesInput($string, $array, t('Values not in use can be removed.'));
+ }
+
+ /**
+ * Options (float) : test 'allowed values' input.
+ */
+ function testOptionsAllowedValuesFloat() {
+ $this->field_name = 'field_options_float';
+ $this->createOptionsField('list_float');
+
+ // Flat list of textual values.
+ $string = "Zero\nOne";
+ $array = array('0' => 'Zero', '1' => 'One');
+ $this->assertAllowedValuesInput($string, $array, t('Unkeyed lists are accepted.'));
+ // Explicit numeric keys.
+ $string = "0|Zero\n.5|Point five";
+ $array = array('0' => 'Zero', '0.5' => 'Point five');
+ $this->assertAllowedValuesInput($string, $array, t('Integer keys are accepted.'));
+ // Check that values can be added and removed.
+ $string = "0|Zero\n.5|Point five\n1.0|One";
+ $array = array('0' => 'Zero', '0.5' => 'Point five', '1' => 'One');
+ $this->assertAllowedValuesInput($string, $array, t('Values can be added and removed.'));
+ // Non-numeric keys.
+ $this->assertAllowedValuesInput("abc|abc\n", 'each key must be a valid integer or decimal', t('Non numeric keys are rejected.'));
+ // Mixed list of keyed and unkeyed values.
+ $this->assertAllowedValuesInput("Zero\n1|One\n", 'invalid input', t('Mixed lists are rejected.'));
+
+ // Create a node with actual data for the field.
+ $settings = array(
+ 'type' => $this->type,
+ $this->field_name => array(LANGUAGE_NOT_SPECIFIED => array(array('value' => .5))),
+ );
+ $node = $this->drupalCreateNode($settings);
+
+ // Check that a flat list of values is rejected once the field has data.
+ $this->assertAllowedValuesInput("Zero\nOne", 'invalid input', t('Unkeyed lists are rejected once the field has data.'));
+
+ // Check that values can be added but values in use cannot be removed.
+ $string = "0|Zero\n.5|Point five\n2|Two";
+ $array = array('0' => 'Zero', '0.5' => 'Point five', '2' => 'Two');
+ $this->assertAllowedValuesInput($string, $array, t('Values can be added.'));
+ $string = "0|Zero\n.5|Point five";
+ $array = array('0' => 'Zero', '0.5' => 'Point five');
+ $this->assertAllowedValuesInput($string, $array, t('Values not in use can be removed.'));
+ $this->assertAllowedValuesInput("0|Zero", 'some values are being removed while currently in use', t('Values in use cannot be removed.'));
+
+ // Delete the node, remove the value.
+ node_delete($node->nid);
+ $string = "0|Zero";
+ $array = array('0' => 'Zero');
+ $this->assertAllowedValuesInput($string, $array, t('Values not in use can be removed.'));
+ }
+
+ /**
+ * Options (text) : test 'allowed values' input.
+ */
+ function testOptionsAllowedValuesText() {
+ $this->field_name = 'field_options_text';
+ $this->createOptionsField('list_text');
+
+ // Flat list of textual values.
+ $string = "Zero\nOne";
+ $array = array('Zero' => 'Zero', 'One' => 'One');
+ $this->assertAllowedValuesInput($string, $array, t('Unkeyed lists are accepted.'));
+ // Explicit keys.
+ $string = "zero|Zero\none|One";
+ $array = array('zero' => 'Zero', 'one' => 'One');
+ $this->assertAllowedValuesInput($string, $array, t('Explicit keys are accepted.'));
+ // Check that values can be added and removed.
+ $string = "zero|Zero\ntwo|Two";
+ $array = array('zero' => 'Zero', 'two' => 'Two');
+ $this->assertAllowedValuesInput($string, $array, t('Values can be added and removed.'));
+ // Mixed list of keyed and unkeyed values.
+ $string = "zero|Zero\nOne\n";
+ $array = array('zero' => 'Zero', 'One' => 'One');
+ $this->assertAllowedValuesInput($string, $array, t('Mixed lists are accepted.'));
+ // Overly long keys.
+ $this->assertAllowedValuesInput("zero|Zero\n" . $this->randomName(256) . "|One", 'each key must be a string at most 255 characters long', t('Overly long keys are rejected.'));
+
+ // Create a node with actual data for the field.
+ $settings = array(
+ 'type' => $this->type,
+ $this->field_name => array(LANGUAGE_NOT_SPECIFIED => array(array('value' => 'One'))),
+ );
+ $node = $this->drupalCreateNode($settings);
+
+ // Check that flat lists of values are still accepted once the field has
+ // data.
+ $string = "Zero\nOne";
+ $array = array('Zero' => 'Zero', 'One' => 'One');
+ $this->assertAllowedValuesInput($string, $array, t('Unkeyed lists are still accepted once the field has data.'));
+
+ // Check that values can be added but values in use cannot be removed.
+ $string = "Zero\nOne\nTwo";
+ $array = array('Zero' => 'Zero', 'One' => 'One', 'Two' => 'Two');
+ $this->assertAllowedValuesInput($string, $array, t('Values can be added.'));
+ $string = "Zero\nOne";
+ $array = array('Zero' => 'Zero', 'One' => 'One');
+ $this->assertAllowedValuesInput($string, $array, t('Values not in use can be removed.'));
+ $this->assertAllowedValuesInput("Zero", 'some values are being removed while currently in use', t('Values in use cannot be removed.'));
+
+ // Delete the node, remove the value.
+ node_delete($node->nid);
+ $string = "Zero";
+ $array = array('Zero' => 'Zero');
+ $this->assertAllowedValuesInput($string, $array, t('Values not in use can be removed.'));
+ }
+
+ /**
+ * Options (boolen) : test 'On/Off' values input.
+ */
+ function testOptionsAllowedValuesBoolean() {
+ $this->field_name = 'field_options_boolean';
+ $this->createOptionsField('list_boolean');
+
+ // Check that the separate 'On' and 'Off' form fields work.
+ $on = $this->randomName();
+ $off = $this->randomName();
+ $allowed_values = array(1 => $on, 0 => $off);
+ $edit = array(
+ 'on' => $on,
+ 'off' => $off,
+ );
+ $this->drupalPost($this->admin_path, $edit, t('Save settings'));
+ $this->assertText("Saved field_options_boolean configuration.", t("The 'On' and 'Off' form fields work for boolean fields."));
+ // Test the allowed_values on the field settings form.
+ $this->drupalGet($this->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_name);
+ $this->assertEqual($field['settings']['allowed_values'], $allowed_values, 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'));
+ }
+
+ /**
+ * Helper function to create list field of a given type.
+ *
+ * @param string $type
+ * 'list_integer', 'list_float', 'list_text' or 'list_boolean'
+ */
+ protected function createOptionsField($type) {
+ // Create a test field and instance.
+ $field = array(
+ 'field_name' => $this->field_name,
+ 'type' => $type,
+ );
+ field_create_field($field);
+ $instance = array(
+ 'field_name' => $this->field_name,
+ 'entity_type' => 'node',
+ 'bundle' => $this->type,
+ );
+ field_create_instance($instance);
+
+ $this->admin_path = 'admin/structure/types/manage/' . $this->type . '/fields/' . $this->field_name;
+ }
+
+ /**
+ * Tests a string input for the 'allowed values' form element.
+ *
+ * @param $input_string
+ * The input string, in the pipe-linefeed format expected by the form
+ * element.
+ * @param $result
+ * Either an expected resulting array in
+ * $field['settings']['allowed_values'], or an expected error message.
+ * @param $message
+ * Message to display.
+ */
+ function assertAllowedValuesInput($input_string, $result, $message) {
+ $edit = array('field[settings][allowed_values]' => $input_string);
+ $this->drupalPost($this->admin_path, $edit, t('Save settings'));
+
+ if (is_string($result)) {
+ $this->assertText($result, $message);
+ }
+ else {
+ field_info_cache_clear();
+ $field = field_info_field($this->field_name);
+ $this->assertIdentical($field['settings']['allowed_values'], $result, $message);
+ }
+ }
+}
/**
* @file
- * Definition of Drupal\options\OptionsWidgetsTest.
+ * Definition of Drupal\options\Tests\OptionsSelectDynamicValuesTest.
*/
namespace Drupal\options\Tests;
-use ListDynamicValuesTestCase;
-
/**
- * Test an options select on a list field with a dynamic allowed values function.
+ * Tests an options select with a dynamic allowed values function.
*/
-class OptionsSelectDynamicValuesTest extends ListDynamicValuesTestCase {
+class OptionsSelectDynamicValuesTest extends OptionsDynamicValuesTest {
public static function getInfo() {
return array(
'name' => 'Options select dynamic values',
// Display form.
$this->drupalGet('test-entity/manage/' . $this->entity->ftid . '/edit');
- $options = $this->xpath('//select[@id="edit-test-list-und"]/option');
+ $options = $this->xpath('//select[@id="edit-test-options-und"]/option');
$this->assertEqual(count($options), count($this->test) + 1);
foreach ($options as $option) {
$value = (string) $option['value'];
/**
* @file
- * Definition of Drupal\options\OptionsWidgetsTest.
+ * Definition of Drupal\options\Tests\OptionsWidgetsTest.
*/
namespace Drupal\options\Tests;
}
function setUp() {
- parent::setUp(array('list', 'field_test', 'list_test', 'taxonomy', 'field_ui'));
+ parent::setUp(array('options', 'field_test', 'options_test', 'taxonomy', 'field_ui'));
// Field with cardinality 1.
$this->card_1 = array(
// Test optgroups.
$this->card_1['settings']['allowed_values'] = array();
- $this->card_1['settings']['allowed_values_function'] = 'list_test_allowed_values_callback';
+ $this->card_1['settings']['allowed_values_function'] = 'options_test_allowed_values_callback';
field_update_field($this->card_1);
// Display form: with no field data, nothing is selected
// 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';
+ $this->card_2['settings']['allowed_values_function'] = 'options_test_allowed_values_callback';
field_update_field($this->card_2);
$instance['required'] = FALSE;
field_update_instance($instance);
/**
* @file
- * Install, update and uninstall functions for the list module.
+ * Install, update and uninstall functions for the options module.
*/
/**
* Implements hook_field_schema().
*/
-function list_field_schema($field) {
+function options_field_schema($field) {
switch ($field['type']) {
case 'list_text':
$columns = array(
* Defines selection, check box and radio button widgets for text and numeric fields.
*/
+use Drupal\field\FieldUpdateForbiddenException;
+use Drupal\entity\EntityFieldQuery;
+
/**
* Implements hook_help().
*/
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>';
+ $output .= '<p>' . t('The Options module defines various fields for storing a list of items, for use with the Field module. Usually these items are entered through a select list, checkboxes, or radio buttons. 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_field_info().
+ */
+function options_field_info() {
+ return array(
+ 'list_integer' => array(
+ 'label' => t('List (integer)'),
+ 'description' => t("This field stores integer values from a list of allowed 'value => label' pairs, i.e. 'Lifetime in days': 1 => 1 day, 7 => 1 week, 31 => 1 month."),
+ 'settings' => array('allowed_values' => array(), 'allowed_values_function' => ''),
+ 'default_widget' => 'options_select',
+ 'default_formatter' => 'list_default',
+ ),
+ 'list_float' => array(
+ 'label' => t('List (float)'),
+ 'description' => t("This field stores float values from a list of allowed 'value => label' pairs, i.e. 'Fraction': 0 => 0, .25 => 1/4, .75 => 3/4, 1 => 1."),
+ 'settings' => array('allowed_values' => array(), 'allowed_values_function' => ''),
+ 'default_widget' => 'options_select',
+ 'default_formatter' => 'list_default',
+ ),
+ 'list_text' => array(
+ 'label' => t('List (text)'),
+ 'description' => t("This field stores text values from a list of allowed 'value => label' pairs, i.e. 'US States': IL => Illinois, IA => Iowa, IN => Indiana."),
+ 'settings' => array('allowed_values' => array(), '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' => array(), 'allowed_values_function' => ''),
+ 'default_widget' => 'options_buttons',
+ 'default_formatter' => 'list_default',
+ ),
+ );
+}
+
+/**
+ * Implements hook_field_settings_form().
+ */
+function options_field_settings_form($field, $instance, $has_data) {
+ $settings = $field['settings'];
+
+ switch ($field['type']) {
+ case 'list_integer':
+ case 'list_float':
+ case 'list_text':
+ $form['allowed_values'] = array(
+ '#type' => 'textarea',
+ '#title' => t('Allowed values list'),
+ '#default_value' => options_allowed_values_string($settings['allowed_values']),
+ '#rows' => 10,
+ '#element_validate' => array('options_field_settings_form_validate_allowed_values'),
+ '#field_has_data' => $has_data,
+ '#field' => $field,
+ '#field_type' => $field['type'],
+ '#access' => empty($settings['allowed_values_function']),
+ );
+
+ $description = '<p>' . t('The possible values this field can contain. Enter one value per line, in the format key|label.');
+ if ($field['type'] == 'list_integer' || $field['type'] == 'list_float') {
+ $description .= '<br/>' . t('The key is the stored value, and must be numeric. The label will be used in displayed values and edit forms.');
+ $description .= '<br/>' . t('The label is optional: if a line contains a single number, it will be used as key and label.');
+ $description .= '<br/>' . t('Lists of labels are also accepted (one label per line), only if the field does not hold any values yet. Numeric keys will be automatically generated from the positions in the list.');
+ }
+ else {
+ $description .= '<br/>' . t('The key is the stored value. The label will be used in displayed values and edit forms.');
+ $description .= '<br/>' . t('The label is optional: if a line contains a single string, it will be used as key and label.');
+ }
+ $description .= '</p>';
+ $form['allowed_values']['#description'] = $description;
+
+ break;
+
+ case 'list_boolean':
+ $values = $settings['allowed_values'];
+ $off_value = array_shift($values);
+ $on_value = array_shift($values);
+
+ $form['allowed_values'] = array(
+ '#type' => 'value',
+ '#description' => '',
+ '#value_callback' => 'options_field_settings_form_value_boolean_allowed_values',
+ '#access' => empty($settings['allowed_values_function']),
+ );
+ $form['allowed_values']['on'] = array(
+ '#type' => 'textfield',
+ '#title' => t('On value'),
+ '#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',
+ '#title' => t('Off value'),
+ '#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'];
+
+ break;
+ }
+
+ // Alter the description for allowed values depending on the widget type.
+ if ($instance['widget']['type'] == 'options_onoff') {
+ $form['allowed_values']['#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>';
+ }
+ elseif ($instance['widget']['type'] == 'options_buttons') {
+ $form['allowed_values']['#description'] .= '<p>' . t("The 'checkboxes/radio buttons' widget will display checkboxes if the <em>Number of values</em> option is greater than 1 for this field, otherwise radios will be displayed.") . '</p>';
+ }
+ $form['allowed_values']['#description'] .= '<p>' . t('Allowed HTML tags in labels: @tags', array('@tags' => _field_filter_xss_display_allowed_tags())) . '</p>';
+
+ $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;
+}
+
+/**
+ * Element validate callback; check that the entered values are valid.
+ */
+function options_field_settings_form_validate_allowed_values($element, &$form_state) {
+ $field = $element['#field'];
+ $has_data = $element['#field_has_data'];
+ $field_type = $field['type'];
+ $generate_keys = ($field_type == 'list_integer' || $field_type == 'list_float') && !$has_data;
+
+ $values = options_extract_allowed_values($element['#value'], $field['type'], $generate_keys);
+
+ if (!is_array($values)) {
+ form_error($element, t('Allowed values list: invalid input.'));
+ }
+ else {
+ // Check that keys are valid for the field type.
+ foreach ($values as $key => $value) {
+ if ($field_type == 'list_integer' && !preg_match('/^-?\d+$/', $key)) {
+ form_error($element, t('Allowed values list: keys must be integers.'));
+ break;
+ }
+ if ($field_type == 'list_float' && !is_numeric($key)) {
+ form_error($element, t('Allowed values list: each key must be a valid integer or decimal.'));
+ break;
+ }
+ elseif ($field_type == 'list_text' && drupal_strlen($key) > 255) {
+ form_error($element, t('Allowed values list: each key must be a string at most 255 characters long.'));
+ break;
+ }
+ }
+
+ // Prevent removing values currently in use.
+ if ($has_data) {
+ $lost_keys = array_diff(array_keys($field['settings']['allowed_values']), array_keys($values));
+ if (_options_values_in_use($field, $lost_keys)) {
+ form_error($element, t('Allowed values list: some values are being removed while currently in use.'));
+ }
+ }
+
+ form_set_value($element, $values, $form_state);
+ }
+}
+
+/**
+* Form element #value_callback: assembles the allowed values for 'boolean' fields.
+*/
+function options_field_settings_form_value_boolean_allowed_values($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 array($off, $on);
+}
+
+/**
+ * Implements hook_field_update_field().
+ */
+function options_field_update_field($field, $prior_field, $has_data) {
+ drupal_static_reset('options_allowed_values');
+}
+
+/**
+ * Returns the array of allowed values for a list field.
+ *
+ * The strings are not safe for output. Keys and values of the array should be
+ * sanitized through field_filter_xss() before being displayed.
+ *
+ * @param $field
+ * The field definition.
+ * @param $instance
+ * (optional) A field instance array. Defaults to NULL.
+ * @param $entity_type
+ * (optional) The type of entity; e.g. 'node' or 'user'. Defaults to NULL.
+ * @param $entity
+ * (optional) The entity object. Defaults to NULL.
+ *
+ * @return
+ * The array of allowed values. Keys of the array are the raw stored values
+ * (number or text), values of the array are the display labels.
+ */
+function options_allowed_values($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
+ $allowed_values = &drupal_static(__FUNCTION__, array());
+
+ if (!isset($allowed_values[$field['id']])) {
+ $function = $field['settings']['allowed_values_function'];
+ // If $cacheable is FALSE, then the allowed values are not statically
+ // cached. See options_test_dynamic_values_callback() for an example of
+ // generating dynamic and uncached values.
+ $cacheable = TRUE;
+ if (!empty($function)) {
+ $values = $function($field, $instance, $entity_type, $entity, $cacheable);
+ }
+ else {
+ $values = $field['settings']['allowed_values'];
+ }
+
+ if ($cacheable) {
+ $allowed_values[$field['id']] = $values;
+ }
+ else {
+ return $values;
+ }
+ }
+
+ return $allowed_values[$field['id']];
+}
+
+/**
+ * Parses a string of 'allowed values' into an array.
+ *
+ * @param $string
+ * The list of allowed values in string format described in
+ * options_allowed_values_string().
+ * @param $field_type
+ * The field type. Either 'list_number' or 'list_text'.
+ * @param $generate_keys
+ * Boolean value indicating whether to generate keys based on the position of
+ * the value if a key is not manually specified, and if the value cannot be
+ * used as a key. This should only be TRUE for fields of type 'list_number'.
+ *
+ * @return
+ * The array of extracted key/value pairs, or NULL if the string is invalid.
+ *
+ * @see options_allowed_values_string()
+ */
+function options_extract_allowed_values($string, $field_type, $generate_keys) {
+ $values = array();
+
+ $list = explode("\n", $string);
+ $list = array_map('trim', $list);
+ $list = array_filter($list, 'strlen');
+
+ $generated_keys = $explicit_keys = FALSE;
+ foreach ($list as $position => $text) {
+ $value = $key = FALSE;
+
+ // Check for an explicit key.
+ $matches = array();
+ if (preg_match('/(.*)\|(.*)/', $text, $matches)) {
+ $key = $matches[1];
+ $value = $matches[2];
+ $explicit_keys = TRUE;
+ }
+ // Otherwise see if we can use the value as the key. Detecting true integer
+ // strings takes a little trick.
+ elseif ($field_type == 'list_text'
+ || ($field_type == 'list_float' && is_numeric($text))
+ || ($field_type == 'list_integer' && is_numeric($text) && (float) $text == intval($text))) {
+ $key = $value = $text;
+ $explicit_keys = TRUE;
+ }
+ // Otherwise see if we can generate a key from the position.
+ elseif ($generate_keys) {
+ $key = (string) $position;
+ $value = $text;
+ $generated_keys = TRUE;
+ }
+ else {
+ return;
+ }
+
+ // Float keys are represented as strings and need to be disambiguated
+ // ('.5' is '0.5').
+ if ($field_type == 'list_float' && is_numeric($key)) {
+ $key = (string) (float) $key;
+ }
+
+ $values[$key] = $value;
+ }
+
+ // We generate keys only if the list contains no explicit key at all.
+ if ($explicit_keys && $generated_keys) {
+ return;
+ }
+
+ return $values;
+}
+
+/**
+ * Generates a string representation of an array of 'allowed values'.
+ *
+ * This string format is suitable for edition in a textarea.
+ *
+ * @param $values
+ * An array of values, where array keys are values and array values are
+ * labels.
+ *
+ * @return
+ * The string representation of the $values array:
+ * - Values are separated by a carriage return.
+ * - Each value is in the format "value|label" or "value".
+ */
+function options_allowed_values_string($values) {
+ $lines = array();
+ foreach ($values as $key => $value) {
+ $lines[] = "$key|$value";
+ }
+ return implode("\n", $lines);
+}
+
+/**
+ * Implements hook_field_update_forbid().
+ */
+function options_field_update_forbid($field, $prior_field, $has_data) {
+ if ($field['module'] == 'options' && $has_data) {
+ // Forbid any update that removes allowed values with actual data.
+ $lost_keys = array_diff(array_keys($prior_field['settings']['allowed_values']), array_keys($field['settings']['allowed_values']));
+ if (_options_values_in_use($field, $lost_keys)) {
+ throw new FieldUpdateForbiddenException(t('A list field (@field_name) with existing data cannot have its keys changed.', array('@field_name' => $field['field_name'])));
+ }
+ }
+}
+
+/**
+ * Checks if a list of values are being used in actual field values.
+ */
+function _options_values_in_use($field, $values) {
+ if ($values) {
+ $query = new EntityFieldQuery();
+ $found = $query
+ ->fieldCondition($field['field_name'], 'value', $values)
+ ->range(0, 1)
+ ->execute();
+ return !empty($found);
+ }
+
+ return FALSE;
+}
+
+/**
+ * Implements hook_field_validate().
+ *
+ * Possible error codes:
+ * - 'list_illegal_value': The value is not part of the list of allowed values.
+ */
+function options_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
+ $allowed_values = options_allowed_values($field, $instance, $entity_type, $entity);
+ foreach ($items as $delta => $item) {
+ if (!empty($item['value'])) {
+ if (!empty($allowed_values) && !isset($allowed_values[$item['value']])) {
+ $errors[$field['field_name']][$langcode][$delta][] = array(
+ 'error' => 'list_illegal_value',
+ 'message' => t('%name: illegal value.', array('%name' => $instance['label'])),
+ );
+ }
+ }
+ }
+}
+
+/**
+ * Implements hook_field_is_empty().
+ */
+function options_field_is_empty($item, $field) {
+ if (empty($item['value']) && (string) $item['value'] !== '0') {
+ return TRUE;
+ }
+ return FALSE;
+}
+
+/**
* 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(),
+ 'field types' => array('list_integer', 'list_float', 'list_text'),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_CUSTOM,
),
),
'options_buttons' => array(
'label' => t('Check boxes/radio buttons'),
- 'field types' => array(),
+ 'field types' => array('list_integer', 'list_float', 'list_text', 'list_boolean'),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_CUSTOM,
),
),
'options_onoff' => array(
'label' => t('Single on/off checkbox'),
- 'field types' => array(),
+ 'field types' => array('list_boolean'),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_CUSTOM,
),
}
/**
+ * Implements hook_options_list().
+ */
+function options_options_list($field, $instance, $entity_type, $entity) {
+ return options_allowed_values($field, $instance, $entity_type, $entity);
+}
+
+/**
* 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.
return $output;
}
+
+/**
+ * Implements hook_field_formatter_info().
+ */
+function options_field_formatter_info() {
+ return array(
+ 'list_default' => array(
+ 'label' => t('Default'),
+ 'field types' => array('list_integer', 'list_float', 'list_text', 'list_boolean'),
+ ),
+ 'list_key' => array(
+ 'label' => t('Key'),
+ 'field types' => array('list_integer', 'list_float', 'list_text', 'list_boolean'),
+ ),
+ );
+}
+
+/**
+ * Implements hook_field_formatter_view().
+ */
+function options_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
+ $element = array();
+
+ switch ($display['type']) {
+ case 'list_default':
+ $allowed_values = options_allowed_values($field, $instance, $entity_type, $entity);
+ foreach ($items as $delta => $item) {
+ if (isset($allowed_values[$item['value']])) {
+ $output = field_filter_xss($allowed_values[$item['value']]);
+ }
+ else {
+ // If no match was found in allowed values, fall back to the key.
+ $output = field_filter_xss($item['value']);
+ }
+ $element[$delta] = array('#markup' => $output);
+ }
+ break;
+
+ case 'list_key':
+ foreach ($items as $delta => $item) {
+ $element[$delta] = array('#markup' => field_filter_xss($item['value']));
+ }
+ break;
+ }
+
+ return $element;
+}
--- /dev/null
+name = "Options test"
+description = "Support module for the Options module tests."
+core = 8.x
+package = Testing
+version = VERSION
+hidden = TRUE
/**
* Allowed values callback.
*/
-function list_test_allowed_values_callback($field) {
+function options_test_allowed_values_callback($field) {
$values = array(
'Group 1' => array(
0 => 'Zero',
/**
* An entity-bound allowed values callback.
*/
-function list_test_dynamic_values_callback($field, $instance, $entity_type, $entity, &$cacheable) {
+function options_test_dynamic_values_callback($field, $instance, $entity_type, $entity, &$cacheable) {
$cacheable = FALSE;
// We need the values of the entity as keys.
return drupal_map_assoc(array_merge(array($entity->ftlabel), entity_extract_ids($entity_type, $entity)));
}
function setUp() {
- parent::setUp(array('field_ui', 'field_test', 'text', 'list'));
+ parent::setUp(array('field_ui', 'field_test', 'text', 'options'));
// Create Article node type.
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
dependencies[] = dashboard
dependencies[] = help
dependencies[] = image
-dependencies[] = list
dependencies[] = menu
dependencies[] = number
dependencies[] = options