随着Drupal 7改变形式

Mar*_*vzz 1 php forms drupal drupal-7 drupal-theming

我在使用hook_theme为特定表单定制单选按钮时遇到问题.下面是我模块上的代码; 看到我的评论阐述了我遇到的问题:

// 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);      
}
Run Code Online (Sandbox Code Playgroud)

Cli*_*ive 7

Drupal 7表单元素的主题需要使用新render array键而不是variables主题定义:

function mymodule_theme() {
  return array(
    'custom_radios' => array(
      'render element' => 'element',
    ),
  );
}
Run Code Online (Sandbox Code Playgroud)

一旦你做了改变,清楚Drupal的缓存和你的代码应该工作(我刚刚测试了上面的内容,它工作正常).