加载节点的先前版本

发布于 2024-09-11 07:40:44 字数 101 浏览 9 评论 0原文

当你得到一个节点时,如何加载之前的版本(修订版)?

我知道如何加载修订版,但不知道如何获取先前的修订版号($node->vid 是当前修订版)。

谢谢

When you get a node, how do you load the previous version (revision)?

I know how loading a revision but not how getting the previous revision number ($node->vid is the current revision).

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰魂雪魄 2024-09-18 07:40:44

假设你有一个节点对象$node,你可以使用下面的代码来获取之前的版本。

$previous_vid = db_result( 
  db_query('SELECT MAX(vid) AS vid FROM {node_revisions} WHERE vid < %d AND nid = %d', $node->vid, $node->nid)
);

获得先前版本后,您可以使用 node_load(array('nid' => $node-nid, 'vid' => $previous_vid)) 加载新节点对象。

该代码应检查 db_result() 是否返回 FALSE(如果没有先前的修订版本)。
需要注意的是,字段vid对于每个节点来说都是全局的;它不包含不同节点的相同值。

Supposing that you have a node object $node, you can use the following code to get the previous revision.

$previous_vid = db_result( 
  db_query('SELECT MAX(vid) AS vid FROM {node_revisions} WHERE vid < %d AND nid = %d', $node->vid, $node->nid)
);

Once you have the previous revision, you can load the new node object with node_load(array('nid' => $node-nid, 'vid' => $previous_vid)).

The code should check if db_result() returns FALSE, in the case there isn't a previous revision.
To note that the field vid is global for each node; it doesn't contain the same value for different nodes.

滥情空心 2024-09-18 07:40:44

谢谢大家。

我还找到了另一个解决方案:

  $revisions = node_revision_list($node);
  next($revisions);
  if ($preview_key = key($revisions)) {
    $preview_revision = $revisions[$preview_key];
    $old_node = node_load($node->nid, $preview_revision->vid);
  }

但是如果你有很多修改,你会得到一个大数组。

Thanks all.

I found also an other solution:

  $revisions = node_revision_list($node);
  next($revisions);
  if ($preview_key = key($revisions)) {
    $preview_revision = $revisions[$preview_key];
    $old_node = node_load($node->nid, $preview_revision->vid);
  }

But if you have a lot of revision you get a big array.

流云如水 2024-09-18 07:40:44

如果我明白你想做什么;您想在有人提交更改后获得节点的预览吗?

预览按钮有自己的提交处理程序, node_form_build_preview ()。在那里,它使用 $form_state 中的数据创建一个新的节点对象并运行 node_preview(),它返回预览的标记。

如果您想在用户单击预览按钮时捕获预览,则需要使用 hook_form_alter 将另一个提交处理程序添加到预览按钮:

$['form']['buttons']['preview']['#submit'][] = 'mymodule_custom_preview';

其中 mymodule_custom_preview 是自定义提交函数的名称。看看 node_form_build_preview() 作为指导,但您的提交函数将如下所示:

function mymodule_custom_preview($form, &$form_state) {
  $node = node_form_submit_build_node($form, $form_state);
  $preview = node_preview($node);
}

另请参阅 node_form(),它让您了解节点表单的结构。完成后,您的模块中将包含如下所示的代码:

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if (strstr($form_id, '_node_form') !== FALSE) {
    $['form']['buttons']['preview']['#submit'][] = 'mymodule_custom_preview';
  }
}

function mymodule_custom_preview($form, &$form_state) {
  $node = node_form_submit_build_node($form, $form_state);
  $preview = node_preview($node);

  // Do what you will with $preview.
}

If I understand what you're trying to do; you want to get the preview of the node after someone submits changes?

The preview button has its own submit handler, node_form_build_preview(). There, it creates a new node object using the data in $form_state and runs node_preview(), which returns the markup for the preview.

If you want to capture that preview when the user clicks the preview button, you'll need to use hook_form_alter to add another submit handler to the preview button:

$['form']['buttons']['preview']['#submit'][] = 'mymodule_custom_preview';

where mymodule_custom_preview is the name of your custom submit function. Take a look at node_form_build_preview() for guidance, but your submit function is going to look something like this:

function mymodule_custom_preview($form, &$form_state) {
  $node = node_form_submit_build_node($form, $form_state);
  $preview = node_preview($node);
}

Also take a look at node_form(), which gives you an idea of how the node form is structured. When you're all done, you're going to have code in your module that looks something like this:

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if (strstr($form_id, '_node_form') !== FALSE) {
    $['form']['buttons']['preview']['#submit'][] = 'mymodule_custom_preview';
  }
}

function mymodule_custom_preview($form, &$form_state) {
  $node = node_form_submit_build_node($form, $form_state);
  $preview = node_preview($node);

  // Do what you will with $preview.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文