dongyongkui6329 2011-12-30 01:16
浏览 19
已采纳

随着Drupal 7改变形式

I'm having issues customizing a radio button for a specific form using hook_theme. Below is the piece of code I have on my module; see my comments elaborating the issue I encounter:

// 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);      
}
  • 写回答

1条回答 默认 最新

  • douli8428 2011-12-30 02:05
    关注

    Themes for Drupal 7 form elements need to use the new render array key instead of variables in the theme definition:

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

    Once you've made the change clear Drupal's caches and your code should work (I've just tested the above and it works fine).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度