简体   繁体   English

如何在drupal节点表单中检测其编辑或添加表单?

[英]how to detect in drupal node form if its edit or add form?

有没有一种方法可以检测正在查看的节点形式是“编辑”还是“添加新节点”形式?

Detect where? 检测到哪里? In a hook_alter? 在hook_alter中? In a template? 在模板中? Somewhere else? 别的地方?

In general, the approach would be to get ahold of the $node object, and see if it's nid field is set. 通常,方法是获取$ node对象,并查看是否设置了nid字段。 If it is, it's an edit. 如果是,则为编辑。

Also you can make use of URL, if you don't want to load entire node object. 如果您不想加载整个节点对象,也可以使用URL。 When it is new node addition, then in the URL arg(0) will be "node", arg(1) will be "add", arg(2) will be "content_type_name" whereas in the case of node viewing arg(0) will be node and arg(1) will be nid(ie Numeric). 当是新节点添加时,URL中的arg(0)将为“ node”,arg(1)将为“ add”,arg(2)将为“ content_type_name”,而在节点查看arg(0)的情况下)将是节点,而arg(1)将是nid(即数字)。 This is just an alternative way to detect. 这只是检测的另一种方法。

check these answers from drupal.stackexchange.com 从drupal.stackexchange.com检查这些答案

for example: 例如:

function mymodule_form_node_form_alter(&$form, &$form_state) {
  $node = $form_state['node'];

  if (!isset($node->nid) || isset($node->is_new)) {
    // This is a new node.
  }
  else {
    // This is not a new node.
  }
}

or use the arg() function as previously has been indicated. 或使用前面已指出的arg()函数。

if ($node->is_new) {do_something_for_new_node();}

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

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