hook_form_alter 不工作
我使用的是 drupal 6.16。下面的 hook_form_alter 代码不起作用。我只是想将用户登录表单的提交按钮上的“登录”更改为“登录”
<?php
//$Id$
function helloworld_form_alter($form_id,&$form) {
switch ($form_id) {
case 'user_login_form':
// Change 'Log in' to 'Sign in'.
$form['submit']['#value'] = t('Sign in');
break;
}
}
有什么方法可以解决这个问题吗?
请帮忙。 谢谢。
Im using drupal 6.16. The below code for hook_form_alter is not working. Im simply trying to change the 'Log In' to 'Sign In' on the submit button of the user login form
<?php
//$Id$
function helloworld_form_alter($form_id,&$form) {
switch ($form_id) {
case 'user_login_form':
// Change 'Log in' to 'Sign in'.
$form['submit']['#value'] = t('Sign in');
break;
}
}
Any way to fix this ?
Please help.
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的代码中有两个错误:
yourModuleName_form_alter(&$form, &$form_state, $form_id)
,因此在您的示例中,表单 id 上的开关永远不会触发。user_login_block
用于作为块提供的小型登录表单(在大多数页面上常用)user_login
用于显式登录页面(通常位于“user/login”下)两种形式在结构上基本相同,因此您通常可以在同一个
hook_form_alter
实现中更改这两种形式 - 只需添加另一个 case 语句来检查第二个版本。There are two errors in your code:
yourModuleName_form_alter(&$form, &$form_state, $form_id)
, so in your example the switch on the form id will never trigger.user_login_block
for the small login form available as a block (commonly used on most pages)user_login
for the explicit login page (usually found under 'user/login')Both forms are mostly identical in structure, so you can usually change both within the same
hook_form_alter
implementation - just add another case statement to check for the second version.我发现使用主题函数来更改表单更容易 - 在主题的 template.php 中只需创建以下内容:
注释掉的行 (dsm) 用于 Drupal 开发模块 - 我也建议安装它。安装此程序并在管理员角色上设置权限以便您可以使用它后,您将获得一个新选项卡,其中准确显示页面的构建方式以及哪些阵列执行哪些操作。
沿着数组中的线索,您几乎可以为网站上的任何内容设置主题。
编辑 - 哦好吧:P 我注意到的一件事是,API 中的示例在函数中有 3 个变量,但之前没有使用过这个钩子,但你有 2 个!不匹配意味着您可能输入了错误的变量:
I find it easier to use theme functions to alter the forms - in your theme's template.php just create this:
the commented out line (dsm) is for the Drupal devel module - which I'd also recommend installing. Once you've installed this and set permissions on your admin role so that you can use it, you'll get a new tab which shows you exactly how the page is constructed and which arrays do what.
Follow the trail within the arrays and you can pretty much theme anything on your site.
EDIT - oh ok :P The one thing I notice, not having used this hook before, is that the example in the API has 3 variables in the function, but you've got 2! Having a mismatch means you're probably being fed the wrong variable:
对于这样一个微不足道的更改,您不应该编写模块。对于性能目标和浪费时间而言,您付出的代价实在是太高了。
您可以执行字符串替换,这将影响 t() 函数处理的字符串。这是通过网站的settings.php配置文件完成的。
以下是如何将“登录”替换为“登录”...
这只会影响英文字符串。请随意将结尾的 _en 替换为特定的语言代码(_fr、_ja、_es),以对其他语言执行相同的操作。
For such a trivial change, you should not write a module. The price you pay in terms of performance hit and time wasted is simply to high for the target goal.
You can perform a string replacement that will affect string that are handled by the t() function. This is done is the settings.php configuration file of the site.
Here is how you could replace "Log In" with "Sign In" ...
This will affect the English strings only. Feel free to replace the trailing _en with a specific language code (_fr, _ja, _es) to do the same for other languages.
最好在开始任何表单更改之前执行此操作:查看表单的源代码并检查隐藏字段 form_id 的值 - 这将为您提供需要使用的确切 form_id。
It is best to do this before you start any form alter: Look at the source code of your form and check for the value of the hidden field form_id - this gives you the exact form_id that you need to use.