hook_form_alter 不工作

发布于 2024-08-31 19:40:16 字数 377 浏览 8 评论 0原文

我使用的是 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 技术交流群。

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

发布评论

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

评论(4

夢归不見 2024-09-07 19:40:16

您的代码中有两个错误

  1. 您的函数签名错误,正如 hfidgen (+1) 已经指出的那样。它需要是 yourModuleName_form_alter(&$form, &$form_state, $form_id),因此在您的示例中,表单 id 上的开关永远不会触发。
  2. 您检查表单 ID 是否错误。在这种情况下,您需要检查两个表单 ID,并且两者都与您正在使用的不同:
    1. user_login_block 用于作为块提供的小型登录表单(在大多数页面上常用)
    2. user_login 用于显式登录页面(通常位于“user/login”下)

两种形式在结构上基本相同,因此您通常可以在同一个 hook_form_alter 实现中更改这两种形式 - 只需添加另一个 case 语句来检查第二个版本。

There are two errors in your code:

  1. Your function signature is wrong, as already pointed out by hfidgen (+1). It needs to be yourModuleName_form_alter(&$form, &$form_state, $form_id), so in your example the switch on the form id will never trigger.
  2. You check for the wrong form id. There are two form ids you need to check in this case, and both are different from the one you're using:
    1. user_login_block for the small login form available as a block (commonly used on most pages)
    2. 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.

携君以终年 2024-09-07 19:40:16

我发现使用主题函数来更改表单更容易 - 在主题的 template.php 中只需创建以下内容:

function YOURTHEMENAMEHERE_user_login_form($form) {
    $form['submit']['#value'] = t('Sign in');
    //dsm($form);
    return drupal_render($form);
}

注释掉的行 (dsm) 用于 Drupal 开发模块 - 我也建议安装它。安装此程序并在管理员角色上设置权限以便您可以使用它后,您将获得一个新选项卡,其中准确显示页面的构建方式以及哪些阵列执行哪些操作。

沿着数组中的线索,您几乎可以为网站上的任何内容设置主题。

编辑 - 哦好吧:P 我注意到的一件事是,API 中的示例在函数中有 3 个变量,但之前没有使用过这个钩子,但你有 2 个!不匹配意味着您可能输入了错误的变量:

function modulename_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'contact_mail_page':
    $form['submit']['#value'] = t('Sign in');
    break;
  }
}

I find it easier to use theme functions to alter the forms - in your theme's template.php just create this:

function YOURTHEMENAMEHERE_user_login_form($form) {
    $form['submit']['#value'] = t('Sign in');
    //dsm($form);
    return drupal_render($form);
}

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:

function modulename_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'contact_mail_page':
    $form['submit']['#value'] = t('Sign in');
    break;
  }
}
_畞蕅 2024-09-07 19:40:16

对于这样一个微不足道的更改,您不应该编写模块。对于性能目标和浪费时间而言,您付出的代价实在是太高了。

您可以执行字符串替换,这将影响 t() 函数处理的字符串。这是通过网站的settings.php配置文件完成的。

以下是如何将“登录”替换为“登录”...

$conf['locale_custom_strings_en'] = array(
  'Log In'      => 'Sign In',
);

这只会影响英文字符串。请随意将结尾的 _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" ...

$conf['locale_custom_strings_en'] = array(
  'Log In'      => '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.

吻安 2024-09-07 19:40:16

最好在开始任何表单更改之前执行此操作:查看表单的源代码并检查隐藏字段 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文