2011-12-06 57 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