craftcms / feed-me

Craft CMS plugin for importing entry data from XML, RSS or ATOM feeds—routine task or on-demand.
Other
287 stars 140 forks source link

Importing multiple child entries into an Entries field uses first child's field values for all remaining children #1227

Closed amphibian closed 1 year ago

amphibian commented 1 year ago

Description

When importing into an Entries field (creating child entries), the mapped Title for each element is correctly saved, but the additional fields (in my case, simple text fields) are not, instead saving the first child entry's data to those fields in all subsequent children.

Of note, the primary element I am importing is a Commerce Product, and the Entries field in question is assigned to that product type.

Field Mapping:

Screenshot 2023-01-31 at 4 33 50 PM

Field Result:

Screenshot 2023-01-31 at 4 34 51 PM

(Titles are correct.)

Content Result:

Screenshot 2023-01-31 at 4 34 35 PM

(2nd and 3rd fields are incorrect.)

Additional info

i-just commented 1 year ago

Hi, thanks for getting in touch. The issue is likely to do with how your feed data is formatted. I believe you’re using something along those lines, and I can replicate this behaviour with this JSON markup:

{
  "entries": [
    {
      "title": "test1",
      "authors": [
        {
          "author": {
            "name": "Person 1",
            "keyname": "person1",
            "bio": "Bio for person 1"
          }
        },
        {
          "author": {
            "name": "Person 2",
            "keyname": "person2",
            "bio": ""
          }
        },
        {
          "author": {
            "name": "Person 3",
            "keyname": "person3",
            "bio": "Bio for person 3"
          }
        }
      ]
    }
  ]
}

but if I switch to:

{
  "entries": [
    {
      "title": "test1",
      "authors": [
        {
          "name": "Person 1",
          "keyname": "person1",
          "bio": "Bio for person 1"
        },
        {
          "name": "Person 2",
          "keyname": "person2",
          "bio": ""
        },
        {
          "name": "Person 3",
          "keyname": "person3",
          "bio": "Bio for person 3"
        }
      ]
    }
  ]
}

then the inner element fields are mapped correctly.

One more thing to mention is that, in this case, keeping all fields you want to map in the feed is important. If I was to remove "bio": "" from Person 2 node, the bio data for Person 3 would get shifted by one (it would be assigned to Person 2, and Person 3 bio would be empty.

I hope this helps you. I’m going to go ahead and close this, but please feel free to reply if you have any further questions.

amphibian commented 1 year ago

Thanks for the reply Iwona. But with this change I am still encountering the bug, and I think it's because we're not quite testing the same thing. My fault, since I realize now I failed to clarify my setup in my initial report.

My "authors" field is not an Entries field, but a Matrix field. Within that Matrix field is a block type which then contains an Entries field and an additional "Role" field (dropdown).

This is why I structured my JSON in this manner:

{
    "entries": [
        {
            "title: "Name of my book",
            "authors": [ <-- Matrix field
              {
                "author": { <-- Entries field
                    "bio": "<p>Delia Gavrus is an Associate Professor in the Department of History at the University of Winnipeg. </p>",
                    "keyname": "Gavrus",
                    "name": "Delia Gavrus"
                },
                "role": "B01"
              },
              {
                "author": {
                    "bio": "<p>James Hanley is a Professor of History at the University of Winnipeg. </p>",
                    "keyname": "Hanley",
                    "name": "James Hanley"
                }
                "role": "B01"
              }
            ]
        }
    ]
}

But even if I remove that extra object layer (which I can, because I'm only associating one author entry inside each block), the duplication of the field values form the first Matrix block remains. Example JSON:

{
    "entries": [
        {
            "title: "Name of my book",
            "authors": [
              {
                "bio": "<p>Delia Gavrus is an Associate Professor in the Department of History at the University of Winnipeg. </p>",
                "keyname": "Gavrus",
                "name": "Delia Gavrus"
                "role": "B01"
              },
              {
                "bio": "<p>James Hanley is a Professor of History at the University of Winnipeg. </p>",
                "keyname": "Hanley",
                "name": "James Hanley"
                "role": "B01"
              }
            ]
        }
    ]
}
i-just commented 1 year ago

Right, sorry, looks like I got confused somewhere along the way. Reopening. And thank you for the example json!

angrybrad commented 1 year ago

Resolved in https://github.com/craftcms/feed-me/pull/1278 for the next FeedMe v4 and v5 release.