I want to theme a checkboxes-element using a custom theme function in my module. However, the options don't show up.
The checkboxes-element in my form:
$form['chk_more'] = array(
'#type' => 'checkboxes',
'#title' => 'Checkboxes Title',
'#title_display' => 'before',
'#default_value' => array(0,2),
'#options' => array(
0 => 'option one',
1 => 'option two',
2 => 'option three',
),
'#theme' => 'testmodule_checkboxes',
);
My implementation of hook_theme:
function testmodule_theme($existing, $type, $theme, $path) {
return array(
'testmodule_checkboxes' => array(
'render element' => 'element',
),
);
}
My custom theme function:
function theme_testmodule_checkboxes($variables){
$element = $variables['element'];
return 'TEST'.theme('checkboxes', $element);
}
The theme function works ('TEST' is displayed), but the options don't show up.
来自 https://www.drupal.org/forum/support/module-development-and-code-questions/2011-05-05/using-theme-to...
使用#theme覆盖theme_checkboxes
我想在我的模块中使用自定义主题功能来主题一个复选框元素。但是,选项不会显示。
我的表单中的复选框元素:
$form['chk_more'] = array(
'#type' => 'checkboxes',
'#title' => 'Checkboxes Title',
'#title_display' => 'before',
'#default_value' => array(0,2),
'#options' => array(
0 => 'option one',
1 => 'option two',
2 => 'option three',
),
'#theme' => 'testmodule_checkboxes',
);
我的hook_theme实现:
function testmodule_theme($existing, $type, $theme, $path) {
return array(
'testmodule_checkboxes' => array(
'render element' => 'element',
),
);
}
我的自定义主题功能:
function theme_testmodule_checkboxes($variables){
$element = $variables['element'];
return 'TEST'.theme('checkboxes', $element);
}
主题功能有效(显示“TEST”),但选项不显示。
我检查了theme_testmodule_checkboxes中的$ element变量,并通过删除checkboxes-element的#theme属性将其与theme_checkboxes中的$ element变量(在form.inc中找到)进行了比较。在我的自定义主题函数中,$ element的#children属性似乎是空的,而它在theme_checkboxes中包含渲染的html 。
我究竟做错了什么?
我自己亲自做的方法 不用上面的方法,,,,而是覆写 theme_checkbox (通过 开启 Theme developer 模块 可以知道应该是覆写这个方法 )
在 sites/all/themes/mygarlandzhutwo/template.php 中
定义了
function mygarlandzhutwo_checkboxes($element) //这个方法是借鉴 theme_checkboxes 方法 ( includes/form.inc 中 )
$class = 'form-checkboxes';
if (isset($element['#attributes']['class'])) {
$class .= ' '. $element['#attributes']['class'];
}
$element['#children'] = '<div class="'. $class .'">'. (!empty($element['#children']) ? $element['#children'] : '') .'</div>';
if ($element['#title'] || $element['#description']) {
unset($element['#id']);
return theme('my_form_checkbox_element', $element, $element['#children']);
}
else {
return $element['#children'];
}
}
然后 再定义 my_form_checkbox_element
function theme_my_form_checkbox_element($element, $value) { //这个方法是借鉴 theme_form_element ( includes/form.inc 中 )
// This is also used in the installer, pre-database setup.
$t = get_t();
$output = '<div class="form-item my_form_checkbox"';
if (!empty($element['#id'])) {
$output .= ' id="'. $element['#id'] .'-wrapper"';
}
$output .= ">\n";
$required = !empty($element['#required']) ? '<span class="form-required" title="'. $t('This field is required.') .'">*</span>' : '';
if (!empty($element['#title'])) {
$title = $element['#title'];
if (!empty($element['#id'])) {
$output .= ' <label for="'. $element['#id'] .'">'. $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
}
else {
$output .= ' <label>'. $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
}
}
$output .= " $value\n";
if (!empty($element['#description'])) {
$output .= ' <div class="description">'. $element['#description'] ."</div>\n";
}
$output .= "</div>\n";
return $output;
}