template_preprocess_user_profile()中未定义的索引#account

时间:2011-12-06 18:53:54

标签: drupal drupal-7 drupal-theming

我正在尝试覆盖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>

我现在有点迷失了,而且时间紧迫。有没有人知道我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

问题是您正在使用的以下行:

$form['#theme'] = 'user_profile';

这些行改变了与表单关联的主题函数,并且它导致调用一些预处理函数,例如[template_preprocess_user_profile()] [1]和[rdf_preprocess_user_profile()] [2]。所有那些被认为是用户配置文件调用的预处理函数都在寻找一些未定义的变量,例如$variables['elements']['#account']

您不使用模板文件来呈现表单。根据您想要实现的目标,您可以使用不同的方法:

  • 如果要删除某些表单字段,请实现hook_form_FORM_ID_alter(),这是您已实现的挂钩,并使用它来隐藏某些表单字段。

    $form[$field_id]['#access'] = FALSE;
    

    这样,该字段将不会显示给用户。我建议使用它,因为它是比unset($form[$field_id])导致其他模块问题少的方法;如果你使用它,$form_state['values']将不包含该字段的值,并且某些验证或提交处理程序可能会报告错误(例如“必须输入[字段名称]的值”)。

  • 如果要将CSS类添加到表单字段,可以使用:

    $form[$field_id]['#prefix'] = '<div class="mymodule-custom-class">';
    $form[$field_id]['#suffix'] = '</div>';
    

    这是更简单,更快捷的方式。如果需要包装多个表单字段,则应使用类似于以下代码的内容:

    $form[$field_id1]['#prefix'] = '<div class="mymodule-custom-class">';
    $form[$field_id2]['#suffix'] = '</div>';
    

    在这种情况下,您通常希望向表单添加CSS样式,使用类似于以下代码的代码:

    $form['#attached']['css'][] = drupal_get_path('module', 'mymodule') . '/mymodule.css';
    

    您还可以在#container表单字段中移动表单字段,如下面的代码所示:

    $form['container_01'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array('mymodule-custom-class'),
      ),          
    );
    
    $form['container_01'][$field_id] = $form[$field_id];
    
    unset($form[$field_id]);
    

    在这种情况下,表单字段将包含<div>标记,并为容器设置CSS类。与此相关的是,田地从他们所在的地方移开;你需要调整自己的体重,然后才能显示出之前的状态。如果您使用此方法,则应确保您的模块是最后一个更改表单的模块,或者期望查找$form[$field_id]的模块会出现问题;这不适用于表单处理程序,除非$form[$field_id]['#tree']设置为TRUE