Drupal 未定义的索引#模板中的帐户_预处理_用户_配置文件()

Drupal 未定义的索引#模板中的帐户_预处理_用户_配置文件(),drupal,drupal-7,drupal-theming,Drupal,Drupal 7,Drupal Theming,我试图覆盖Drupal 7中的用户配置文件编辑表单,并不断收到以下错误消息: 注意:未定义索引:#模板中的帐户_预处理_用户_profile()(第189行/var/www/menit/modules/user/user.pages.inc)。 注意:未定义的索引:#rdf_preprocess_user_profile()中的帐户(/var/www/menit/modules/rdf/rdf.module的第578行)。 注意:正在尝试获取user_uri()中非对象的属性(第190行/var

我试图覆盖Drupal 7中的用户配置文件编辑表单,并不断收到以下错误消息:

注意:未定义索引:#模板中的帐户_预处理_用户_profile()(第189行/var/www/menit/modules/user/user.pages.inc)。
注意:未定义的索引:#rdf_preprocess_user_profile()中的帐户(/var/www/menit/modules/rdf/rdf.module的第578行)。
注意:正在尝试获取user_uri()中非对象的属性(第190行/var/www/menit/modules/user/user.module)。
注意:尝试获取rdf_preprocess_user_profile()中非对象的属性(第603行/var/www/menit/modules/rdf/rdf.module)。
注意:尝试获取rdf_preprocess_user_profile()中非对象的属性(第604行/var/www/menit/modules/rdf/rdf.module)

我所做的是编写一个自定义模块,其中包含以下代码:

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',
    ),
  );
}
并将以下用户_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>


我现在有点迷路了,而且时间不多了。有人知道我在这里做错了什么吗?

问题在于您使用的以下行:

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

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

  • 如果要删除某些表单字段,可以实现
    hook\u form\u ID\u alter()
    ,这是您已经实现的钩子,并使用它隐藏一些表单字段

    $form[$field_id]['#access'] = FALSE;
    
    这样,该字段将不会为用户显示。我建议使用该方法,因为与未设置($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>';
    
    $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]);
    
    在这种情况下,表单字段将被一个
    标记包装,并为容器设置CSS类。这样做的缺点是,田地从原来的地方移走了;你需要调整它们的重量,使它们出现在原来的位置。如果使用此方法,则应确保您的模块是最后一个更改表单的模块,否则期望查找
    $form[$field\u id]
    的模块将出现一些问题;这不适用于表单处理程序,除非
    $form[$field\u id]['#tree']
    设置为
    TRUE


非常感谢阿尔贝托,他现在很有魅力
$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]);