Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
IT is not possible to pass variables to eval'd code, I know, but at least we could provide some featured APIs for this?
I now depend on helpers module, for example:
/**
* Find out if the current page is a node page.
* @return TRUE if the page is a node, FALSE if it is another page.
*/
function is_node_page() {
if (arg(0) == 'node' && is_numeric(arg(1))) {
return TRUE;
}
return FALSE;
}
/**
* Grab the current 'active' node
* An active node is the one node 123 as shown on page with path node/123, or
* its aliased path(s).
* @return $node object if the page is a node, empty $node object if it is not a
* node, or if we cannot see it, for any valid reason.
*/
function node_load_current() {
$node = new stdClass();
if (is_node_page()) {
$node = node_load(arg(1));
}
return $node;
}
But we need something that makes $node available on_validate and _preview.
Comments
Comment #1
fagohm, yes this approach looks fine for node updates. But as you said for node creation things are more difficult. For sure the easiest way is to use the token module.
But for those going with php: To get the appropriate $node one needs to get the global $form_values, which is an array representation of the node. This should do it for both cases.
Comment #2
fagoI've added the $form_values tip to the README. However any code that makes $node available as pattern would be still nice..
Comment #3
gaurav.kapoor CreditAttribution: gaurav.kapoor commented