2011-12-30 105 views
1

我遇到了使用hook_theme爲特定表單定製單選按鈕的問題。以下是我在模塊上的一段代碼;看到我的意見擬訂的問題我遇到:用Drupal 7修改主題表格

// Implementation of hook_form_alter(). 
function mymodule_form_alter(&$form, $form_state, $form_id){ 
    // e.g form id: commerce_cart_add_to_cart_form_u6onPJSgS7pOgw0Tlo7zHy42LTQzbV913taANkYQKTo 
    if (strpos($form_id, 'commerce_cart_add_to_cart_form') !== FALSE) { 
    // Alter add to cart form 
    mymodule_commerce_cart_add_to_cart_form_alter($form, $form_state, $form_id); 
    } 
} 

function mymodule_commerce_cart_add_to_cart_form_alter(&$form, $form_state, $form_id) {  
    // Change the field type to radios. 
    $form['attributes']['field_artwork_ref']['#type'] = 'radios'; 
    // Apply my custom theme for radios. 
    $form['attributes']['field_artwork_ref']['#theme'] = array('custom_radios');  
} 

// Implementation of hook_theme(). 
function mymodule_theme() { 
    return array(
    'custom_radios' => array(
     'variables' => array('element' => NULL), 
    ), 
); 
} 

function theme_custom_radios($variables) { 
    // Custom theme should go here. 
    // However, $variables are empty, print_r gives me "Array ([element] =>)." 
    // I am at least expecting to see my radio element here. 
    print_r($variables);  
} 

回答

7

主題爲Drupal 7表單元素需要在主題定義爲使用新render array密鑰而不是variables

function mymodule_theme() { 
    return array(
    'custom_radios' => array(
     'render element' => 'element', 
    ), 
); 
} 

一旦你所做的改變明確的Drupal的緩存和你的代碼應該工作(我剛剛測試過上面,它工作正常)。

+0

哇我不知道這個,我必須去重讀他們的文檔。感謝它的工作。 – Marvzz 2011-12-30 02:52:54