Empty tables are being rendered

Created on 22 May 2025, 15 days ago

The viewElements() method creates table structure based on the paragraph bundle configuration rather than checking if there are actual entities to display. This is a departure from the behaviour exhibited by standard drupal paragraphs and therefore should be adjusted.

Table structure is built unconditionally - The code creates $table_header and builds the table structure even when $entities is empty.

To prevent empty tables from rendering, you need to add a check early in the loop:
foreach ($handler["target_bundles"] as $targetBundle) {
if (empty($entities) && empty($setting['ajax'])) {
continue;
}

You would also need to suppress the "Add" button:
$addButton = NULL;
if (empty($entities) && empty($setting['ajax']) && empty($output)) {
return $output;
}

In my opinion there should also be a form based mechanism to suppress the add button.

🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇦🇺Australia dougiep

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @dougiep
  • 🇫🇷France lazzyvn paris

    Could you make a patch?

  • 🇦🇺Australia dougiep

    I have a patch made already that I have applied, but what needs to be investigated is whether this would break some other functionality.

    --- a/src/Plugin/Field/FieldFormatter/ParagraphsTableFormatter.php
    +++ b/src/Plugin/Field/FieldFormatter/ParagraphsTableFormatter.php
    @@ -458,6 +458,11 @@ class ParagraphsTableFormatter extends EntityReferenceFormatterBase {
     
         foreach ($handler["target_bundles"] as $targetBundle) {
    +      // Skip rendering table if no entities exist and AJAX is not enabled
    +      if (empty($entities) && empty($setting['ajax'])) {
    +        continue;
    +      }
    +      
           $table = $table_header = $fields = [];
           $table_rows = $notEmptyColumn = [];
           if ($setting['number_column']) {
    @@ -658,6 +663,12 @@ class ParagraphsTableFormatter extends EntityReferenceFormatterBase {
         }
         $addButton = NULL;
    +    
    +    // Don't show add button if no entities exist and no table was rendered
    +    if (empty($entities) && empty($setting['ajax']) && empty($output)) {
    +      return $output;
    +    }
    +    
         $userRoles = $this->currentUser->getRoles();
         $cardinality = $field_definition->getFieldStorageDefinition()->get('cardinality');
         if ($entityId &&
Production build 0.71.5 2024