Empty tables are being rendered

Created on 22 May 2025, 4 months 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

Merge Requests

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 &&
  • 🇩🇪Germany Anybody Porta Westfalica

    Thanks. we just ran into the same issue. If the paragraphs field is empty, for some cases we'd expect to entirely hide the field (and thereby the table).

    Instead the table is shown with a result saying that no entries have been found.

    I'd like to suggest adding two things in the field formatter:

    • Add a setting to hide the table if empty
    • Add a setting to override the "Empty text" (if visible empty)

    That should also target the point from #3:

    whether this would break some other functionality

    The setting should keep existing behaviour so that nothing breaks.

    Furthermore, #3 already takes care of the AJAX mode, which is important.

    @dougiep could you create a MR from your patch posted?

  • 🇩🇪Germany Anybody Porta Westfalica
  • 🇩🇪Germany Anybody Porta Westfalica

    Some more details: Despite the table display I choose, the field is always shown by the formatter, even if empty. Typically empty Drupal fields are hidden by default, for that reason I think we should categorize this as bug.

    Only BootstrapTable falls back to an empty text from the library saying: "No matching records found". See https://examples.bootstrap-table.com/#welcomes/no-data.html

    So from my perspective we should just hide the field like the other formatters do. And if needed, add a checkbox setting for the formatter "Show empty table", but that should then also work cleanly for all table styles. For that reason, I'd vote against the extra work and just fix this case.

  • 🇩🇪Germany Anybody Porta Westfalica

    @dougiep $items->isEmpty() is here for the rescue to check the referenced entities for emptyness cleanly.

    Please review and test MR attached.

  • Merge request !19Check ->isEmpty() before rendering the field → (Closed) created by Anybody
  • 🇩🇪Germany Anybody Porta Westfalica
  • 🇩🇪Germany Anybody Porta Westfalica
  • Pipeline finished with Success
    about 1 month ago
    Total: 132s
    #578267
  • 🇩🇪Germany Anybody Porta Westfalica

    Static patch attached until this is merged.

  • 🇩🇪Germany Grevil

    This seems to be fixed through https://git.drupalcode.org/project/paragraphs_table/-/commit/3d5ae0820aa...?

    @lazzyvn a new release would be nice!

  • 🇩🇪Germany Anybody Porta Westfalica

    @lazzyvn crediting me here would still be fair. Thank you for the fix.

    Please mind the differences, for example for the "Add" button, which I think will not be visible for empty fields any more in the dev implementation?

  • 🇫🇷France lazzyvn paris

    oh this patch for someone interested who wants to add patch to composer.json, it will not merge with module because it will break another functionality, it needs to render div for ajax loading or adding button new,...
    #3 is better than this patch

     public function viewElements(FieldItemListInterface $items, $langcode) {
        // Hide the field entirely, if empty:
        if ($items->isEmpty()) {
          return [];
        }
    
  • 🇩🇪Germany Anybody Porta Westfalica

    @lazzyvn #3 doesn't even use

    $items->isEmpty()
    

    which is IMHO the only correct way to delegate emptiness checking to the entities!
    You also correctly used it in your last commit: https://git.drupalcode.org/project/paragraphs_table/-/commit/3d5ae0820aa...

    So I don't know what you mean. Still, I think crediting would be totally fair, but you're the maintainer. If feedback helped to learn and improve the project, it would be nice to give back.

  • 🇫🇷France lazzyvn paris

    I think you are confused about widget paragraph json. If there is no element, it can return an empty string, no problem. The topic here is widget paragraph table, the patch you create for table widget, you check if there is empty, it will not print anything. Meanwhile, there is more function than that, for example if it is empty, you can add a button to add new paragraph, you can load ajax to add or add a custom twig below to customize.
    Some people like you may not like these customizations, you can do patch, others just copy the patch link and then put it composer.json every time it is updated, it will patch itself to module.

  • 🇩🇪Germany Grevil

    I think you are confused about widget paragraph json. If there is no element, it can return an empty string, no problem. The topic here is widget paragraph table

    Both the patch from #3 and this issue's MR target "ParagraphsTableFormatter" not the Widget. Meaning this issue is fixed through https://git.drupalcode.org/project/paragraphs_table/-/commit/3d5ae0820aa....

    So no need to merge anything. But a new release would be great! 👍

Production build 0.71.5 2024