Flexifield table formatter
chipway-drupal - August 5, 2008 - 08:27
| Project: | Flexifield |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs review |
Description
Hi,
I guess that it will be a great module.
I would like to be able to have table display for flexifield embedded fields (one line per embedded CT and one column per embedded CT field). Do you think it could do this ?
I tested and found not working with content_taxonomy (but it is still a proof of concept).
Thank you

#1
Thank you very much for the request. It's very helpful to get ideas of what people are interested in.
I expect this to be possible. The next thing I'm working on is the theming approach, but I plan on being able to have theming functions / tpl files able to control how the containing field / contained fields are themed in both view mode and edit mode.
I'll also look into whether formatters can be configured so that rendering as a table could be configured entirely through the interface and not require knowledge of how to work with theming functions / tpl files.
I don't have an estimate on when the theming / formatters will be ready. I'll be actively working on it, since we need it for a project my company is working on, but I also have other work unrelated to this module that I need to work on.
#2
It's good to hear this. A table-like theme as CwDrup proposal could be achieved converting field label to "table headers" reducing redundancies (repeated labels for each field). Another interesting case for a "table" theme could be the text field option widget handling. Each option could be returned as a table row under a common header, displaying each label one time.
A fixed header on scrolling like admin/build/modules on Drupal 6.x should be fine with an increasing number of elements. (@CwDrup: I've opened an issue for Content Taxonomy elements not showing).
#3
Hello,
Is this "table-like" appearance implemented?
This is a great module, but you can understand that if you have a bunch of fields to enter something as simple as a number, then it's appearance in edit mode is not very convenient!
Has anyone found any way to work around this?
Thanks!
#4
I would also really like to see this feature included.
Thanks for the module, it is great.
Leo
#5
Since effulgentsia is not very active, I don't submit this as patch but instead as standalone module.
This code is maybe ugly, and "needs work" but setting it as "needs review" to get more attention. Please try and review.
Some notes:
#6
Thanks for small but great add-on module to FlexiField.
I currently testing it, and had made some useful correction, maybe it make sense for other peoples over here.
Imagen situation when your flexifield-field displayed as table, and your have all table data for some column empty, maybe better to hide this column at all. Well, that is simple.
Replace this lines (starting from line 57 in flexifield_table.module )
<?php
// Skip hidden field columns
if ($formatter_name != 'hidden') {
if (!isset($header[$column])) {
$header[$column] = check_plain(t($field['widget']['label']));
}
$rows[$row][$column] = $cell;
}
?>
With this :
<?php
if(isset($cell)) /* do not set header-text if cell empty */
{
// Skip hidden field columns
if ($formatter_name != 'hidden') {
if (!isset($header[$column])) {
$header[$column] = check_plain(t($field['widget']['label']));
}
$rows[$row][$column] = $cell;
}
}
?>
E.g. - one additional check before, and a closing bracket after.
If anything wrong in my post style.. correct me please, because I am newbie here.
#7
Why would you want to skip cells like that ? What if on one row same column is empty and on another it's not ? Header will be set up anyway.
There could be use cases to show empty columns. And as it was said in #5 there is setting to hide column so adding additional code seems unneeded.
#8
I was getting the following error:
To make it work I changed line 43 from:
<?php
flexifield_item_invoke('view', $aItem, $aElement['#node']);
?>
to this:
<?php
_flexifield_field_invoke('view', $aItem, $aElement['#node']);
?>
Hope someone finds this helpful. :-)
#9
The label for each item (same as the column headers) was showing in every cell (which is annoying)...
to eliminate the label from showing in every cell I had to do add a couple of lines:
<?php
/**
* Theme function for table flexifield formatter.
*/
function theme_flexifield_table_formatter_table($aElement) {
//variables added for label removal -zeno129 ***
$find = '';
$replace = '';
$result_str = '';
$output = '';
$header = array();
$rows = array();
foreach (element_children($aElement) as $row) {
// Invoke 'view' on the item, and let the item and content element created by
// the view operation have access to the node.
$aItem = $aElement[$row]['#item'];
if (!isset($aItem['#node'])) {
$aItem['#node'] = $aElement['#node'];
}
// Setting up display context. We need it to be able to select formatters of individual fields,
// and to hide table columns with hidden fields
$context = 'flexifield_table';
$aElement['#node']->build_mode = $context;
_flexifield_field_invoke('view', $aItem, $aElement['#node']);
$aContentElement = $aItem['value']['content'];
if (!isset($aContentElement['#node'])) {
$aContentElement['#node'] = $aItem['#node'];
}
foreach (element_children($aContentElement) as $column) {
$field = content_fields($column, $aContentElement[$column]['#type_name']);
$cell = drupal_render($aContentElement[$column]); //this line sets each field -zeno129 ***
// Check formatter for every field in row
$formatter_name = isset($field['display_settings'][$context]) && isset($field['display_settings'][$context]['format']) ? $field['display_settings'][$context]['format'] : 'default';
// Skip hidden field columns
if ($formatter_name != 'hidden') {
if (!isset($header[$column])) {
$header[$column] = check_plain(t($field['widget']['label'])); //this line sets the name for each column -zeno129 ***
}
//$rows[$row][$column] = $cell; //original line
//additions: -zeno129 ***
$find = $header[$column]; //this gets the label we want to remove -zeno129 ***
$result_str = str_replace($find . ':',$replace,$cell);
//$result_str = trim($result_str); //Trim doesn't seem to work to eliminate the 2 spaces at the beginning of each item -zeno129 ***
//$result_str = substr($result_str,2); //Cannot use substr because there is a div before where the label used to go -zeno129 ***
$rows[$row][$column] = $result_str;
}
}
}
if (!empty($rows)) {
// Theme multiple flexifield items as table
$output = theme('table', $header, $rows);
}
return $output;
}
?>
I wasn't able to eliminate the 2 spaces that appear right before the item... if anyone figures this out, please let me know.
I've attached the module with the changes I made for anyone who wants it.
#10
This is cool. Thanks for taking the initiative to work on it. I've been busy on other issues, but I would like to get something like this rolled into the project before moving to beta releases.
Clarification in response to comment #8. Alpha4 of flexifield had the function named "_flexifield_field_invoke". With alpha5 (released a few minutes ago), the function is "flexifield_item_invoke".
#11
One more note:
You can theme output of each field in the table same way as any CCK field: using template files like "content-field.tpl.php" (exact filename depends on your usecase - see CCK field theming documentation). Default template produces lots of trash markup code which is unneeded in Flexifield use case so you can make your own templates and remove the trash.
This may change, depending on how effulgentsia actually implements formatter code, but atm this works.
#12
Subscribing. I've just started using this, and it's working fine.