summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaren Stevenson2008-09-27 14:08:09 (GMT)
committer Karen Stevenson2008-09-27 14:08:09 (GMT)
commit3924cd86659c71bd76f3242c40f3b24de6e47084 (patch)
tree7dd891260fbdf10e0e359a0ff5932c813317d15d
parentd275141d6244d531e2fdfe24b42254079608fd34 (diff)
#119102 Combo field prep, rework fieldgroup name validation into API to be used by other modules.
-rw-r--r--CHANGELOG.txt1
-rw-r--r--modules/fieldgroup/fieldgroup.module105
-rw-r--r--theme/content-admin-field-overview-form.tpl.php2
3 files changed, 70 insertions, 38 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index c889eb3..d3a210d 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -5,6 +5,7 @@ CCK 6.2-dev
Be sure to visit update.php after uploading this release.
+- #119102 Combo field prep, rework fieldgroup name validation into API to be used by other modules.
- #312546 by stella - Change some links to make translation easier.
- Added the #delta value to the wrong place in the element, it was inaccessible to the formatter theme.
- #119102 Combo field prep, rework the field overview form so it can hanle other kinds of groups.
diff --git a/modules/fieldgroup/fieldgroup.module b/modules/fieldgroup/fieldgroup.module
index 78985bc..8a653f1 100644
--- a/modules/fieldgroup/fieldgroup.module
+++ b/modules/fieldgroup/fieldgroup.module
@@ -9,6 +9,7 @@
* - hook_fieldgroup_view: Alter the group $element added to $node->content.
* - hook_fieldgroup_form: Alter the group portion of the node form.
* - hook_fieldgroup_types: Add additional fieldgroup group_types.
+ * - hook_fieldgroup_default_settings: Add additional fieldgroup default settings.
* - hook_fieldgroup_save: Do additional processing when a fieldgroup is saved.
*/
/**
@@ -333,51 +334,79 @@ function fieldgroup_form_alter(&$form, $form_state, $form_id) {
}
}
-function fieldgroup_field_overview_form_validate($form, &$form_state) {
- $form_values = $form_state['values'];
- $group = $form_values['_add_new_group'];
+/**
+ * API for group name validation.
+ *
+ * Pulled into separate function to be re-usable.
+ */
+function fieldgroup_validate_name($group) {
+ $errors = array();
+
+ // No label.
+ if (!$group['label']) {
+ $errors['label'][] = t('You need to provide a label.');
+ }
- if (array_filter(array($group['label'], $group['group_name']))) {
- // No label.
- if (!$group['label']) {
- form_set_error('_add_new_group][label', t('Add new group: you need to provide a label.'));
+ // No group name.
+ if (!$group['group_name']) {
+ $errors['group_name'][] = t('You need to provide a group name.');
+ }
+ // Group name validation.
+ else {
+ $group_name = $group['group_name'];
+ $group['group_type'] = !empty($group['group_type']) ? $group['group_type'] : 'standard';
+
+ // Add the 'group_' prefix.
+ if (substr($group_name, 0, 6) != 'group_') {
+ $group_name = 'group_'. $group_name;
}
- // No group name.
- if (!$group['group_name']) {
- form_set_error('_add_new_group][group_name', t('Add new group: you need to provide a group name.'));
+ // Invalid field name.
+ if (!preg_match('!^group_[a-z0-9_]+$!', $group_name)) {
+ $errors['group_name'][] = t('The group name %group_name is invalid. The name must include only lowercase unaccentuated letters, numbers, and underscores.', array('%group_name' => $group_name));
+ }
+ if (strlen($group_name) > 32) {
+ $errors['group_name'][] = t('The group name %group_name is too long. The name is limited to 32 characters, including the \'group_\' prefix.', array('%group_name' => $group_name));
}
- // Group name validation.
- else {
- $group_name = $group['group_name'];
- $group['group_type'] = !empty($group['group_type']) ? $group['group_type'] : 'standard';
-
- // Add the 'group_' prefix.
- if (substr($group_name, 0, 6) != 'group_') {
- $group_name = 'group_'. $group_name;
- form_set_value($form['_add_new_group']['group_name'], $group_name, $form_state);
- }
- // Invalid field name.
- if (!preg_match('!^group_[a-z0-9_]+$!', $group_name)) {
- form_set_error('_add_new_group][group_name', t('Add new group: the group name %group_name is invalid. The name must include only lowercase unaccentuated letters, numbers, and underscores.', array('%group_name' => $group_name)));
- }
- if (strlen($group_name) > 32) {
- form_set_error('_add_new_group][group_name', t('Add new group: the group name %group_name is too long. The name is limited to 32 characters, including the \'group_\' prefix.', array('%group_name' => $group_name)));
+ // Group name already exists.
+ $used = FALSE;
+ $groups = fieldgroup_groups();
+ foreach ($groups as $type => $groups_per_type) {
+ foreach ($groups_per_type as $existing_group) {
+ $used |= ($existing_group['group_name'] == $group_name);
}
+ }
+ if ($used) {
+ $errors['group_name'][] = t('The group name %group_name already exists.', array('%group_name' => $group_name));
+ }
+ if (empty($errors['group_name'])) {
+ $group['group_name'] = $group_name;
+ }
+ }
+ return array('group_name' => $group['group_name'], 'errors' => $errors);
+}
- // Group name already exists.
- $used = FALSE;
- $groups = fieldgroup_groups();
- foreach ($groups as $type => $groups_per_type) {
- foreach ($groups_per_type as $existing_group) {
- $used |= ($existing_group['group_name'] == $group_name);
+function fieldgroup_field_overview_form_validate($form, &$form_state) {
+ $form_values = $form_state['values'];
+ $group = $form_values['_add_new_group'];
+
+ if (array_filter(array($group['label'], $group['group_name']))) {
+ $validation = fieldgroup_validate_name($group);
+ if (!empty($validation['errors'])) {
+ foreach ($validation['errors'] as $type => $messages) {
+ foreach ($messages as $message) {
+ if ($type == 'label') {
+ form_set_error('_add_new_group][label', t('Add new group:') .' '. $message);
+ }
+ else {
+ form_set_error('_add_new_group][group_name', t('Add new group:') .' '. $message);
+ }
}
}
- if ($used) {
- form_set_error('_add_new_group][group_name', t('Add new group: the group name %group_name already exists.', array('%group_name' => $group_name)));
- }
}
+ $group_name = $validation['group_name'];
+ form_set_value($form['_add_new_group']['group_name'], $group_name, $form_state);
}
else {
// Fail validation if attempt to nest fields under a new group without the
@@ -401,7 +430,7 @@ function fieldgroup_field_overview_form_submit($form, &$form_state) {
// Create new group if needed.
if (!empty($form_values['_add_new_group']['label'])) {
$group = $form_values['_add_new_group'];
- $group['settings'] = _field_group_default_settings();
+ $group['settings'] = field_group_default_settings($group['group_type']);
fieldgroup_save_group($type_name, $group);
$new_group_name = $group['group_name'];
}
@@ -444,7 +473,7 @@ function fieldgroup_field_overview_form_submit($form, &$form_state) {
cache_clear_all('fieldgroup_data', content_cache_tablename());
}
-function _field_group_default_settings() {
+function field_group_default_settings($group_type) {
$settings = array(
'form' => array('style' => 'fieldset', 'description' => ''),
'display' => array('description' => '', 'label' => 'above'),
@@ -453,6 +482,8 @@ function _field_group_default_settings() {
foreach (array_keys(content_build_modes()) as $key) {
$settings['display'][$key]['format'] = 'fieldset';
}
+ // Allow other modules to add new default settings.
+ $settings = array_merge($settings, module_invoke_all('fieldgroup_default_settings', $group_type));
return $settings;
}
diff --git a/theme/content-admin-field-overview-form.tpl.php b/theme/content-admin-field-overview-form.tpl.php
index c0f8df9..d1edbe7 100644
--- a/theme/content-admin-field-overview-form.tpl.php
+++ b/theme/content-admin-field-overview-form.tpl.php
@@ -87,8 +87,8 @@
</td>
<td><div class="content-new">&nbsp;</div><?php print $row->weight . $row->parent . $row->hidden_name; ?></td>
<td><div class="content-new">&nbsp;</div><?php print $row->group_name; ?></td>
- <td><div class="content-new">&nbsp;</div><?php print $row->group_option; ?></td>
<td><div class="content-new">&nbsp;</div><?php print $row->group_type; ?></td>
+ <td><div class="content-new">&nbsp;</div><?php print $row->group_option; ?></td>
<?php break;
endswitch; ?>
</tr>