如何在drupal表单中隐藏字段

时间:2022-09-12 08:52:44

I have a specific content type in drupal6. I want to implement a hook, wich hides the body field of that content type from the add form, but not from the edit form. How can i do that?

我在drupal6中有一个特定的内容类型。我想要实现一个钩子,它从add表单中隐藏了该内容类型的body字段,但不是来自编辑表单。我怎么做呢?

4 个解决方案

#1


9  

You can use hook_form_alter. Which you can programmatically alter the contents of the form api build. This gives you the full $form array of which you can simply unset($form['the_field_you_dont_want']);.

您可以使用hook_form_alter。您可以通过编程方式更改表单api构建的内容。这提供了完整的$form数组,您可以简单地取消设置($form['the_field_you_dont_want']);

But the easier way to get rid of the body field is in the edit content type there is a field labelled 'Body field label:' just leave this blank and the body field will be omitted.

但是,更容易摆脱body字段的方法是在编辑内容类型中,有一个名为“body field label”的字段:“只需保留这个空字段,body字段就会被省略。”

#2


9  

unset seems to destroy the value as well, as does the #access property. I just use this to hide a field (in this case a reference if it was preset using the URL:

unset似乎也会破坏值,正如#access属性一样。我只是用它来隐藏一个字段(在这种情况下,如果是使用URL预先设置的引用):

$form['field_reference']['#prefix'] = "<div class='hide'>";
$form['field_reference']['#suffix'] = "</div>";

#3


6  

The solution found here https://drupal.stackexchange.com/questions/11237/hide-field-in-node-add-page works great for me. Here I am repeating moon.watcher's solution:

在这里找到的解决方案是https://drupal。stackexchange.com/questions/11237/hide-field-in-node-add-page对我来说很有用。我在这里重复月亮。观察者的解决办法:

function test_remove_filed_form_alter(&$form, &$form_state) {

    if (arg(0) == 'node' && arg(1) == 'add') {
    $form['field_test']['#access'] = 0;
    }

}

The disadvantage of using unset() is that it will entirely remove the field and you can't further on like, for example, on node presave. In my case, I just wanted to remove the field from the form on the first moment, but I wanted to populate it later on, prior to saving the node. The solution on the link above works perfect for me for this reason.

使用unset()的缺点是,它将完全删除字段,并且您不能像在node presave中那样更进一步。在我的例子中,我只是想在第一时间从表单中删除字段,但是我想稍后填充它,然后保存节点。基于这个原因,上面链接的解决方案非常适合我。

#4


1  

Did you implement the content type within a module (using hook_node_info)? If so, set the has_body attribute to false.

您是否在模块中实现了内容类型(使用hook_node_info)?如果是,将has_body属性设置为false。

#1


9  

You can use hook_form_alter. Which you can programmatically alter the contents of the form api build. This gives you the full $form array of which you can simply unset($form['the_field_you_dont_want']);.

您可以使用hook_form_alter。您可以通过编程方式更改表单api构建的内容。这提供了完整的$form数组,您可以简单地取消设置($form['the_field_you_dont_want']);

But the easier way to get rid of the body field is in the edit content type there is a field labelled 'Body field label:' just leave this blank and the body field will be omitted.

但是,更容易摆脱body字段的方法是在编辑内容类型中,有一个名为“body field label”的字段:“只需保留这个空字段,body字段就会被省略。”

#2


9  

unset seems to destroy the value as well, as does the #access property. I just use this to hide a field (in this case a reference if it was preset using the URL:

unset似乎也会破坏值,正如#access属性一样。我只是用它来隐藏一个字段(在这种情况下,如果是使用URL预先设置的引用):

$form['field_reference']['#prefix'] = "<div class='hide'>";
$form['field_reference']['#suffix'] = "</div>";

#3


6  

The solution found here https://drupal.stackexchange.com/questions/11237/hide-field-in-node-add-page works great for me. Here I am repeating moon.watcher's solution:

在这里找到的解决方案是https://drupal。stackexchange.com/questions/11237/hide-field-in-node-add-page对我来说很有用。我在这里重复月亮。观察者的解决办法:

function test_remove_filed_form_alter(&$form, &$form_state) {

    if (arg(0) == 'node' && arg(1) == 'add') {
    $form['field_test']['#access'] = 0;
    }

}

The disadvantage of using unset() is that it will entirely remove the field and you can't further on like, for example, on node presave. In my case, I just wanted to remove the field from the form on the first moment, but I wanted to populate it later on, prior to saving the node. The solution on the link above works perfect for me for this reason.

使用unset()的缺点是,它将完全删除字段,并且您不能像在node presave中那样更进一步。在我的例子中,我只是想在第一时间从表单中删除字段,但是我想稍后填充它,然后保存节点。基于这个原因,上面链接的解决方案非常适合我。

#4


1  

Did you implement the content type within a module (using hook_node_info)? If so, set the has_body attribute to false.

您是否在模块中实现了内容类型(使用hook_node_info)?如果是,将has_body属性设置为false。