Problem/Motivation
When using TableField with multiple allowed values (e.g., allowing 3 tables per field), empty tables are still rendered in the frontend. For example, if a user only fills data in the first table but leaves the other allowed values empty, all three tables are displayed - one with content and two completely empty tables.
This creates unnecessary markup in the page output and can be confusing for end users who see empty table structures with no meaningful data.
The module already has a simple check with !empty($table->value)
but this only verifies that the table structure exists, not that it contains actual data. A table can be structurally defined but still be functionally empty (all cells empty except for special fields like 'weight').
Steps to reproduce
- Enable the TableField module
- Create a content type with a TableField that allows multiple values (e.g., set "Allowed number of values" to 3)
- Add content with data only in the first table, leaving the other tables empty
- View the content - all three tables will be rendered, including the two empty ones
Proposed resolution
Add a new helper method isTableDataEmpty()
that recursively checks if a table contains any non-empty values, ignoring special fields like 'weight'. This method returns TRUE if the table is functionally empty, and FALSE if it contains any data.
Then modify the condition in viewElements()
method to use this helper method alongside the existing check:
if (!empty($table->value) && !$this->isTableDataEmpty($table->value)) {
// Process and render the table
}
The implementation of isTableDataEmpty()
is as follows:
/**
* Checks if the table data is empty, ignoring specific fields like 'weight'.
*
* @param array $data
* The table data to check.
*
* @return bool
* TRUE if the table data is empty, FALSE otherwise.
*/
protected function isTableDataEmpty($data) {
foreach ($data as $key => $value) {
// Ignore specific keys like 'weight'.
if ($key === 'weight') {
continue;
}
if (is_array($value)) {
// Recursively check nested arrays.
if (!$this->isTableDataEmpty($value)) {
return FALSE;
}
}
elseif (!empty($value)) {
return FALSE;
}
}
return TRUE;
}
This ensures that only tables with actual content are rendered to the user, improving the frontend display when using multi-value TableFields.
Remaining tasks
- Write tests to verify that empty tables are not rendered
- Update documentation if necessary
User interface changes
Empty tables will no longer appear in the frontend, resulting in cleaner page output when using multi-value TableFields with some empty values.
API changes
None. This is an internal implementation change that doesn't affect the public API.
Data model changes
None. This change only affects rendering behavior and doesn't modify how data is stored.