When I am writing the UI for a module, often times I need to display the information in a table. Not a problem with theme_table. There are also times where that table needs to contain form API elements such as weights, buttons, textfields and so on. Currenly, one has to create the form, then set the #prefix and #suffix properties on particular elements, set the class of necessary rows and the table.

In order to simplify this process, I have written a function (form_table) that allows the developer to create form tables similar to the way you call theme_table. form_table will even handle the drupal_add_tabledrag for you (if the config is provided as a table attribute).

I've attached the source, example and a sceenshot of the example output. I'm looking forward to see if this is useful to anybody else.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tutumlum’s picture

I liked it. I was looking for something similar. I am starting to use it now for my new-developing modules :)
Thanks a lot...

tutumlum’s picture

Version: 6.x-dev » 7.x-dev
catch’s picture

Priority: Minor » Normal
Status: Needs review » Needs work

Please attach this as a patch rather than a tarball. Also have you looked at the tableselect property in Drupal 7? Not sure how much that might cover what you're doing here.

vangorra’s picture

Status: Needs work » Needs review
FileSize
10.69 KB

Here is the patch to be applied against includes/form.inc. I was thinking of replacing $attributes on theme_table with $options['attributes'], so more config options can be passed to the function cleanly. What do people think? Any other suggestions?

Status: Needs review » Needs work

The last submitted patch failed testing.

vangorra’s picture

Status: Needs work » Needs review
Issue tags: +patch, +table, +form, +form_table, +drupal7
FileSize
10.56 KB

Sorry, I didn't include the relative path in the patch.

kika’s picture

Issue tags: -patch, -form, -drupal7

Sorry for junk followup, but do we need all these junk tags? At least "D7", "form" and "patch" are totally unneccessary.

vangorra’s picture

Probably not, I'm not familiar with the preferred tagging convention.

Frando’s picture

Status: Needs review » Needs work

I didn't look at the patch in much detail, but for this to be considered it really has to be a regular form element that can be used by setting the #type (or #theme) property on an element. And it should be merged with tabeleselect, for sure. Also, please correct the coding style (see our coding standards), e.g. Drupal doesn't do tabs nor @author doxygen tags.

Edit: See also #297893: #theme Form elements into tables and #80855: Add element #type table and merge tableselect/tabledrag into it. I smell duplicates..

vangorra’s picture

Status: Needs work » Needs review

After reviewing form_tableselect, I can see where merging form_table and form_tableselect would be beneficial. In doing so, the features of the function would no longer reflect the function name. I propose the creation of a "formtable" element. The theme_formtable function would render each column that is a form element. See element example below:

Any suggestions on how this could be improved?

theme_formtable element

$el = array(
	//attributes of the table can be set in this way
	'#attributes' => array(
		'id' => 'mytable-id',
		'class' => 'mytable',
	),
	//the header can handle form_table style arrays or FAPI elements
	'#header' => array(
		'Check', 
		array(
			'data' => 'CH2',
			'class' => 'CH2-head',
		),
		'ch3-head-field' => array(
			'#type' => 'checkbox',
			'#title' => 'CH3',
			'#default_value' => false,
		),
	),
	//each child element is handled as a table row
	'row1' => array(
		//attributes of the row can be defined here
		'#attributes' => array(
			'class' => 'red-row'
		),
		//each child of the row is a column
		'col1' => array(
			'#type' => 'checkbox',
			'#default_value' => true,
		),
		//columns can be form_table style arrays or FAPI elements
		'col2' => array(
			'data' => 'Column 2',
			'class' => 'Column2',
		),
		'col3' => array(
			'#type' => 'textfield',
			'#default_value' => 'Column 3',
		),
	),
	'row2' => array(
		'#attributes' => array(
			'class' => 'green-row'
		),
		'col1' => array(
			'#type' => 'checkbox',
			'#default_value' => false,
		),
		'col2' => array(
			'data' => 'Column 2',
			'class' => 'Column2',
		),
		'col3' => array(
			'#type' => 'textfield',
			'#default_value' => 'Column 3',
		),
	),
);
moshe weitzman’s picture

