- Issue created by @dougiep
- 🇦🇺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
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.
- 🇩🇪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 patchpublic 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! 👍