By selimoezkan on
Hello all,
I use the following function in my custom module which gives me a table where one can select a favourite colour. It actually works well:
http://www.selimoezkan.com/files/images/snap_2.jpg
The only thing: How can I put the selected attribute to the according option-Tag after submitting the form, so that the selected value keeps selected.
This is the code:
function theme_colour_radios($variables) {
global $colours;
$counter = 0;
$element = $variables['element'];
$output = '<tr>';
foreach ($element['#options'] as $option => $value) {
$counter++;
$output .= '<td style="width: 57px; text-align: center; background-color: #'.$colours[$option].';">';
$output .= drupal_render($element[$option]);
$output = preg_replace('/\>[0-9a-fA-F ]+\<\/label\>/', '></label>', $output);
$output = preg_replace('/name\="favouritecolour" value\="[0-9]+"/', 'name="favouritecolour" value="#'.$colours[$option].'"', $output);
if($counter % 12 == 0) $output .= '</tr><tr>';
}
$output .= '</tr>';
return $output;
}
Many thanks for help.
Best wishes
Soezkan
Comments
It will happen in your form
It will happen in your form definition, not your form theme. What does your form definition look like?
Contact me to contract me for D7 -> D10/11 migrations.
Hi Jaypan,
Hi Jaypan,
this is my .module:
Do I have to touch personality_form($form, &$form_state)?
Many thanks
Soezkan
Change this:
Add this:
To this:
Then add this to your submit function:
Contact me to contract me for D7 -> D10/11 migrations.
Thanks. But it doesn't seem
Thanks. But it doesn't seem to work yet.
This is what I have changed in my function personality_form($form, &$form_state) :
This was already in my submit function:
Any ideas?
I found that even if "hard coded" like:
It remains without a selected one.
Mayn thanks
Soezkan
What is in $colors?
What is in $colors?
Contact me to contract me for D7 -> D10/11 migrations.
$colours is this:
In the $colours array are all the hexdec colours for the table selector:
Just found that the "problem" can be solved the dirty way like this:
Added the ...
... in the function theme_colour_radios.
There is probably a more coherent way to do this. But it seems to work.
Many thanks
Soezkan