I am trying to override the theme_fieldset function in a custom node edit form where the fieldset is added dynamically.
Here is what my code looks like:
function cms_content_edit(){
$node = node_load(3089);
module_load_include('inc', 'node', 'node.pages');
$form = drupal_get_form('test_node_form', $node);
$form['block1'] = array(
'#type' => 'fieldset',
'#title' => t('Block 1'),
'#weight' => -50,
'#collapsible' => true,
'#collapsed' => true,
'#attributes' => array('class' => array('portlet box blue-hoki')),
'#theme' => 'fieldset_custom3',
);
$form['block1']['title'] = $form['title'];
unset($form['title']);
$form['block1']['body'] = $form['body'];
unset($form['body']);
$form['block1']['field_fichier'] = $form['field_fichier'];
unset($form['field_fichier']);
$output = drupal_render($form);
return $output;
}
The custom function is defined this way in my .module file:
function theme_fieldset_custom3($variables) {
$element = $variables['element'];
element_set_attributes($element, array('id'));
_form_set_class($element, array('form-wrapper'));
$output = '<fieldset' . drupal_attributes($element['#attributes']) . '>';
if (!empty($element['#title'])) {
// Always wrap fieldset legends in a SPAN for CSS positioning.
$output .= '<div class="portlet-title">' . $element['#title'] . '</div>';
}
$output .= '<div class="fieldset-wrapper">';
if (!empty($element['#description'])) {
$output .= '<div class="fieldset-description">' . $element['#description'] . '</div>';
}
$output .= $element['#children'];
if (isset($element['#value'])) {
$output .= $element['#value'];
}
$output .= '</div>';
$output .= "</fieldset>\n";
return $output;
}
Unfortunately my custom function is not called at all.
Did I do something wrong?
******** EDIT *********
Thanks to @kiamlaluno, after I added my custom function to hook_theme(), my custom function was actually loaded.
function mymodule_theme() {
return array(
'fieldset_custom3' => array(
'render element' => 'element',
),
);
}
The problem now is that the .fieldset-wrapper div is empty. In my custom function, the $element['#children'] array is empty...
My fieldset is created dynamically, like that (in hook_form_alter()):
if(isset($form['#node_edit_form']) && $form['#node_edit_form']==TRUE && $form['nid']['#value']!=''){
$form['block1'] = array(
'#type' => 'fieldset',
'#title' => t('Block 1'),
'#weight' => -50,
'#collapsible' => false,
'#collapsed' => false,
);
$form['block1']['title'] = $form['title'];
unset($form['title']);
$form['block1']['body'] = $form['body'];
unset($form['body']);
$form['block1']['field_fichier'] = $form['field_fichier'];
unset($form['field_fichier']);
$form['block1']['#theme'] = 'fieldset_custom3';
// $form['actions']['submit']['#submit']['0'] = 'node_form_submit_custom';
}
I also tried this way (this is a custom page):
function cms_content_edit(){
$node = node_load(3089);
module_load_include('inc', 'node', 'node.pages');
$form = drupal_get_form('test_node_form', $node);
$form['block1'] = array(
'#type' => 'fieldset',
'#title' => t('Block 1'),
'#weight' => -50,
'#collapsible' => true,
'#collapsed' => true,
'#attributes' => array('class' => array('portlet box blue-hoki')),
'#theme' => 'fieldset_custom3',
);
$form['block1']['title'] = $form['title'];
unset($form['title']);
$form['block1']['body'] = $form['body'];
unset($form['body']);
$form['block1']['field_fichier'] = $form['field_fichier'];
unset($form['field_fichier']);
$output = drupal_render($form);
return $output;
}
But the problem is the same.
******** EDIT 2 *********
I also noticed that the issue also happens when I try to use the default theme_fieldset() function as configuration:
$block1 = array(
'#type' => 'fieldset',
'#title' => t('Block 1'),
'#weight' => -50,
'#collapsible' => true,
'#collapsed' => false,
'#description' => t('Edit the welcome e-mail messages sent to new member accounts created by an administrator.'),
'#tree' => TRUE,
'#theme' => 'fieldset',
);
$form['block1'] = $block1;
$form['block1']['body'] = $form['body'];
I double checked and the theme_fieldset() function (/includes/form.inc) is actually well called, but the fieldset is not generated as expected. Apparently something is missing so Drupal can do the job. May be the $variables object.
Here is a screenshot of what my form looks like with this configuration.
When I don't use the "#theme" config, everything is perfect:
$block1 = array(
'#type' => 'fieldset',
'#title' => t('Block 1'),
'#weight' => -50,
'#collapsible' => true,
'#collapsed' => false,
'#description' => t('Edit the welcome e-mail messages sent to new member accounts created by an administrator.'),
'#tree' => TRUE,
);
$form['block1'] = $block1;
$form['block1']['body'] = $form['body'];
theme_fieldset()
: You are using a different theme function. – kiamlaluno♦ Jun 15 '16 at 3:38