Status: Needs review » Closed (duplicate)

as frando said, please work at #80855: Add element #type table and merge tableselect/tabledrag into it. perhaps improve upon drumm's file there.

vangorra’s picture

Status: Closed (duplicate) » Needs review
FileSize
7.17 KB

Here is the patch that creates a new FAPI element called tableform. The following example demonstrates how to use the element. Please review it and provide me with any feedback how this can be made acceptable.
Thanks.

	$form = array(
		'table' => array(
			'#type' => 'tableform',
			'#header' => array('col1', 'col2', 'col3'),
			'#caption' => 'Hello',
			'#tabledrag' => array(
				'action' => 'order',
				'relationship' => 'sibling',
				'group' => 2,
				'hidden' => false,
			),
		),
	);
	
	for($i = 0; $i < 10; $i++) {
		$form['table']["row-$i"] = array(
			'col1' => array(
				'#type' => 'checkbox',
				'#default_value' => rand(0,1),
			),
			'col2' => array(
				'#type' => 'checkbox',
				'#default_value' => rand(0,1),
			),
			'col3' => array(
				'#type' => 'select',
				'#default_value' => 0,
				'#options' => array_combine(range(0,10),range(0,10)),
			),
		);
	}

Status: Needs review » Needs work

The last submitted patch failed testing.

Bevan’s picture

Status: Needs work » Needs review
Issue tags: +Needs usability review, +Usability
vangorra’s picture

FileSize
7.39 KB

Here is a patch based on CVS. This should pass the test. Again, here is an example on how to utilize the element type.

    $form = array(
        'table' => array(
            '#type' => 'tableform',
            '#header' => array('col1', 'col2', 'col3'),
            '#caption' => 'Hello',
            '#tabledrag' => array(
                'action' => 'order',
                'relationship' => 'sibling',
                'group' => 2,
                'hidden' => false,
            ),
        ),
    );
   
    for($i = 0; $i < 10; $i++) {
        $form['table']["row-$i"] = array(
            'col1' => array(
                '#type' => 'checkbox',
                '#default_value' => rand(0,1),
            ),
            'col2' => array(
                '#type' => 'checkbox',
                '#default_value' => rand(0,1),
            ),
            'col3' => array(
                '#type' => 'select',
                '#default_value' => 0,
                '#options' => array_combine(range(0,10),range(0,10)),
            ),
        );
    }
vangorra’s picture

Bevan, what sort of usability tests do you recommend? How can I help?

vangorra’s picture

Has anybody had a chance to review this patch? I'd really like to see it get included in drupal7.

seb192’s picture

Version: 7.x-dev » 6.13

hi
how i can install the patch?
thanks

catch’s picture

Version: 6.13 » 7.x-dev

@seb192 Please don't change versions back to Drupal 6 without a very good reason, that just makes patches drop out of the active (7.x) development queue.

@vangorra, the patch currently has a lot of code style issues (tabs etc.) which makes it harder to review. It'd be worth looking at http://drupal.org/coding-standards and/or running http://drupal.org/project/coder on it - if you can fix that up you're likely to get more meaningful reviews. You're also more likely to get reviews if you post your patch to #80855: Add element #type table and merge tableselect/tabledrag into it and close this issue as duplicate - since there's more discussion there, and several potential reviewers have already posted.

vangorra’s picture

Status: Needs review » Closed (fixed)

@catch Thank you for the feedback, that was very helpful. I have styled the codebase and submitted the patch to the suggested thread. I look forward to your feedback in the new thread.

The project is not dead, just closing this thread in favour of:
http://drupal.org/node/80855#comment-1981750

dropcube’s picture

Status: Closed (fixed) » Closed (duplicate)