template_preprocess_user_profile() 中未定义索引#account
我试图覆盖 Drupal 7 中的用户配置文件编辑表单,但我不断收到以下错误消息:
注意:未定义索引:template_preprocess_user_profile() 中的#account(/var/www/menit/modules/user/user.pages.inc 第 189 行)。
注意:未定义索引:rdf_preprocess_user_profile() 中的#account(/var/www/menit/modules/rdf/rdf.module 的第 578 行)。
注意:尝试在 user_uri() 中获取非对象的属性(/var/www/menit/modules/user/user.module 的第 190 行)。
注意:尝试在 rdf_preprocess_user_profile() 中获取非对象的属性(/var/www/menit/modules/rdf/rdf.module 的第 603 行)。
注意:尝试在 rdf_preprocess_user_profile() 中获取非对象的属性(/var/www/menit/modules/rdf/rdf.module 的第 604 行)。
我所做的是编写一个自定义模块,包含以下代码:
function custom_profile_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
global $user;
if ($user->uid != 1){
$form['#theme'] = 'user_profile';
}
}
function menit_theme($existing, $type, $theme, $path){
return array(
'user_profile' => array(
'render element' => 'form',
'template' => 'templates/user_profile',
),
);
}
并将以下 user_profile.tpl.theme 添加到我的主题模板文件夹中:
<div class="profile"<?php print $attributes; ?>>
<?php print render($user_profile['field_second_name']); ?>
<?php print render($user_profile['field_first_name']);?>
<?php print render($user_profile);?>
</div>
我现在有点迷失了,而且时间不多了。有谁知道我在这里做错了什么?
I'm trying to override the user profile edit form in Drupal 7, and I keep on getting the following error messages:
Notice: Undefined index: #account in template_preprocess_user_profile() (line 189 of /var/www/menit/modules/user/user.pages.inc).
Notice: Undefined index: #account in rdf_preprocess_user_profile() (line 578 of /var/www/menit/modules/rdf/rdf.module).
Notice: Trying to get property of non-object in user_uri() (line 190 of /var/www/menit/modules/user/user.module).
Notice: Trying to get property of non-object in rdf_preprocess_user_profile() (line 603 of /var/www/menit/modules/rdf/rdf.module).
Notice: Trying to get property of non-object in rdf_preprocess_user_profile() (line 604 of /var/www/menit/modules/rdf/rdf.module).
What I have done is writing a custom module, containing the following code:
function custom_profile_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
global $user;
if ($user->uid != 1){
$form['#theme'] = 'user_profile';
}
}
function menit_theme($existing, $type, $theme, $path){
return array(
'user_profile' => array(
'render element' => 'form',
'template' => 'templates/user_profile',
),
);
}
and added the following user_profile.tpl.theme to my theme templates folder:
<div class="profile"<?php print $attributes; ?>>
<?php print render($user_profile['field_second_name']); ?>
<?php print render($user_profile['field_first_name']);?>
<?php print render($user_profile);?>
</div>
I'm kind of lost now, and I'm running short on time. Does anyone have an idea of what have I done wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您正在使用的以下行:
该行更改了与表单关联的主题函数,并且导致调用一些预处理函数,例如 [template_preprocess_user_profile()][1] 和 [rdf_preprocess_user_profile()][ 2]。所有这些预处理函数都被认为是为用户配置文件调用的,它们正在寻找一些在您的情况下未定义的变量,例如
$variables['elements']['#account']
。您不使用模板文件来呈现表单。根据您想要实现的目标,您可以使用不同的方法:
如果您想删除某些表单字段,您可以实现hook_form_FORM_ID_alter(),这是您已经实现的钩子,并使用它隐藏一些表单字段。
这样,该字段将不会向用户显示。我建议使用该方法,因为与
unset($form[$field_id])
相比,该方法对其他模块造成的问题更少;如果您使用此选项,$form_state['values']
将不包含该字段的值,并且某些验证或提交处理程序可能会报告错误(例如“[字段名称”的值] 必须输入”)。如果您想向表单字段添加 CSS 类,您可以使用:
这是更简单、更快的方法。如果您需要包装多个表单字段,那么您应该使用类似于以下代码的内容:
在这种情况下,您通常需要向表单添加 CSS 样式,这可以使用类似于以下代码的代码来完成:
您还可以移动
#container
表单字段中的表单字段,如以下代码所示:在这种情况下,表单字段将使用
$form[$field_id]
的模块将会出现一些问题;这不适用于表单处理程序,除非$form[$field_id]['#tree']
设置为TRUE
。The problem is the following line you are using:
That lines changes the theme function associated to the form, and it is causing some preprocess functions to be invoked, such as [template_preprocess_user_profile()][1], and [rdf_preprocess_user_profile()][2]. All those preprocess functions, which are thought to be called for user profile, are looking for some variables that in your case are not defined, such as
$variables['elements']['#account']
.You don't use a template file to render a form. Depending what you want to achieve, you can use different methods:
If you want to remove some form fields, you implement
hook_form_FORM_ID_alter()
, which is the hook you have already implemented, and use it to hide some form fields.In this way, the field will not shown for the user. I suggest using that, as it is the method that causes less problem to other modules than
unset($form[$field_id])
; if you use this,$form_state['values']
will not contain the values for that field, and some validation, or submission handler could report an error (e.g. "the value for [name of the field] must be entered").If you want to add a CSS class to a form field, you can use:
This is the simpler, and faster way. If you need to wrap more than one form field, then you should use something similar to the following code:
In such cases, you normally want to add a CSS style to the form, which is done with code similar to the following one:
You could also move the form fields in a
#container
form field, as with the following code:In this case, the form field will be wrapped with a
<div>
tag with the CSS class set for the container. The con with this is that the fields are moved from where they were; you need to adjust their weight to make then appear where they were before. If you use this method, you should be sure your module is the last one to change the form, or modules that expect to find$form[$field_id]
will have some problems; this doesn't apply to the form handlers, except when$form[$field_id]['#tree']
is set toTRUE
.