[documentation] [Documentation bug] Update needed to
node_example.module
pwolanin
drupal-docs at drupal.org
Sun Sep 17 20:41:18 UTC 2006
Issue status update for
http://drupal.org/node/84602
Post a follow up:
http://drupal.org/project/comments/add/84602
Project: Documentation
Version: <none>
Component: Developer Guide
Category: bug reports
Priority: normal
Assigned to: Anonymous
Reported by: plumbley
Updated by: pwolanin
Status: active
I think form_set_value *is* needed to chage a vlue at the validate sage,
as opposed to being able to directly change the node in 4.6. I was just
try to help someone figure this out recently:
http://drupal.org/node/83130
pwolanin
Previous comments:
------------------------------------------------------------------------
Sun, 17 Sep 2006 08:06:24 +0000 : plumbley
Current CVS documentation for hook_example_validate() includes the
example
<?php
function node_example_validate(&$node) {
[ ... if ($node->quantity) { ]
// Let an empty field mean "zero."
$node->quantity = 0;
}
}
?>
But since 4.7 it looks like any changes to $node made in hook_validate
are ignored. E.g. in modules/node/node.module we have
<?php
function node_validate($node, $form = array()) {
...
node_invoke($node, 'validate', $form);
node_invoke_nodeapi($node, 'validate', $form);
}
?>
which is pass-by-value rather than pass-by-reference. So although
node_invoke() passes by reference, no changes to $node can "escape" out
of node_validate.
I'm guessing this is by design, but if so the documentation should say
what to do instead. E.g. I *think* form_set_value() should now be used,
but its a bit non-obvious. For example, I used something similar to the
following elsewhere (in 4.7):
<?php
form_set_value(array('#parents' => array('quantity')), 0);
?>
Is this now the recommended route to change nodes? (or not?). Mark.
PS Looks like the change to validation operation in 4.7 also caused
problems for node_import, which ideally should be able to validate a
node without requiring a form to be present, so a documentation update
might help getting validation working there too.
------------------------------------------------------------------------
Sun, 17 Sep 2006 19:13:44 +0000 : plumbley
Renamed now I finally found out what I probably should have found much
earlier about the 4.7 submit process (see e.g.
http://drupal.org/node/22218#node_hook_order). Please ignore my previous
rubbish about form_set_value().
It looks like node_example.module as appears in 4.7 at
http://api.drupal.org/api/4.7/file/developer/examples/node_example.modul...
and CVS at
http://api.drupal.org/api/HEAD/file/developer/examples/node_example.modu...
actually applies to 4.6, and is in need of update.
I'm still a bit new here but perhaps hook_validate() should now be
pass-by-value and read e.g.:
<?php
function node_example_validate($node) {
// If the field is set, it must be a number
if (!empty($node->quantity) && !is_numeric($node->quantity)) {
form_set_error('quantity', t('The quantity must be a number.'));
}
}
?>
with a new hook_submit() which performs the modification
<?php
function node_example_submit(&$node) {
if (empty($node->quantity)) {
// Let an empty field mean "zero."
$node->quantity = 0;
}
}
?>
If its too fiddly to fix for the moment, perhaps just a note to say
"node_example.module applies to 4.6, see
http://api.drupal.org/api/4.7/file/developer/hooks/node.php for up to
date documentation" in the meantime? Mark.
More information about the documentation
mailing list