By selimoezkan on
Hi there,
I stuck with form element theming.
In my form I need radio buttons which indicate a particular colour
and so I thought of wrapping each radio-input form field with
a table cell with the background of whatever "yellow, green, blue, ...".
I tried this:
$colours = array(
'red',
'green',
'blue',
...
);
...
function colour_theme() {
return array(
'colour_radios' => array(
'arguments' => array('element' => NULL),
),
);
}
function colour_form($form, &$form_state) {
global $colours;
...
$form['favouritecolour'] = array(
'#type' => 'radios',
'#title' => t('Your favourite colour'),
'#options' => $colours,
'#theme' => array('colour_radios'),
);
}
The form shows all radiobuttons listed up each one below the other - ok.
But how do I manage to pass the necessary form element to this funtion in my .module-File?
function theme_colour_radios($element) {
$keys = array_keys($element['#options']);
$type = $element[$keys[0]]['#type'];
foreach ($keys as $key) {
$output .= '<td style="background: '.$key.'">';
$output .= theme('colour_radios', $element[$key]);
$output .= '</td>';
}
return $output;
}
$element should countain $form['favouritecolour'], but it doesn't.
Any advice on this?
Many thanks
Soezkan
Comments
#theme do not need to be an
#themedo not need to be an array.Try this
'#theme' => 'colour_radios'What's new and changing in PHP 8.4
Hi Ayesh,
Hi Ayesh,
thanks for your reply.
I tried
'#theme' => 'colour_radios'. It makes no differencesince the whole:
... is not called at all. It makes no difference if I indicate it in my .module-File, or leave it. It has no effect on the output.
Maybe the funtion is at the wrong place? Should it be somewhere in the themes folder?
Many thanks
Soezkan
You need to add a render
You need to add a render element definition in your theme declaration, otherwise your theme callback will never be called.
Try like this:
Great catch!
Great catch!
you can also use `render element` form, to get parent stuff as well.
What's new and changing in PHP 8.4
Thank you for your reply.
Thank you for your reply.
Hm, what a pity, but I still can't manage to give a background colour to the radios.
This is the code I try to work with - and as I am new with module development, I believe I forgot something important.
Any idea? It seems theme_colour_radios($element) is never been called.
Many thanks
Soezkan
Your theme_colour_radios is
Your theme_colour_radios is called but your implementation is not really correct. The theme definition of the render element is element which means that all data passed as argument is available within the $variables['element'].
Here is an example of a more correct implementation which could help you further.
Many many thanks!
Many many thanks!
I could solve it with your help.
Some things I added and now it works (added the global $colours-Array containing all hexdec-colour codes and changed the $options parameter in the formatting attributes of the table cell to an array-index, see below):
The only last thing I wonder is: How can I remove the hexdec codes from within the table cells beside the radiobuttons?
I only need the background colour and the value of the radio.
http://www.selimoezkan.com/files/images/snap_002.jpg
Thanks again.
Bests
Soezkan
I solved it now like this:<
I solved it now like this:
Now I have a new issue: How
Now I have a new issue: How can I put the #default value of a radio button (namely "selected")
after submitting my form?
I tried with global $form_state, but nothing delivered.
Any ideas?
Many thanks
Soezkan
As you said, now you have a
As you said, now you have a new issue. New issues deserve new threads.
Contact me to contract me for D7 -> D10/11 migrations.
You're right.
You're right.
Here is the new post:
https://drupal.org/node/2216091
Many thanks
Soezkan