Мне надо выполнить код после сохранения ноды в базе, однако у меня он почему-то выполняется ранее, хотя везде утверждается, что все наоборот.
Вот что написано на api.drupal.org для хука hook_nodeapi:
"update": The node has just been updated in the database.
А у меня:
<?php
function mccme_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
switch ($op) {
case 'update':
print "Выполняю код";
break;
}
}
?>
Далее открыл файл modules/node/node.module и добавил в конец функции node_save сторочку
<?php
print "Выполняю сохранение";
?>
В итоге, при пересохранении любой страницы я получаю <<Выполняю код.Выполняю сохранение>>
Комментарии
Операции 'update' и 'insert' в этом хуке выполняются после того, как нода была сохранена. См. код node_save() и мои комментарии:
* Save a node object into the database.
*/
function node_save(&$node) {
// Let modules modify the node before it is saved to the database.
node_invoke_nodeapi($node, 'presave');
global $user;
// Insert a new node.
$node->is_new = empty($node->nid);
if ($node->is_new || !empty($node->revision)) {
// When inserting a node, $node->log must be set because
// {node_revisions}.log does not (and cannot) have a default
// value. If the user does not have permission to create
// revisions, however, the form will not contain an element for
// log so $node->log will be unset at this point.
if (!isset($node->log)) {
$node->log = '';
}
}
elseif (empty($node->log)) {
// When updating a node, however, avoid clobbering an existing
// log entry with an empty one.
unset($node->log);
}
// For the same reasons, make sure we have $node->teaser and
// $node->body set.
if (!isset($node->teaser)) {
$node->teaser = '';
}
if (!isset($node->body)) {
$node->body = '';
}
// Save the old revision if needed.
if (!$node->is_new && !empty($node->revision) && $node->vid) {
$node->old_vid = $node->vid;
}
$time = time();
if (empty($node->created)) {
$node->created = $time;
}
// The changed timestamp is always updated for bookkeeping purposes (revisions, searching, ...)
$node->changed = $time;
$node->timestamp = $time;
$node->format = isset($node->format) ? $node->format : FILTER_FORMAT_DEFAULT;
// Generate the node table query and the node_revisions table query.
if ($node->is_new) {
_node_save_revision($node, $user->uid);
drupal_write_record('node', $node);
db_query('UPDATE {node_revisions} SET nid = %d WHERE vid = %d', $node->nid, $node->vid);
$op = 'insert';
}
else {
drupal_write_record('node', $node, 'nid');
if (!empty($node->revision)) {
_node_save_revision($node, $user->uid);
db_query('UPDATE {node} SET vid = %d WHERE nid = %d', $node->vid, $node->nid);
}
else {
_node_save_revision($node, $user->uid, 'vid');
}
$op = 'update';
}
// Нода уже сохранена и имеет $node->nid.
// Вызывается hook_nodeapi с $op='update' или 'insert' в зависимости от того, что было раньше задано.
// Call the node specific callback (if any).
node_invoke($node, $op);
node_invoke_nodeapi($node, $op);
// Update the node access table for this node.
node_access_acquire_grants($node);
// Clear the page and block caches.
cache_clear_all();
}
Дадада, Вы все правильно написали. Я как-то невнимательно посмотрел.
Но у меня все равно не выходит (((
Вот смотрите как получается..
Да, нода сохранена, однако значения CCK полей, которые мне и нужны, нифига еще не сохранены.
У меня получается так:
И привет, код все время работает с теми значениями, что были до обновления ноды.
Мне бы их вызов местами поменять -)
Ура)
Оказывается, каждому модулю приписан свой вес, на основании его и выбирается порядок вызовов всех хуков.
Если вдруг кому понадобится: http://drupal.org/node/110238