craftcms / feed-me

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

Mapping to entry field with maintain hierarchy breaks unchanged content feature #1418

Closed ccchapman closed 1 month ago

ccchapman commented 3 months ago

Description

A feed mapping to an entry field which has the maintain hierarchy option enabled breaks the unchanged content feature. In other words, Feed Me will consider the element changed because the "new" content data only contains the leaf.

Expected output
Node `#1` skipped. No content has changed.

Array
(
    [productName] => Vinegar-Champagne Reims
    [productBrand] => Array
        (
            [0] => 30205
        )

    [productCategory] => Array
        (
            [0] => 72444
            [1] => 72487
        )

    [productSku] => X-VIN176C
    [productSize] => 500 ML
)

Processing 1 elements finished in 0.19s
Actual debugging output
productCategory - diff

Array
(
    [0] => Array
        (
            [0] => 72444
            [1] => 72487
        )

    [1] => Array
        (
            [0] => 72487
        )

)

productCategory - existing

Array
(
    [0] => 72444
    [1] => 72487
)

productCategory - new

Array
(
    [0] => 72487
)

Array
(
    [productName] => Vinegar-Champagne Reims
    [productBrand] => Array
        (
            [0] => 30205
        )

    [productCategory] => Array
        (
            [0] => 72487
        )

    [productSku] => X-VIN176C
    [productSize] => 500 ML
)

Processing 1 elements finished in 0.29s

For the time being, I have written a "hack" to workaround the issue.

Expand "hack" ```php Event::on( Process::class, Process::EVENT_STEP_BEFORE_ELEMENT_SAVE, function (FeedProcessEvent $event) { $canonicalElement = $event->element ->find() ->id($event->element->id) ->siteId($event->element->siteId) ->one(); $canonicalCategories = $canonicalElement->productCategory->ids(); if ( array_slice($canonicalCategories, -1) !== $event->contentData["productCategory"] ) { return; } $event->contentData["productCategory"] = $canonicalCategories; }, ); ```

Steps to reproduce

  1. Create two sections: Products and Product Categories (the latter should be a structure)
  2. Create an entry field: Product Category with maintain hierarchy option enabled
  3. Create two entries in Product Categories section, the second one nested under the first
  4. Create Feed Me feed called Products and setup mapping to Product Category field
  5. Process the feed

Additional info

ccchapman commented 3 months ago

Perhaps the contentData needs to be passed through something that will "fill gaps in element" if maintainHierarchy.

i-just commented 1 month ago

Hi, thanks for reporting and clear description! I raised a PR to adjust this.

angrybrad commented 1 month ago

Merged and this will be included in the next release.