Commit 071dccf6 authored by alexpott's avatar alexpott

Issue #680546 followup by swentel, Cottser, sun: [Followup] Add a custom...

Issue #680546 followup by swentel, Cottser, sun: [Followup] Add a custom 'number of values' for 'multiple / multi-value' field. (e.g. nodereference, imagefield).
parent 7361d56b
......@@ -4303,14 +4303,14 @@ function form_pre_render_tel($element) {
* @param array $element
* An associative array containing the properties of the element.
* Properties used: #title, #value, #description, #min, #max, #placeholder,
* #required, #attributes, #step.
* #required, #attributes, #step, #size.
*
* @return array
* The $element with prepared variables ready for theme_input().
*/
function form_pre_render_number($element) {
$element['#attributes']['type'] = 'number';
element_set_attributes($element, array('id', 'name', 'value', 'step', 'min', 'max', 'placeholder'));
element_set_attributes($element, array('id', 'name', 'value', 'step', 'min', 'max', 'placeholder', 'size'));
_form_set_attributes($element, array('form-number'));
return $element;
......
......@@ -57,38 +57,39 @@ public function buildForm(array $form, array &$form_state, FieldInstance $field_
// Build the configurable field values.
$cardinality = $field['cardinality'];
$form['field']['container'] = array(
$form['field']['cardinality_container'] = array(
// We can't use the container element because it doesn't support the title
// or description properties.
'#type' => 'item',
// Reset #parents to 'field', so the additional container does not appear.
'#parents' => array('field'),
'#field_prefix' => '<div class="container-inline">',
'#field_suffix' => '</div>',
'#title' => t('Maximum number of values users can enter'),
'#title' => t('Allowed number of values'),
);
$form['field']['container']['cardinality'] = array(
$form['field']['cardinality_container']['cardinality'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(range(1, 5)) + array(FIELD_CARDINALITY_UNLIMITED => t('Unlimited')) + array('other' => t('More')),
'#default_value' => ($cardinality < 6) ? $cardinality : 'other',
'#title' => t('Allowed number of values'),
'#title_display' => 'invisible',
'#options' => array(
'number' => t('Limited'),
FIELD_CARDINALITY_UNLIMITED => t('Unlimited'),
),
'#default_value' => ($cardinality == FIELD_CARDINALITY_UNLIMITED) ? FIELD_CARDINALITY_UNLIMITED : 'number',
);
// @todo Convert when http://drupal.org/node/1207060 gets in.
$form['field']['container']['cardinality_other'] = array(
$form['field']['cardinality_container']['cardinality_number'] = array(
'#type' => 'number',
'#default_value' => $cardinality > 5 ? $cardinality : 6,
'#default_value' => $cardinality != FIELD_CARDINALITY_UNLIMITED ? $cardinality : 1,
'#min' => 1,
'#title' => t('Custom value'),
'#title' => t('Limit'),
'#title_display' => 'invisible',
'#size' => 2,
'#states' => array(
'visible' => array(
':input[name="field[container][cardinality]"]' => array('value' => 'other'),
':input[name="field[cardinality]"]' => array('value' => 'number'),
),
),
);
if (field_behaviors_widget('multiple values', $this->instance) == FIELD_BEHAVIOR_DEFAULT) {
$form['field']['container']['#description'] = t('%unlimited will provide an %add-more button so users can add as many values as they like.', array(
'%unlimited' => t('Unlimited'),
'%add-more' => t('Add another item'),
));
}
// Build the non-configurable field values.
$form['field']['field_name'] = array('#type' => 'value', '#value' => $field['field_name']);
......@@ -117,10 +118,10 @@ public function buildForm(array $form, array &$form_state, FieldInstance $field_
*/
public function validateForm(array &$form, array &$form_state) {
// Validate field cardinality.
$cardinality = $form_state['values']['field']['container']['cardinality'];
$cardinality_other = $form_state['values']['field']['container']['cardinality_other'];
if ($cardinality == 'other' && empty($cardinality_other)) {
form_error($form['field']['container']['cardinality_other'], t('Number of values is required.'));
$cardinality = $form_state['values']['field']['cardinality'];
$cardinality_number = $form_state['values']['field']['cardinality_number'];
if ($cardinality === 'number' && empty($cardinality_number)) {
form_error($form['field']['cardinality_container']['cardinality_number'], t('Number of values is required.'));
}
}
......@@ -133,11 +134,10 @@ public function submitForm(array &$form, array &$form_state) {
$field_values = $form_values['field'];
// Save field cardinality.
$cardinality = $field_values['container']['cardinality'];
$cardinality_other = $field_values['container']['cardinality_other'];
$cardinality_other = $form_state['values']['field']['container']['cardinality_other'];
if ($cardinality == 'other') {
$cardinality = $cardinality_other;
$cardinality = $field_values['cardinality'];
$cardinality_number = $field_values['cardinality_number'];
if ($cardinality === 'number') {
$cardinality = $cardinality_number;
}
$field_values['cardinality'] = $cardinality;
unset($field_values['container']);
......
......@@ -180,37 +180,34 @@ function cardinalitySettings() {
$field_edit_path = 'admin/structure/types/manage/article/fields/node.article.body/field';
// Assert the cardinality other field cannot be empty when cardinality is
// set to other.
// set to 'number'.
$edit = array(
'field[container][cardinality]' => 'other',
'field[container][cardinality_other]' => '',
'field[cardinality]' => 'number',
'field[cardinality_number]' => '',
);
$this->drupalPost($field_edit_path, $edit, t('Save field settings'));
$this->assertText('Number of values is required.');
// Assert the cardinality field is set to 'Other' when the value is greater
// than 5.
// Submit a custom number.
$edit = array(
'field[container][cardinality]' => 'other',
'field[container][cardinality_other]' => 16,
'field[cardinality]' => 'number',
'field[cardinality_number]' => 6,
);
$this->drupalPost($field_edit_path, $edit, t('Save field settings'));
$this->assertText('Updated field Body field settings.');
$this->drupalGet($field_edit_path);
$this->assertFieldByXPath("//select[@name='field[container][cardinality]']", 'other');
$this->assertFieldByXPath("//input[@name='field[container][cardinality_other]']", 16);
$this->assertFieldByXPath("//select[@name='field[cardinality]']", 'number');
$this->assertFieldByXPath("//input[@name='field[cardinality_number]']", 6);
// Assert the cardinality other field is set back to 6 after changing the
// cardinality to less than 6.
// Set to unlimited.
$edit = array(
'field[container][cardinality]' => 3,
'field[container][cardinality_other]' => 16,
'field[cardinality]' => FIELD_CARDINALITY_UNLIMITED,
);
$this->drupalPost($field_edit_path, $edit, t('Save field settings'));
$this->assertText('Updated field Body field settings.');
$this->drupalGet($field_edit_path);
$this->assertFieldByXPath("//select[@name='field[container][cardinality]']", 3);
$this->assertFieldByXPath("//input[@name='field[container][cardinality_other]']", 6);
$this->assertFieldByXPath("//select[@name='field[cardinality]']", FIELD_CARDINALITY_UNLIMITED);
$this->assertFieldByXPath("//input[@name='field[cardinality_number]']", 1);
}
/**
......
......@@ -91,7 +91,7 @@ function testTextField() {
*/
function testTextFieldFormatted() {
// Make node body multiple.
$edit = array('field[container][cardinality]' => -1);
$edit = array('field[cardinality]' => FIELD_CARDINALITY_UNLIMITED);
$this->drupalPost('admin/structure/types/manage/article/fields/node.article.body/field', $edit, t('Save field settings'));
$this->drupalGet('node/add/article');
$this->assertFieldByXPath("//input[@name='body_add_more']", t('Add another item'), 'Body field cardinality set to multiple.');
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment