【问题标题】:Theming forms on alter with Drupal 7使用 Drupal 7 改变主题形式
【发布时间】:2011-12-30 01:16:13
【问题描述】:

我在使用 hook_theme 为特定表单自定义单选按钮时遇到问题。下面是我的模块上的一段代码;请参阅我的 cmets 详细说明我遇到的问题:

// 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);      
}

【问题讨论】:

    标签: php forms drupal drupal-7 drupal-theming


    【解决方案1】:

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

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

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

    【讨论】:

    • 哇,我不知道这个,然后我必须去重新阅读他们的文档。谢谢它的工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    相关资源
    最近更新 更多