Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ccchapman opened this issue Mar 19, 2024 · 2 comments
Assignees
Labels

Comments

@ccchapman
Copy link

ccchapman commented Mar 19, 2024

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"
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

  • Craft version: 4.7
  • PHP version: 8.3
  • Database driver & version: PostgreSQL 14.9
  • Plugins & versions: Feed Me 5.4.0
@ccchapman ccchapman added the bug label Mar 19, 2024
@ccchapman
Copy link
Author

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

@ccchapman ccchapman changed the title Mapping to entry field with maintain hiearchy breaks unchanged content feature Mapping to entry field with maintain hierarchy breaks unchanged content feature Mar 20, 2024
@i-just i-just self-assigned this May 16, 2024
@i-just
Copy link
Contributor

i-just commented May 16, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants