Drupal 6 从提交的表单中获取节点标题

发布于 2024-09-18 10:18:57 字数 358 浏览 9 评论 0原文

我在编辑内容时使用 form_alter 来编辑提交功能。在我的自定义函数中,我希望用标题名称编辑一条自定义消息到屏幕上。我认为我可以做到这一点的一种方法如下

function mymodule_myfunction(&$form) {
    drupal_set_message(t('Some text ' . $form['#node']->title));
}

标题没有加入到“某些文本”中,

我通过在 form_alter 中使用以下行来调用我的函数:

$form['#submit'][] = 'mymodule_myfunction';

I am using form_alter to edit the submit function when editing content. In my custom function I wish to edit a custom message to the screen with the title name. I thought a way I could do this is something as follows

function mymodule_myfunction(&$form) {
    drupal_set_message(t('Some text ' . $form['#node']->title));
}

The title is not being joined to the 'Some text'

I am calling my function by using the following line in my form_alter:

$form['#submit'][] = 'mymodule_myfunction';

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

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

发布评论

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

评论(3

甜中书 2024-09-25 10:18:57

所有提交函数都有两个参数传递给它们:$form,这是对 hook_form_alter 等进行所有调整后的最终表单数组,以及 $form_state,其中包含已提交的值,这些值已被清理并检查范围。 (例如,如果选择框中有三个项目,则 $form_state['values'] 中的数据已确保该输入的值是三个合法值之一。)

通常,您不应该使用$form['#post'] - 它不是获取值的已发布方式的一部分,并且更新核心以处理 FAPI 的某些问题可能会破坏您的代码。

试试这个:

function mymodule_myfunction($form, &$form_state) {
  drupal_set_message(t('Some Message @title'), 
  array('@title' => $form_state['values']['title'])));
}

注意 t() 函数的正确使用 - 意图该功能的主要目的是允许其他用户翻译文本,因此通过使用“Some Message @title”,翻译人员可以更多地了解正在发生的事情。此外,您还可以获得以下优势:以这种方式通过 t 函数馈送的文本也通过 check_plain 馈送(),这可以防止有人对输入进行恶意操作。

All submit functions get two parameters passed to them: $form, which is the final form array after all of the adjustments for hook_form_alter and the like, and $form_state which among other values contains the submitted values, which have been cleaned and checked for ranges. (For instance, if you have three items in a select box, the data in $form_state['values'] already has made sure that the value for that input is one of the three legal values.)

Generally, you shouldn't use $form['#post'] - it's not part of the published way to get at values, and an update to the core to handle some problem with FAPI could conceivably break your code.

Try this:

function mymodule_myfunction($form, &$form_state) {
  drupal_set_message(t('Some Message @title'), 
  array('@title' => $form_state['values']['title'])));
}

Note the corrected use of the t() function - the intent of that function is to allow other users to translate text, and so by using 'Some Message @title' the translator knows more about what is going on. Additionally you get the advantage that text fed through the t function in this way also is fed through check_plain(), which prevents someone from doing something malicious with the input.

时光沙漏 2024-09-25 10:18:57

DKinzer 建议使用 dsm($form) 查看变量。节点标题未填充。它可以在 Post 数组中找到。下面的行让我可以做我想做的事。

drupal_set_message(t('Some Text '.$form['#post']['title']));

DKinzer recommended using dsm($form)to see the variables. The Node title is not populated. It can be found in the Post array. The following line allowed me to do what I wanted.

drupal_set_message(t('Some Text '.$form['#post']['title']));
深居我梦 2024-09-25 10:18:57

的签名

function mymodule_myfunction(&$form) {
    drupal_set_message(t('Some text ' . $form['#node']->title));
}

尝试更改您的To:

function mymodule_myfunction($form, &$form_state) {
    drupal_set_message(t('Some text ' . $form['#node']->title));
}

:同时尝试安装 devel 模块,以便您可以执行类似的操作

dsm($form);
dsm($form_state);

并准确查看您正在处理的内容。

另外,如果您只想在创建“X”类型的新节点时发出消息,更好的方法是使用 hook_nodeapi;

它可能看起来像这样;

function modulename_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {

if ($op == 'insert' && $node->type == 'my node  type') {
  drupal_set_message($node-title . ' is cool.');
}
}

Try changing the signature of your

function mymodule_myfunction(&$form) {
    drupal_set_message(t('Some text ' . $form['#node']->title));
}

To:

function mymodule_myfunction($form, &$form_state) {
    drupal_set_message(t('Some text ' . $form['#node']->title));
}

Also try installing the devel module so you can do things like

dsm($form);
dsm($form_state);

And see exactly what you are dealing with.

Also, if all you want to do is give a message when a new node of type 'X' is created a better way is to use hook_nodeapi;

It could look something like this;

function modulename_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {

if ($op == 'insert' && $node->type == 'my node  type') {
  drupal_set_message($node-title . ' is cool.');
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文