简体   繁体   English

Drupal 6,如何从代码发布节点

[英]Drupal 6, how to publish node from code

How to publish node from php code?如何从 php 代码发布节点? Tried simple query db_query("UPDATE {node} SET status = 1 WHERE nid = %d", $nid);尝试了简单的查询db_query("UPDATE {node} SET status = 1 WHERE nid = %d", $nid); but it doesn't work.但它不起作用。 My scenario: By default nodes are unpublished.我的场景:默认情况下节点未发布。 I'm altering edit-node form to add textfield.我正在更改编辑节点表单以添加文本字段。 If user enters right code node become published.如果用户输入正确的代码节点成为发布。 So I adding another submit function in form_alter and in that function checking code and if it's right trying to update node status to published.因此,我在 form_alter 和 function 检查代码中添加了另一个提交 function 以及尝试将节点状态更新为已发布是否正确。

<?php
// load the node object
$node = node_load($nid);
// set status property to 1
$node->status = 1;
// re-save the node
node_save($node);
?>

Also, I saw your comment of using $form['nid']['#value'].另外,我看到您对使用 $form['nid']['#value'] 的评论。 Are you sure that variable holds the node ID value?您确定该变量包含节点 ID 值吗? Usually you would be executing code on the submit handler of a form, which means you would be using something like: $form_state['values']['nid'] to get the value of the $form['nid'] element.通常你会在表单的提交处理程序上执行代码,这意味着你会使用类似的东西: $form_state['values']['nid'] 来获取 $form['nid'] 元素的值。

Example:例子:

<?php
function mymodule_myform() {
  $form = array();
  $form['nid'] = array(
    '#type' => 'textfield',
    '#title' => 'Node ID to Publish'
  );
  $form['submit'] = array('#type' => 'submit', '#value' => 'Submit');
}

function mymodule_myform_submit($form, &$form_state) {
  $node = node_load($form_state['values']['nid']);

  if ($node) {
    $node->status = 1;
    node_save($node);
  }
}

It's best practice not to code against the db schema directly as writing queries yourself introduces redundancy.最好不要直接针对 db 模式进行编码,因为自己编写查询会引入冗余。 You should always use the node API.您应该始终使用节点 API。

Drupal invokes the node api when performing a node save. Drupal 在执行节点保存时调用节点 api。 This gives all the other modules in your application a chance to modify the node in any way they wish.这使您的应用程序中的所有其他模块有机会以他们希望的任何方式修改节点。 So, it may be likely that your code is working correctly just not as you expect.因此,您的代码可能正常工作,但与您预期的不同。 For example, another module could set the node status after you set it and over write your value.例如,另一个模块可以在您设置节点状态后设置节点状态并覆盖您的值。

This might be a little hard to debug.这可能有点难以调试。 You could try changing the weight of the module your code runs in to be called after everything else.您可以尝试更改代码运行的模块的权重,以便在其他所有操作之后调用。 This is one of the few exceptions where you can modify the database schema directly (there is a module that exposes module weights in the UI but this is a terrible idea).这是您可以直接修改数据库模式的少数例外之一(有一个模块可以在 UI 中公开模块权重,但这是一个糟糕的主意)。 I don't particularly like relying on module weights to get code to work.我不是特别喜欢依靠模块权重来让代码工作。 You could end up with race conditions but this will at least identify this as the cause.您最终可能会遇到竞争条件,但这至少可以确定这是原因。

See:看:
http://api.drupal.org/api/drupal/modules--node--node.module/function/node_save/6 http://api.drupal.org/api/drupal/modules--node--node.module/function/node_save/6

Try using the node_load and node_save functions built in to drupal.尝试使用 drupal 内置的node_loadnode_save函数。

  1. Check that your custom module/theme enabled.检查您的自定义模块/主题是否已启用。

  2. I would also try to create a node through Drupal interface (make it unpublished).我还会尝试通过 Drupal 接口创建一个节点(使其未发布)。 Then run db_query("UPDATE {node} SET status = 1 WHERE nid = %d", [paste_just_created_node_id_here]);然后运行db_query("UPDATE {node} SET status = 1 WHERE nid = %d", [paste_just_created_node_id_here]); in your custom module and see if this works.在您的自定义模块中,看看这是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM