summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Davison2013-08-16 09:57:24 (GMT)
committer Alan Davison2013-08-16 09:57:24 (GMT)
commit81f94b50be6af80d02656896a66692b34f4adf3e (patch)
treec48bb1ddcfb8cae3ae3b5db9b3f43d3fe4b3ad98
parent2acf98e360ded1417f596fbcf59bbca65058b581 (diff)
Issue 2047643 by RoSk0, Alan D., MJCO & techsoldaten: Support for instance overrides for field settings; label, components and minimum components.
-rw-r--r--includes/name.content.inc57
-rw-r--r--name.admin.inc48
-rw-r--r--name.devel_generate.inc9
-rw-r--r--name.module29
-rw-r--r--tests/name.test6
5 files changed, 134 insertions, 15 deletions
diff --git a/includes/name.content.inc b/includes/name.content.inc
index 8c0a775..dcee1ed 100644
--- a/includes/name.content.inc
+++ b/includes/name.content.inc
@@ -172,9 +172,28 @@ function _name_field_settings_form_validate($elements, &$form_state, $form) {
_name_options_validate($values['generational_options'], $values['max_length']['generational'],
t('Generational options'), 'field][settings][generational_options');
- // Validates that a minimum component is checked when that component is not used.
+ // Validates that a minimum component is not selected when that component is
+ // not selected.
_name_field_components_validate($values['components'],
$values['minimum_components'], 'field][settings][minimum_components');
+
+ if (!empty($form_state['values']['instance'])) {
+ $instance_settings = $form_state['values']['instance']['settings'];
+ $instance_components = array_filter($instance_settings['components']);
+ $instance_minimum_components = array_filter($instance_settings['minimum_components']);
+ if ($instance_components && $instance_minimum_components) {
+ _name_field_components_validate($instance_components,
+ $instance_minimum_components, 'instance[settings][minimum_components');
+ }
+ elseif ($instance_minimum_components) {
+ _name_field_components_validate($values['components'],
+ $instance_minimum_components, 'instance[settings][minimum_components');
+ }
+ elseif ($instance_components) {
+ _name_field_components_validate($instance_components,
+ $values['minimum_components'], 'instance[settings][components');
+ }
+ }
}
// TODO - hook_field_views_data() if required
@@ -304,8 +323,28 @@ function _name_field_instance_settings_form($field, $instance = NULL) {
'#default_value' => isset($settings['inline_css']) ? $settings['inline_css'][$key] : '',
'#size' => 8,
);
+ $form['labels'][$key] = array(
+ '#type' => 'textfield',
+ '#title' => t('Label for !title', array('!title' => $title)),
+ '#default_value' => isset($settings['labels']) ? $settings['labels'][$key] : '',
+ '#size' => 12,
+ );
}
+ $form['components'] = array(
+ '#type' => 'checkboxes',
+ '#title' => t('Components overrides'),
+ '#default_value' => $settings['components'],
+ '#options' => $components,
+ '#element_validate' => array('_name_field_minimal_component_requirements'),
+ );
+ $form['minimum_components'] = array(
+ '#type' => 'checkboxes',
+ '#title' => t('Minimum components overrides'),
+ '#default_value' => $settings['minimum_components'],
+ '#element_validate' => array('_name_field_minimal_component_requirements'),
+ '#options' => $components,
+ );
$form['component_css'] = array(
'#type' => 'textfield',
'#title' => t('Component separator CSS'),
@@ -381,7 +420,18 @@ function _name_field_widget_form(&$form, &$form_state, $field, $instance, $langc
_name_defaults($field['settings'], 'settings');
$fs = $field['settings'];
$ws = $instance['settings'];
+ $ws += array(
+ 'components' => array(),
+ 'minimum_components' => array(),
+ );
+ // Use populated overrides if present.
+ if (array_filter($ws['components'])) {
+ $fs['components'] = $ws['components'];
+ }
+ if (array_filter($ws['minimum_components'])) {
+ $fs['minimum_components'] = $ws['minimum_components'];
+ }
$element += array(
'#type' => 'name_element',
'#title' => check_plain($instance['label']),
@@ -405,7 +455,8 @@ function _name_field_widget_form(&$form, &$form_state, $field, $instance, $langc
$size = !empty($ws['size'][$key]) ? $ws['size'][$key] : 60;
$title_display = isset($ws['title_display'][$key]) ? $ws['title_display'][$key] : 'description';
- $element['#components'][$key]['title'] = check_plain($fs['labels'][$key]);
+ $component_label = empty($ws['labels'][$key]) ? $fs['labels'][$key] : $ws['labels'][$key];
+ $element['#components'][$key]['title'] = check_plain($component_label);
$element['#components'][$key]['title_display'] = $title_display;
$element['#components'][$key]['size'] = $size;
@@ -540,4 +591,4 @@ function _name_get_vocabulary_id_by_code_or_number($value) {
$vid = $vocab->vid;
}
return $vid;
-} \ No newline at end of file
+}
diff --git a/name.admin.inc b/name.admin.inc
index 0d874bf..2cffccb 100644
--- a/name.admin.inc
+++ b/name.admin.inc
@@ -555,6 +555,25 @@ function _name_field_instance_settings_pre_render($form) {
'#suffix' => '</tr>',
'#weight' => 4,
),
+ 'field_override_label' => array(
+ '#markup' => '<tr><td colspan="7"><strong><em>' . t('Field setting overrides') . '</em></strong></td>',
+ '#weight' => 5,
+ ),
+ 'components' => array(
+ '#prefix' => '<tr><td><strong>' . t('Components') . '<sup>5</sup></strong></td>',
+ '#suffix' => '</tr>',
+ '#weight' => 6,
+ ),
+ 'minimum_components' => array(
+ '#prefix' => '<tr><td><strong>' . t('Minimum components') . '<sup>6</sup></strong></td>',
+ '#suffix' => '</tr>',
+ '#weight' => 7,
+ ),
+ 'labels' => array(
+ '#prefix' => '<tr><td><strong>' . t('Labels') . '<sup>7</sup></strong></td>',
+ '#suffix' => '</tr>',
+ '#weight' => 8,
+ ),
),
'tfoot' => array(
'#markup' => '<tfoot><tr><td colspan="7"><ol>'
@@ -563,6 +582,9 @@ function _name_field_instance_settings_pre_render($form) {
. '<li>' . t('The Field type controls how the field is rendered. Autocomplete is a text field with autocomplete, and the behaviour of this is controlled by the field settings.') . '</li>'
. '<li>' . t('The HTML size property tells the browser what the width of the field should be when it is rendered. This gets overriden by the themes CSS properties. This must be between 1 and 255.') . '</li>'
. '<li>' . t('Additional inline styles for the input element. For example, "width: 45px; background-color: #f3f3f3"') . '</li>'
+ . '<li>' . t('Optional overrides for <em>Components</em> used. Leave all unchecked to use the field settings.') . '</li>'
+ . '<li>' . t('Optional overrides for <em>Minimum components</em> used. Leave all unchecked to use the field settings.') . '</li>'
+ . '<li>' . t('Optional overrides for field labels.') . '</li>'
. '</ol></td></tr></tfoot>',
'#weight' => 2,
),
@@ -591,6 +613,9 @@ function _name_field_instance_settings_pre_render($form) {
unset($form['instance_inline_css'][$key]['#description']);
unset($form['instance_inline_css'][$key]['#title']);
+ unset($form['instance_labels'][$key]['#description']);
+ unset($form['instance_labels'][$key]['#title']);
+
// Moves the size element into the table.
$form['styled_settings']['tbody']['size'][$key] = $form['instance_size'][$key];
$form['styled_settings']['tbody']['size'][$key]['#prefix'] = '<td>';
@@ -612,6 +637,21 @@ function _name_field_instance_settings_pre_render($form) {
$form['styled_settings']['tbody']['inline_css'][$key]['#suffix'] = '</td>';
$form['styled_settings']['tbody']['inline_css'][$key]['#weight'] = $i;
+ $form['styled_settings']['tbody']['labels'][$key] = $form['instance_labels'][$key];
+ $form['styled_settings']['tbody']['labels'][$key]['#prefix'] = '<td>';
+ $form['styled_settings']['tbody']['labels'][$key]['#suffix'] = '</td>';
+ $form['styled_settings']['tbody']['labels'][$key]['#weight'] = $i;
+
+ $form['styled_settings']['tbody']['components'][$key] = $form['instance_components'][$key];
+ $form['styled_settings']['tbody']['components'][$key]['#prefix'] = '<td>';
+ $form['styled_settings']['tbody']['components'][$key]['#suffix'] = '</td>';
+ $form['styled_settings']['tbody']['components'][$key]['#weight'] = $i;
+
+ $form['styled_settings']['tbody']['minimum_components'][$key] = $form['instance_minimum_components'][$key];
+ $form['styled_settings']['tbody']['minimum_components'][$key]['#prefix'] = '<td>';
+ $form['styled_settings']['tbody']['minimum_components'][$key]['#suffix'] = '</td>';
+ $form['styled_settings']['tbody']['minimum_components'][$key]['#weight'] = $i;
+
// Clean up the leftovers.
unset($form['instance_size'][$key]);
$form['instance_size']['#access'] = FALSE;
@@ -625,6 +665,14 @@ function _name_field_instance_settings_pre_render($form) {
unset($form['instance_inline_css'][$key]);
$form['instance_inline_css']['#access'] = FALSE;
+ unset($form['instance_labels'][$key]);
+ $form['instance_labels']['#access'] = FALSE;
+
+ unset($form['instance_components'][$key]);
+ $form['instance_components']['#access'] = FALSE;
+
+ unset($form['instance_minimum_components'][$key]);
+ $form['instance_minimum_components']['#access'] = FALSE;
}
return $form;
diff --git a/name.devel_generate.inc b/name.devel_generate.inc
index 28286a6..ad8dbe8 100644
--- a/name.devel_generate.inc
+++ b/name.devel_generate.inc
@@ -28,8 +28,13 @@ function _name_devel_generate($object, $field, $instance, $bundle) {
$func = variable_get('name_devel_generate_names', '_name_devel_generate_name');
$names = $func($field);
-
- $components = array_filter($field['settings']['components']);
+ $instance['setting'] += array(
+ 'components' => array(),
+ );
+ $components = array_filter($instance['setting']['components']);
+ if (!$components) {
+ $components = array_filter($field['settings']['components']);
+ }
return array_intersect_key($names[array_rand($names)], $components);
}
diff --git a/name.module b/name.module
index bb43e2a..7a14b98 100644
--- a/name.module
+++ b/name.module
@@ -911,6 +911,10 @@ function name_field_info() {
),
'inline_css' => array_combine($keys, array_fill(0, $count, '')),
'title_display' => array_combine($keys, array_fill(0, $count, 'description')),
+ // Field instance overrides.
+ 'components' => array(),
+ 'minimum_components' => array(),
+ 'labels' => array_combine($keys, array_fill(0, $count, '')),
// New settings to override the user name. Only applies to user entities.
'override_format' => 'default',
// Prevents the default break between the name components and credentials
@@ -977,9 +981,15 @@ function name_field_settings_form_validate($form, &$form_state) {
*/
function name_field_load($entity_type, $entities, $field, $instances, $langcode, &$items) {
foreach ($entities as $id => $entity) {
+ // Use populated overrides if present.
+ $instances[$id]['settings'] += array('components' => array());
+ $components = array_filter($instances[$id]['settings']['components']);
+ if (!$components) {
+ $components = $field['settings']['components'];
+ }
foreach ($items[$id] as $delta => $item) {
foreach (_name_translations() as $key => $title) {
- if (empty($field['settings']['components'][$key])) {
+ if (empty($components[$key])) {
unset($items[$id][$delta][$key]);
}
else {
@@ -1011,13 +1021,16 @@ function name_field_is_empty($item, $field) {
* component is not selected.
*/
function _name_field_minimal_component_requirements($element, &$form_state) {
- $required_field_set = array_flip(array('given', 'family'));
- $value = array_intersect_key($required_field_set, array_filter((array) $element['#value']));
- if (empty($value)) {
- $required_fields = array_intersect_key(_name_translations(), $required_field_set);
- form_set_error(implode('][', $element['#parents']) . '][given]',
- t('!label must have one of the following components: %components',
- array('!label' => $element['#title'], '%components' => implode(', ', $required_fields))));
+ $values = array_filter((array) $element['#value']);
+ if ($values) {
+ $required_field_set = array_flip(array('given', 'family'));
+ $values = array_intersect_key($required_field_set, $values);
+ if (empty($values)) {
+ $required_fields = array_intersect_key(_name_translations(), $required_field_set);
+ form_set_error(implode('][', $element['#parents']) . '][given]',
+ t('!label must have one of the following components: %components',
+ array('!label' => $element['#title'], '%components' => implode(', ', $required_fields))));
+ }
}
}
diff --git a/tests/name.test b/tests/name.test
index e78a47a..0c2bd20 100644
--- a/tests/name.test
+++ b/tests/name.test
@@ -514,10 +514,12 @@ class Name_FieldTestCase extends NameTestHelper {
t('!field options field is required.', array('!field' => $n['title'])),
t('!field options field is required.', array('!field' => $n['generational'])),
+ // There is now empty tests on both of these fields, so the second
+ // message will not show if the required message is shown.
t('!field field is required.', array('!field' => t('Components'))),
- t('!field must have one of the following components: !components', array('!field' => t('Components'), '!components' => check_plain(implode(', ', array($n['given'], $n['family']))))),
t('!field field is required.', array('!field' => t('Minimum components'))),
- t('!field must have one of the following components: !components', array('!field' => t('Minimum components'), '!components' => check_plain(implode(', ', array($n['given'], $n['family']))))),
+ // t('!field must have one of the following components: !components', array('!field' => t('Components'), '!components' => check_plain(implode(', ', array($n['given'], $n['family']))))),
+ // t('!field must have one of the following components: !components', array('!field' => t('Minimum components'), '!components' => check_plain(implode(', ', array($n['given'], $n['family']))))),
);
foreach ($required_messages as $message) {
$this->assertText($message);