summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaren Stevenson2007-01-25 13:26:21 (GMT)
committer Karen Stevenson2007-01-25 13:26:21 (GMT)
commitf3fdae4af1a6a63612c5238b800c2fa10c344fb7 (patch)
tree7f4df6e22f0925cd6fb70204e9206c89c228c070
parent2c2da16ff93b9571570e968d042b34bc6428a7ed (diff)
#112464 standardize validation in field rather than widget, standardize use of $items rather than $node_field
-rw-r--r--nodereference.module103
-rw-r--r--optionwidgets.module28
-rw-r--r--text.module19
-rw-r--r--userreference.module71
4 files changed, 108 insertions, 113 deletions
diff --git a/nodereference.module b/nodereference.module
index da27884..2bd7ba3 100644
--- a/nodereference.module
+++ b/nodereference.module
@@ -104,10 +104,36 @@ function nodereference_field_settings($op, $field) {
/**
* Implementation of hook_field().
*/
-//function nodereference_field($op, &$node, $field, &$items, $teaser, $page) {
-// switch ($op) {
-// }
-//}
+function nodereference_field($op, &$node, $field, &$items, $teaser, $page) {
+ switch ($op) {
+ case 'validate':
+ foreach ($items as $delta => $item) {
+ $error_field = $field['field_name'].']['.$delta.'][node_name';
+ if (!empty($item['node_name'])) {
+ preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $item['node_name'], $matches);
+ if (!empty($matches)) {
+ // explicit nid
+ list(, $title, $nid) = $matches;
+ $refs = _nodereference_potential_references($field, TRUE);
+ if (!in_array($nid, array_keys($refs))) {
+ form_set_error($error_field, t('This post can\'t be referenced.'));
+ }
+ elseif (!empty($title) && ($n = $refs[$nid]) && $title != $n->node_title) {
+ form_set_error($error_field, t('Title mismatch. Please reiterate your selection.'));
+ }
+ }
+ else {
+ // no explicit nid
+ $refs = _nodereference_potential_references($field, FALSE, $item['node_name'], TRUE);
+ if (empty($refs)) {
+ form_set_error($error_field, t('No post with that title can be referenced.'));
+ }
+ }
+ }
+ }
+ return;
+ }
+}
/**
* Implementation of hook_field_formatter_info().
@@ -165,12 +191,12 @@ function nodereference_widget_info() {
/**
* Implementation of hook_widget().
*/
-function nodereference_widget($op, &$node, $field, &$node_field) {
+function nodereference_widget($op, &$node, $field, &$items) {
if ($field['widget']['type'] == 'nodereference_select') {
switch ($op) {
case 'prepare form values':
- $node_field_transposed = content_transpose_array_rows_cols($node_field);
- $node_field['default nids'] = $node_field_transposed['nid'];
+ $items_transposed = content_transpose_array_rows_cols($items);
+ $items['default nids'] = $items_transposed['nid'];
break;
case 'form':
@@ -188,7 +214,7 @@ function nodereference_widget($op, &$node, $field, &$node_field) {
$form[$field['field_name']]['nids'] = array(
'#type' => 'select',
'#title' => t($field['widget']['label']),
- '#default_value' => $node_field['default nids'],
+ '#default_value' => $items['default nids'],
'#multiple' => $field['multiple'],
'#size' => min(count($options), 6),
'#options' => $options,
@@ -201,30 +227,30 @@ function nodereference_widget($op, &$node, $field, &$node_field) {
case 'process form values':
if ($field['multiple']) {
// if nothing selected, make it 'none'
- if (empty($node_field['nids'])) {
- $node_field['nids'] = array(0 => '0');
+ if (empty($items['nids'])) {
+ $items['nids'] = array(0 => '0');
}
// drop the 'none' options if other items were also selected
- elseif (count($node_field['nids']) > 1) {
- unset($node_field['nids'][0]);
+ elseif (count($items['nids']) > 1) {
+ unset($items['nids'][0]);
}
- $node_field = content_transpose_array_rows_cols(array('nid' => $node_field['nids']));
+ $items = content_transpose_array_rows_cols(array('nid' => $items['nids']));
}
else {
- $node_field[0]['nid'] = $node_field['nids'];
+ $items[0]['nid'] = $items['nids'];
}
// Remove the widget's data representation so it isn't saved.
- unset($node_field['nids']);
+ unset($items['nids']);
}
}
else {
switch ($op) {
case 'prepare form values':
- foreach ($node_field as $delta => $item) {
- if (!empty($node_field[$delta]['nid'])) {
- $node_field[$delta]['default node_name'] = db_result(db_query(db_rewrite_sql('SELECT n.title FROM {node} n WHERE n.nid = %d'), $node_field[$delta]['nid']));
- $node_field[$delta]['default node_name'] .= ' [nid:'. $node_field[$delta]['nid'] .']';
+ foreach ($items as $delta => $item) {
+ if (!empty($items[$delta]['nid'])) {
+ $items[$delta]['default node_name'] = db_result(db_query(db_rewrite_sql('SELECT n.title FROM {node} n WHERE n.nid = %d'), $items[$delta]['nid']));
+ $items[$delta]['default node_name'] .= ' [nid:'. $items[$delta]['nid'] .']';
}
}
break;
@@ -237,7 +263,7 @@ function nodereference_widget($op, &$node, $field, &$node_field) {
$form[$field['field_name']]['#type'] = 'fieldset';
$form[$field['field_name']]['#description'] = $field['widget']['description'];
$delta = 0;
- foreach ($node_field as $item) {
+ foreach ($items as $item) {
if ($item['nid']) {
$form[$field['field_name']][$delta]['node_name'] = array(
'#type' => 'textfield',
@@ -264,42 +290,15 @@ function nodereference_widget($op, &$node, $field, &$node_field) {
'#type' => 'textfield',
'#title' => t($field['widget']['label']),
'#autocomplete_path' => 'nodereference/autocomplete/'. $field['field_name'],
- '#default_value' => $node_field[0]['default node_name'],
+ '#default_value' => $items[0]['default node_name'],
'#required' => $field['required'],
'#description' => $field['widget']['description'],
);
}
return $form;
- case 'validate':
- foreach ($node_field as $delta => $item) {
- $error_field = $field['field_name'].']['.$delta.'][node_name';
- if (!empty($item['node_name'])) {
- preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $item['node_name'], $matches);
- if (!empty($matches)) {
- // explicit nid
- list(, $title, $nid) = $matches;
- $refs = _nodereference_potential_references($field, TRUE);
- if (!in_array($nid, array_keys($refs))) {
- form_set_error($error_field, t('This post can\'t be referenced.'));
- }
- elseif (!empty($title) && ($n = $refs[$nid]) && $title != $n->node_title) {
- form_set_error($error_field, t('Title mismatch. Please reiterate your selection.'));
- }
- }
- else {
- // no explicit nid
- $refs = _nodereference_potential_references($field, FALSE, $item['node_name'], TRUE);
- if (empty($refs)) {
- form_set_error($error_field, t('No post with that title can be referenced.'));
- }
- }
- }
- }
- return;
-
case 'process form values':
- foreach ($node_field as $delta => $item) {
+ foreach ($items as $delta => $item) {
$nid = 0;
if (!empty($item['node_name'])) {
preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $item['node_name'], $matches);
@@ -318,13 +317,13 @@ function nodereference_widget($op, &$node, $field, &$node_field) {
}
}
if (!empty($nid)) {
- $node_field[$delta]['nid'] = $nid;
+ $items[$delta]['nid'] = $nid;
// Remove the widget's data representation so it isn't saved.
- unset($node_field[$delta]['node_name']);
+ unset($items[$delta]['node_name']);
}
else {
// Don't save empty fields when they're not the first value (keep '0' otherwise)
- if ($delta > 0) {unset($node_field[$delta]);}
+ if ($delta > 0) {unset($items[$delta]);}
}
}
break;
diff --git a/optionwidgets.module b/optionwidgets.module
index 262c3f0..4f6603b 100644
--- a/optionwidgets.module
+++ b/optionwidgets.module
@@ -49,12 +49,12 @@ function optionwidgets_widget_settings($op, $widget) {
/**
* Implementation of hook_widget().
*/
-function optionwidgets_widget($op, &$node, $field, &$node_field) {
+function optionwidgets_widget($op, &$node, $field, &$items) {
switch ($op) {
case 'prepare form values':
$options = _optionwidgets_options($field);
- $node_field_transposed = content_transpose_array_rows_cols($node_field);
- $values = (isset($node_field_transposed['value']) && is_array($node_field_transposed['value'])) ? $node_field_transposed['value'] : array();
+ $items_transposed = content_transpose_array_rows_cols($items);
+ $values = (isset($items_transposed['value']) && is_array($items_transposed['value'])) ? $items_transposed['value'] : array();
$keys = array();
foreach ($values as $value) {
@@ -64,10 +64,10 @@ function optionwidgets_widget($op, &$node, $field, &$node_field) {
}
}
if ($field['multiple']) {
- $node_field['default keys'] = $keys;
+ $items['default keys'] = $keys;
}
else {
- $node_field['default key'] = reset($keys);
+ $items['default key'] = reset($keys);
}
break;
@@ -85,7 +85,7 @@ function optionwidgets_widget($op, &$node, $field, &$node_field) {
$form[$field['field_name']]['keys'] = array(
'#type' => 'select',
'#title' => t($field['widget']['label']),
- '#default_value' => $node_field['default keys'],
+ '#default_value' => $items['default keys'],
'#multiple' => TRUE,
'#size' => min(count($options), 6),
'#options' => $options,
@@ -97,7 +97,7 @@ function optionwidgets_widget($op, &$node, $field, &$node_field) {
$form[$field['field_name']]['key'] = array(
'#type' => 'select',
'#title' => t($field['widget']['label']),
- '#default_value' => $node_field['default key'],
+ '#default_value' => $items['default key'],
'#multiple' => FALSE,
'#options' => $options,
'#required' => $field['required'],
@@ -111,7 +111,7 @@ function optionwidgets_widget($op, &$node, $field, &$node_field) {
$form[$field['field_name']]['keys'] = array(
'#type' => 'checkboxes',
'#title' => t($field['widget']['label']),
- '#default_value' => $node_field['default keys'],
+ '#default_value' => $items['default keys'],
'#options' => $options,
'#required' => $field['required'],
'#description' => $field['widget']['description'],
@@ -122,7 +122,7 @@ function optionwidgets_widget($op, &$node, $field, &$node_field) {
$form[$field['field_name']]['key'] = array(
'#type' => 'radios',
'#title' => t($field['widget']['label']),
- '#default_value' => $node_field['default key'],
+ '#default_value' => $items['default key'],
'#options' => $options,
'#required' => $field['required'],
'#description' => $field['widget']['description'],
@@ -137,10 +137,10 @@ function optionwidgets_widget($op, &$node, $field, &$node_field) {
$options = _optionwidgets_options($field);
if ($field['multiple']) {
- $keys = $node_field['keys'];
+ $keys = $items['keys'];
}
else {
- $keys = array($node_field['key']);
+ $keys = array($items['key']);
}
$values = array();
@@ -150,11 +150,11 @@ function optionwidgets_widget($op, &$node, $field, &$node_field) {
}
}
- $node_field = content_transpose_array_rows_cols(array('value' => $values));
+ $items = content_transpose_array_rows_cols(array('value' => $values));
// Remove the widget's data representation so it isn't saved.
- unset($node_field['keys']);
- unset($node_field['key']);
+ unset($items['keys']);
+ unset($items['key']);
break;
}
}
diff --git a/text.module b/text.module
index 33e3489..17d904e 100644
--- a/text.module
+++ b/text.module
@@ -130,6 +130,14 @@ function text_field($op, &$node, $field, &$items, $teaser, $page) {
}
}
}
+ if ($field['max_length'] > 0) {
+ foreach ($items as $delta => $data) {
+ $error_field = $field['field_name'].']['.$delta.'][value';
+ if (strlen($data['value']) > $field['max_length']) {
+ form_set_error($error_field, t('%label is longer than %max characters.', array('%label' => $field['widget']['label'], '%max' => $field['max_length'])));
+ }
+ }
+ }
break;
}
}
@@ -325,17 +333,6 @@ function text_widget($op, &$node, $field, &$items) {
}
return $form;
- case 'validate':
- if ($field['max_length'] > 0) {
- foreach ($items as $delta => $data) {
- $error_field = $field['field_name'].']['.$delta.'][value';
- if (strlen($data['value']) > $field['max_length']) {
- form_set_error($error_field, t('%label is longer than %max characters.', array('%label' => $field['widget']['label'], '%max' => $field['max_length'])));
- }
- }
- }
- break;
-
case 'process form values':
// Don't save empty fields except the first value
foreach ($items as $delta => $item) {
diff --git a/userreference.module b/userreference.module
index e224ddf..54a1dc1 100644
--- a/userreference.module
+++ b/userreference.module
@@ -66,10 +66,18 @@ function userreference_field_settings($op, $field) {
/**
* Implementation of hook_field().
*/
-//function userreference_field($op, &$node, $field, &$items, $teaser, $page) {
-// switch ($op) {
-// }
-//}
+function userreference_field($op, &$node, $field, &$items, $teaser, $page) {
+ switch ($op) {
+ case 'validate':
+ foreach ($items as $delta => $item) {
+ $error_field = $field['field_name'].']['.$delta.'][user_name';
+ if ($item['user_name'] && !in_array($item['user_name'], _userreference_potential_references($field))) {
+ form_set_error($error_field, t('Invalid user name.'));
+ }
+ }
+ return;
+ }
+}
/**
* Implementation of hook_field_formatter_info().
@@ -127,27 +135,27 @@ function userreference_widget_info() {
/**
* Implementation of hook_widget().
*/
-function userreference_widget($op, &$node, $field, &$node_field) {
+function userreference_widget($op, &$node, $field, &$items) {
if ($field['widget']['type'] == 'userreference_select') {
switch ($op) {
case 'prepare form values':
- $node_field_transposed = content_transpose_array_rows_cols($node_field);
+ $items_transposed = content_transpose_array_rows_cols($items);
// get rid of null values
- $node_field['default uids'] = array_filter((array) $node_field_transposed['uid']);
+ $items['default uids'] = array_filter((array) $items_transposed['uid']);
break;
case 'form':
$form = array();
$options = _userreference_potential_references($field);
- if (empty($node_field['default uids'])) {
- $node_field['default uids'][] = 'none';
+ if (empty($items['default uids'])) {
+ $items['default uids'][] = 'none';
}
$form[$field['field_name']] = array('#tree' => TRUE);
$form[$field['field_name']]['uids'] = array(
'#type' => 'select',
'#title' => t($field['widget']['label']),
- '#default_value' => $node_field['default uids'],
+ '#default_value' => $items['default uids'],
'#multiple' => $field['multiple'],
'#size' => min(count($options), 6),
'#options' => $options,
@@ -160,27 +168,27 @@ function userreference_widget($op, &$node, $field, &$node_field) {
case 'process form values':
if ($field['multiple']) {
// drop the 'none' option
- unset($node_field['uids']['none']);
- if (!empty($node_field['uids'])) {
- $node_field = content_transpose_array_rows_cols(array('uid' => $node_field['uids']));
+ unset($items['uids']['none']);
+ if (!empty($items['uids'])) {
+ $items = content_transpose_array_rows_cols(array('uid' => $items['uids']));
}
else {
- $node_field[0]['uid'] = '';
+ $items[0]['uid'] = '';
}
}
else {
- $node_field[0]['uid'] = ($node_field['uids'] != 'none') ? $node_field['uids'] : '';
+ $items[0]['uid'] = ($items['uids'] != 'none') ? $items['uids'] : '';
}
// Remove the widget's data representation so it isn't saved.
- unset($node_field['uids']);
+ unset($items['uids']);
}
}
else {
switch ($op) {
case 'prepare form values':
- foreach ($node_field as $delta => $item) {
- if (!empty($node_field[$delta]['uid'])) {
- $node_field[$delta]['default user_name'] = db_result(db_query("SELECT name FROM {users} WHERE uid = '%d'", $node_field[$delta]['uid']));
+ foreach ($items as $delta => $item) {
+ if (!empty($items[$delta]['uid'])) {
+ $items[$delta]['default user_name'] = db_result(db_query("SELECT name FROM {users} WHERE uid = '%d'", $items[$delta]['uid']));
}
}
break;
@@ -193,7 +201,7 @@ function userreference_widget($op, &$node, $field, &$node_field) {
$form[$field['field_name']]['#type'] = 'fieldset';
$form[$field['field_name']]['#description'] = $field['widget']['description'];
$delta = 0;
- foreach ($node_field as $item) {
+ foreach ($items as $item) {
if ($item['uid']) {
$form[$field['field_name']][$delta]['user_name'] = array(
'#type' => 'textfield',
@@ -220,34 +228,25 @@ function userreference_widget($op, &$node, $field, &$node_field) {
'#type' => 'textfield',
'#title' => t($field['widget']['label']),
'#autocomplete_path' => 'user/autocomplete',
- '#default_value' => $node_field[0]['default user_name'],
+ '#default_value' => $items[0]['default user_name'],
'#required' => $field['required'],
'#description' => $field['widget']['description'],
);
}
return $form;
- case 'validate':
- foreach ($node_field as $delta => $item) {
- $error_field = $field['field_name'].']['.$delta.'][user_name';
- if ($item['user_name'] && !in_array($item['user_name'], _userreference_potential_references($field))) {
- form_set_error($error_field, t('Invalid user name.'));
- }
- }
- return;
-
case 'process form values':
- foreach ($node_field as $delta => $item) {
+ foreach ($items as $delta => $item) {
$uid = '';
- if (!empty($node_field[$delta]['user_name'])) {
- $uid = db_result(db_query("SELECT uid FROM {users} WHERE name = '%s'", $node_field[$delta]['user_name']));
+ if (!empty($items[$delta]['user_name'])) {
+ $uid = db_result(db_query("SELECT uid FROM {users} WHERE name = '%s'", $items[$delta]['user_name']));
}
// Remove the widget's data representation so it isn't saved.
- unset($node_field[$delta]['user_name']);
- $node_field[$delta]['uid'] = $uid;
+ unset($items[$delta]['user_name']);
+ $items[$delta]['uid'] = $uid;
// Don't save empty fields except the first value
if (empty($uid) && $delta > 0) {
- unset($node_field[$delta]);
+ unset($items[$delta]);
}
}
}