One of the fields in my main content type has a potential for multiple values, but most of the time it is empty. What I've noticed lately when editing content is that over time, the number of blank values increases. So dev load shows something like this:

I don't even want to think about how to track down a problem like this, but am interested in how to fix it. It's mostly an issue because when people go to edit the node, this creates a lot of clutter on the form.

Accepted answer

I've made a change and pushed it to the repo - which I believe fixes the issue.

Most helpful answers

I've created a small module that implements hook_node_presave() that removes the last empty item from long text fields with unlimited cardinality. Simply by removing the last empty item, the empty items are not saved.

I called it "Long-text tribbles" in honor of the self-multiplying, annoying tiny creatures from the original Start Trek Series 😄

It's in my repo. I can move to backdrop-contrib if people find it helpful.

https://github.com/argiepiano/long_text_tribbles

Yup, that's a well known bug that has not gotten fixed. 

https://github.com/backdrop/backdrop-issues/issues/5153

Unfortunately there is no current fix. As a temporary fix, you may want to use hook_form_alter() to add a custom submit handler, and basically remove the extra elements from that field array, or implement hook_node_presave() to do the same thing before the node is saved. 

 

Comments

Yup, that's a well known bug that has not gotten fixed. 

https://github.com/backdrop/backdrop-issues/issues/5153

Unfortunately there is no current fix. As a temporary fix, you may want to use hook_form_alter() to add a custom submit handler, and basically remove the extra elements from that field array, or implement hook_node_presave() to do the same thing before the node is saved. 

 

Oh, oops. 

As a temporary fix, you may want to use hook_form_alter() to add a custom submit handler, and basically remove the extra elements from that field array, or implement hook_node_presave() to do the same thing before the node is saved.

Do you mean in a custom module?

Also, any thoughts on how to track down my problem in https://forum.backdropcms.org/forum/how-fix-problem-trimmed-output

Thank you!

I've created a small module that implements hook_node_presave() that removes the last empty item from long text fields with unlimited cardinality. Simply by removing the last empty item, the empty items are not saved.

I called it "Long-text tribbles" in honor of the self-multiplying, annoying tiny creatures from the original Start Trek Series 😄

It's in my repo. I can move to backdrop-contrib if people find it helpful.

https://github.com/argiepiano/long_text_tribbles

Wonderful! I will try it out soon.

Tribbles is the perfect name! :)

So I'm getting some warnings:

Warning: Undefined array key "und" in long_text_tribbles_node_presave() (line 16 of /var/www/html/backdrop1/modules/custom/long_text_tribbles/long_text_tribbles.module).
Warning: foreach() argument must be of type array|object, null given in long_text_tribbles_node_presave() (line 16 of /var/www/html/backdrop1/modules/custom/long_text_tribbles/long_text_tribbles.module).

It does remove the empty field elements ... including the 0'th element. Is that intentional? It looks fine on the node edit form.

I'll take a look. In the meantime there is a PR for the bug. Leeksoup, it would be great if you could test the PR - you can find it in my bug report link above. 

Will do!

Thanks for the quick fix, also!

I've made a change and pushed it to the repo - which I believe fixes the issue.

Can you please add this comment in the issue queue? That will help the developer who created the PR. The comment is no good here. 

I already did add a comment at the PR conversation. Was that the wrong place?