2011-12-06 54 views
0

我试图覆盖用户配置文件编辑表单在Drupal 7,和我不断收到以下错误信息:未定义指数#帐户

注意:未定义指数:#帐户中template_preprocess_user_profile()(/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> 

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

回答

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

    在此CA se,表单字段将用包含为容器设置的CSS类的<div>标签打包。与此相对应的是,田地从他们原来的位置移开;你需要调整自己的体重以使之出现在之前的位置。如果你使用这种方法,你应该确保你的模块是最后一个改变窗体的模块,或者期望找到$form[$field_id]的模块会有一些问题;这不适用于表单处理程序,除非$form[$field_id]['#tree']设置为TRUE

+0

非常感谢Alberto,像现在的魅力一样 – Eytyy