Drupal 6 从提交的表单中获取节点标题
我在编辑内容时使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所有提交函数都有两个参数传递给它们:$form,这是对 hook_form_alter 等进行所有调整后的最终表单数组,以及 $form_state,其中包含已提交的值,这些值已被清理并检查范围。 (例如,如果选择框中有三个项目,则 $form_state['values'] 中的数据已确保该输入的值是三个合法值之一。)
通常,您不应该使用$form['#post'] - 它不是获取值的已发布方式的一部分,并且更新核心以处理 FAPI 的某些问题可能会破坏您的代码。
试试这个:
注意 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:
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.
DKinzer 建议使用 dsm($form) 查看变量。节点标题未填充。它可以在 Post 数组中找到。下面的行让我可以做我想做的事。
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.
的签名
尝试更改您的To:
:同时尝试安装 devel 模块,以便您可以执行类似的操作
并准确查看您正在处理的内容。
另外,如果您只想在创建“X”类型的新节点时发出消息,更好的方法是使用 hook_nodeapi;
它可能看起来像这样;
Try changing the signature of your
To:
Also try installing the devel module so you can do things like
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;