In paragraphs_field_widget_form_build() field_attach_form() is called directly with $element. But this may lead to unforeseen behaviour. A concrete problem is, that there is an $element['#entity'] attached, that is the host entity of the given field in that moment. When field_attach_form() is called, the Drupal Field API replaces that with the pargraphs entity in some cases: @see field_default_form().

I suggest that the attached form, should be placed in $element['subform']. I'm currently investigating that stuff, as I think that provides some problems when storing the delta values or weight.

Comments

jeroen.b’s picture

Is this actually giving issues?
Do you have any steps to reproduce this or do you just suspect a problem with it?

duaelfr’s picture

I have an issue for which I am not able to find the source and that could be caused by this kind of things.

I deployed a content type with a few paragraphs on my stage server then created some example nodes using these paragprahs.
My users started to edit the nodes and play with them.
Later, I deployed more paragraphs styles and let my users continue playing with their content.
A few days later, they fill me an issue explaining that image upload is not working anymore on their nodes.
Digging a bit, I find that somehow, a few paragraphs has been duplicated in their host entity. They are nested paragraphs so I can see in the database two paragraphs containing the same sub-paragraphs.

Here is a "schema" to explain a bit more:

- Content type: page
- Paragraph field: field_paragraphs (unlimited, can contain 'columns' paragraphs)
- Paragraph type: columns
- Paragraph field: field_columns (unlimited, can contain 'column_item' paragraphs)
- Paragraph type: column_item
- Text field: field_title
- Image field: field_image

At start:
I had a 'page' node (nid=1) containing one column (item_id=1).
I had a 'columns' paragraph (item_id=1) containing two column_items (item_id=2,3)

A few days ago:
I have a 'page' node (nid=1) containing two columns (item_id=1,4).
I have a 'columns' paragraph (item_id=1) containing two column_items (item_id=2,3)
I have an other 'columns' paragraph (item_id=4) containing two column_items (item_id=2,3)

I have no idea about what happened and I don't know is it is related but I think it could be.
I hope my case will give you a clue on this.

derhasi’s picture

That is the behaviour I started researching the code for. In code there is not obviously a way to figure that out, because Form API and Field API do a lot of work in obfuscating the widget behaviour. I have to take a deeper look in that issue today, and might start with a fresh prototype for a more solid form value storage.

jeroen.b’s picture

@derhasi, any news on this?

jeroen.b’s picture

derhasi’s picture

Not yet, I'm looking forward on working on it next week.

jeroen.b’s picture

Also please check if this is still an issue after #2271845

seanb’s picture

I can reproduce this issue the following way:

  • Create a normal paragraph bundle with some field.
  • Create another paragraph bundle with a field collection.
  • Create a node type that uses both bundles.
  • Create a node with the following bundles in a specific order:
    1. Add the normal paragraph bundle
    2. Add the paragraph bundle with the field collection
    3. Add another paragraph with the field collection
  • Save the node.
  • Open the node again and delete the normal paragraph.
  • Now there are only 2 paragraphs left in the node, try to edit the second paragraph with a field collection. This second paragraph item contains the field collection items of the first paragraph.

Everything goes as planned untill field_attach_form is called. After that I'm not sure where it goes wrong. I will investigate some more, but hopefully this helps to reproduce the issue.

*edit: Yeah, this is still an issue after #2271845

seanb’s picture

It looks like there are some references in the form state which are not updated after deleting a paragraph item or something. Like it looks for the field values in their old delta nr. I have no time left today, but will try to check again later.

seanb’s picture

Priority: Major » Critical

Making it critical, since it can cause data loss.

derhasi’s picture

I will have a look at it today. We seam to still have some issues with this.

derhasi’s picture

jeroen.b’s picture

In the D8 version I don't use $element directly for the entity, everything is in a $element['subform'] there, maybe we can base a fix on that?

The issue you mentioned is because of node clone and having a reference to the same paragraph item.

jeroen.b’s picture

@seanB, I can't reproduce anymore after fixing #2410931.
Is it possible that it's already fixed or is there still some other issue with directly using $element?

jeroen.b’s picture

Status: Active » Needs work
derhasi’s picture

Assigned: Unassigned » derhasi
manu manu’s picture

Hi,

It seems that I experience a problem related to this issue, in the context of nested Paragraphs, using the latest dev.

Given:
A node having a paragraph field field_content that can embed a container paragraph bundle.
The container paragraph bundle have a field_content field.

The problem:
When editing the node and creating a container in field_content, editing a field of this container will make paragraphs_field_widget_form_build() use the node as host entity (from $element['#entity']) instead of the container bundle. Later, when extracting ids, the passed entity is not the right one:

EntityMalformedException: Missing bundle property on entity of type paragraphs_item. in entity_extract_ids() (line 7848 of /vagrant/www/includes/common.inc). 
Backtrace:
entity_extract_ids('paragraphs_item', Object) ParagraphsItemEntity.inc:205
ParagraphsItemEntity->setHostEntity('paragraphs_item', Object, 'und', ) paragraphs.field_widget.inc:410
paragraphs_field_widget_form_build(Array, Array, Array, Array, 'und', Array, 2, Array) paragraphs.field_widget.inc:91
paragraphs_field_multiple_value_form(Array, Array, 'und', Array, Array, Array, 0, Array) paragraphs.field_widget.inc:42
paragraphs_field_widget_form(Array, Array, Array, Array, 'und', Array, 0, Array) field.form.inc:112
field_default_form('node', Object, Array, Array, 'und', Array, Array, Array) field.attach.inc:214
_field_invoke('form', 'node', Object, Array, Array, Array) field.attach.inc:390
_field_invoke_default('form', 'node', Object, Array, Array, Array) field.attach.inc:579
field_attach_form('node', Object, Array, Array, 'en') node.pages.inc:326
node_form(Array, Array, Object) 
[...]

Some people doesn't have issues with nested paragraphs, so I may be wrong somewhere...

seanb’s picture

I can confirm the issue in #17. This only happens when using nested paragraphs. I have sites where this is working fine, so it is probably not easy to reproduce this.

I guess this is related to https://www.drupal.org/node/2564327

eelkeblok’s picture

WRT #17, this is not much more than a hunch, at this point, but I seem to have tracked this down to the i18n_field module. I have a very similar situation to #17 (identical stack trace), except that I have a paragraph field that is in my node as well as in a field collection in the node. The field instance info for the node type's field instance is overwritten at some point by the instance info for the field_collection's instance of the same field. This is causing entity_extract_ids() to choke down the line. I was interested to know whether people who are seeing this behaviour have i18n_field module enabled as well.

eelkeblok’s picture

This indeed seems to be the problem, at least for me. Please see #2685853: i18n_field does not correctly set form_state for nested fields for details. I'd be interested to learn whether this also solves #17. If not, you may want to investigate whether there are any other modules that are corrupting the information about the field in the form_state.

recrit’s picture

tylersamples’s picture

I can replicate this, it doesn't seem to be specific to the i18n_field module.

Structure I have:
Node -> Paragraph -> Paragraph -> Field collection

Somewhere around form_attach_field it's failing to retrieve the correct instance.

eelkeblok’s picture

It's conceivable other modules do something similar to i18n_field. It would be really interesting if you could debug further.

jstoller’s picture

Status: Needs work » Closed (outdated)

In an effort to cleanup the Paragraphs issue queue I'm closing this ticket and marking it "outdated." If you find this is still a problem with the latest Paragraphs 7.x-1.x-dev release than feel free to reopen the issue.