繁体   English   中英

使用Drupal 7在alter上的主题表单

[英]Theming forms on alter with Drupal 7

我在使用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);      
}

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

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

一旦做出更改,清除了Drupal的缓存,您的代码应该可以工作了(我已经测试了上面的内容,并且工作正常)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM