views:

192

answers:

2

I need to show matrix like this http://eugen.gotdns.com/test/zeitplaner.png in content profile form. I can arrange checkboxes with CSS in this way, but i need also some labels on the top and on the left. Should i work with $form array to add some labels? Can this be done with css maybe?

A: 

Although you could theoretically do this with CSS, it would be a browser-compatibility nightmare.

Your best bet is to modify the $form array to make it into a <table>. You could do this either using prefixes and suffixes on the individual elements (for the opening and closing of td's and tr's), or you could just add #markup elements in between the form fields for that. Either way would work, but I'd go the prefix/suffix direction just because it would be a lot cleaner.

If you need help implementing this, feel free to ask here (probably in a different question).

Mike Crittenden
Can i use prefix for it? Description: Define a string that should be prefixed to the value, like $ or €. Leave blank for none. Separate singular and plural values with a pipe (pound|pounds).I tried place there <b>, but i did not found it in html
EugenA
i got it. I mean #prefix in the $form array. ok, ok. :-)
EugenA
i placed table, tr and td tags in #prefix and #suffix of the $form array. I see the tags in the array. But there are no such tags in the html output if i create new node.
EugenA
i'm using content_profile module
EugenA
+2  A: 

I agree with Mike Crittenden that the way to do this is to use tables, I don't agree with the way he want to do it though.

The best solution is to make a custom theme function for the form. Basicly what you do it to loop through your form items and render them. The easiest is to render them one table row at a time.

I have copied some old code that shows this example, but you probably need to modify it some. I had some uncertainties not knowing which row and columns I would need, So I have made this a bit more generic that you will need, and sent some data with the form when I generated it.
You can render your form items any way you want, it also depends on how you structured your form items how you can loop through if even possible. The code below is based on form structure like this: $form['category']['item'] = checkbox. If you have structured your form items differently you will need to loop through them different. What you will need to do however, id to render the form items one row at a time. $rows will be an array of array, where each array in it, will be the rows of your matrix and should in order contain the elements in that row.

When you render a form item Drupal will know that it has been rendered, so when you use drupal_render($form) only the form items that hasn't been rendered will be rendered like submit buttons etc.

function theme_form_id($form) {
    $labels = array(
      ...
    );
    $cats = $form['categories']['#value']; // This would for you be Mo, Di ...
    $pros = $form['profile_items']['#value']; // array of name the form items have.
    $header = array('Ziet' ...);
    $rows = array();

    foreach ($pros as $item) {
        $new_row = array($labels[$item]);
        foreach ($cats as $cat) {
            $new_row[] = array('data' => drupal_render($form[$cat][$item]), 'class' => 'checkbox');
        }
        $rows[] = $new_row;
    }
    $output = theme('table', $header, $rows);
    $output .= drupal_render($form);
    return $output;
}
googletorp
+1, this is much cleaner and more logical than my solution.
Mike Crittenden
hm.. 1) $cats - it is not realy clear, where from do i get 'mo', 'di' and so on? 2) $pros.. what is it? I don't really understand this code completely.. I need some explanation, please
EugenA
This is my $form: http://pastebin.com/urtVwXxF I'd like to place the 'field_1', 'field_2' in the matrix
EugenA
From what I can tell, that's not a form, but a node. Anyways, a form that simple wont sum up to 4.7k lines of code to in a pastebin.
googletorp
it is in $form var, so i thought it is form. In the code i cannot see how $form is filled. I think i should begin here http://api.drupal.org/api/drupal/developer--topics--forms_api.html/6
EugenA
There is the question of where do you get the form? This is clearly not something you have defined yourself. If your goal is to create a simple matrix, you should create the form yourself, and in the submit handler, handle how the data should be saved. What is it exactly you are trying to do? From what you pasted, this isn't just a simple matrix $form.
googletorp
The matrix should be the part of large form with many cck fields.
EugenA
Create the matrix part of the form yourself, then you will know what form items you will have to render in the table. Handle the saving of the data in the submit handler.
googletorp
how drupal knows where to store values for dynamically generated fields?
EugenA
i think it was better idea to use already created checkboxes and only place them in a table
EugenA
so, how can i render single field if i have this array: http://pastebin.com/urtVwXxF
EugenA
now i rendered my checkboxes in a table. But i see same checkboxes one more time without table at the bottom, why?
EugenA

related questions