【问题标题】:Undefined index #account in template_preprocess_user_profile()template_preprocess_user_profile() 中未定义的索引#account
【发布时间】:2023-03-20 19:06:02
【问题描述】:

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

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

【问题讨论】:

    标签: drupal drupal-7 drupal-theming


    【解决方案1】:

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

    $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]);
      

      在这种情况下,表单字段将用&lt;div&gt; 标记包装,并为容器设置CSS 类。这样做的缺点是田地从原来的位置移走了。您需要调整它们的重量以使它们出现在以前的位置。如果你使用这种方法,你应该确定你的模块是最后一个改变表单的,否则期望找到$form[$field_id]的模块会出现一些问题;这不适用于表单处理程序,除非 $form[$field_id]['#tree'] 设置为 TRUE

    【讨论】:

    • 非常感谢 Alberto,现在像魅力一样工作
    猜你喜欢
    • 2020-09-05
    • 1970-01-01
    • 2012-12-08
    • 2012-09-18
    • 1970-01-01
    • 2017-12-01
    • 2018-03-05
    • 1970-01-01
    相关资源
    最近更新 更多