Forgive me if I'm being dense. I've read all the open and closed issues regarding using Feed Items (Feeds item: Item GUID) to specify and update unique nodes. However, from what I can tell, this just sets a unique GUID upon import, which can then be used for future updating. If you are trying to update an existing node that was not created in this same way, no GUID exists and consequently the node cannot be updated via Feeds.

In D7 you can target the node ID, which exists for all nodes regardless of how they were created. Is there a way of doing this in D8? Or does the GUID exist for all nodes and I am just unaware of it?

Thanks for any info. Also, after having read these many issues I must say thanks for all the many hours and years of time spend on this module. It's much appreciated!

Thanks,
Mike

Comments

mxwright created an issue. See original summary.

megachriz’s picture

Title: Updating existing nodes » Add a target to entity ID
Category: Bug report » Feature request

@Mike
A GUID value for an entity only exists when you inserted or updated that entity with Feeds and when you mapped to Feeds Item GUID. Or when you programmatically set a value for it.

Besides the GUID, other targets can be set as unique as well. Targets that generally can be set as unique are text fields, urls, email fields and telephone fields. I believe there is a feature request to allow number fields to be set as unique target as well.

There isn't a target for node ID indeed. I'm not sure how hard it will be to add that one. Let's turn this into a feature request for adding that.

argogi’s picture

@mxwright did you ever solve this? facing the same issue, hoping to update entities with the only unique fields being photos and entity_id.

mxwright’s picture

@argogi no unfortunately I haven't had the time, nor frankly the D8 chops, to tackle this. For me it's an almost essential part of Feeds that's still missing. I'm hoping to take a closer look soon.

back from 7’s picture

This is a feature that is needed. Old D7 uses NID as references to other content.

carolpettirossi’s picture

I would definitely use this feature if it was available.

I need to update some existing content created via UI. I've tried to add target to URL alias, but the Unique flag is not available.

Any ideas on how can I contribute? Is it difficult to add this target?

Thanks,
Carol

megachriz’s picture

@carolpettirossi
Firstly, you need to create a new FeedsTarget plugin and figure out how it can set the ID of an entity. An extra challenge is that the ID of existing entities cannot be changed. So this should somehow be clarified in the user interface. And if someone tries to change the entity ID anyway, the import should fail.
It would be great that if someone maps to entity ID that "unique" is ticked by default.

So these requirements do make this feature harder to implement than other field types. Because it is easy for users to make errors with this target. And these errors should thus be catched and explained to the user. It would be a plus if errors can be prevented early on in the user interface, for example that the mapping form wouldn't allow to tick out the "Unique" checkbox for the entity ID target.

Great that you want to give it a shot.

megachriz’s picture

Assigned: Unassigned » megachriz

Working on this...

megachriz’s picture

Status: Active » Needs review
StatusFileSize
new5.56 KB

I figured out that Feeds doesn't display a target to entity ID, because Drupal has marked it as read only and because Feeds skips read only fields.

So this patch in fact exposes all read only fields, including entity ID. But I've made it so that Feeds only sets a value when the field in question doesn't have one yet. Because else I found out that you could run into the following error:

Error: Call to a member function getValue() on null in Drupal\Core\Entity\Plugin\Validation\Constraint\ValidReferenceConstraintValidator->validate() (line 128 of core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/ValidReferenceConstraintValidator.php).

This is because Feeds always tries to empty targets first. And emptying entity ID apparently isn't allowed.

The patch depends on the patch from #2829283-82: Language support in Feed import.
And you'll probably also need the patch from #2918222: Use Number fields as unique mapping target in order to make this one useful. Without that patch you cannot set entity ID as unique target.

jamesdixon’s picture

StatusFileSize
new4.63 KB

#9 failed to apply in latest 8.3.x dev so I rerolled it.

megachriz’s picture

@jamesdixon
Thanks for helping, but it looks you forgot to upload the patch file. ;)

jamesdixon’s picture

StatusFileSize
new5.46 KB

Ah made mistake uploading two files. Here we go!

mxwright’s picture

Haven't tested this latest patch yet, but in #9 two other patches were referenced - it looks like those have since been committed.. So we should be good to test this patch alone on the latest dev?

megachriz’s picture

@mxwright
Yes, this patch can be tested on top of the latest dev. The other two mentioned issues are indeed done and their patches are in dev now.

In #2829283-100: Language support in Feed import an issue related to importing aliases was reported by @qdoscc:

Alias is not set (but I expected this as feeds showed it as Read only on the mapping... is there another way?)

On which I responded with:

I wasn't aware that it would also expose path alias as read only field. Read only fields can only be set once as else that causes issues for entity ID. Sounds like that needs more refinement. But you can import aliases with a separate feed type, using the URL alias processor (I hope, did not test that myself). Since D8.8 a path alias became a separate entity type in Drupal core, thus as a result you could no longer import it in one go with nodes.

So that implies that this patch could introduces issues for importing path aliases. I know the Pathauto module marks the path alias field as read-only, so I assume that module must be installed for the issue to appear.

jamesdixon’s picture

Status: Needs review » Reviewed & tested by the community

This patch allows mapping to read only fields. It was tested to map to the read only name field for Entity Queues successfully:

#3106819: Error while importing Node Queues in D8

jamesdixon’s picture

Status: Reviewed & tested by the community » Needs work

Megachriz gave us some direction on what is needed next:

@jamesdixon Thanks for checking, the next step for that issue is:
Automated tests.
Check how this affects importing paths - using the node processor. I thought this was no longer possible since Drupal 8.8, but maybe I’m wrong about that.

megachriz’s picture

Status: Needs work » Needs review
StatusFileSize
new20.34 KB
new15.58 KB

This patch adds the following:

  • Kernel tests for importing values for entity ID:
    • Tests entity ID's can be set upon create.
    • Tests that entities can be updated by ID.
    • Tests that entity ID's cannot change afterwards.

    And these tests also check that there are no SQL related warnings in the process.

  • In entitySaveAccess(), the entity owner does not exist during kernel tests when their ID is 0. Therefore the invalid user warning is not triggered when the entity owner ID is 0.
  • When setting an ID for a new entity, Feeds checks first if an entity with that ID doesn't exist already to avoid SQL "duplicate entry" errors.
  • On the mapping form, "unique" gets ticked by default when adding a mapper to entity ID (though only when javascript is enabled).
  • When saving the mapping form, a warning gets displayed when the mapper to entity ID does not have "unique" ticked. But the form does not invalidate, it's only a warning. This is because there is a use case where you don't want to set that target as unique. For example when you want to set an ID, but never want to update it by ID.
  • Functional tests for the mapping form UI enhancements related to the entity ID mapping target.
  • On the mapping form, a summary is displayed even if the target does not have a plugin form.

Next step: check how this patch affects importing aliases.

megachriz’s picture

StatusFileSize
new20.36 KB
new375 bytes

I forgot to add a test group for the kernel test "EntityIdTest".

megachriz’s picture

StatusFileSize
new21.93 KB
new1.57 KB

I think that this patch fixes the issue for when importing paths.

You do need the patch from #3163927: Add supporting of pathauto when using the Pathauto module.

megachriz’s picture

StatusFileSize
new21.89 KB

Reroll (required after committing #3163927: Add supporting of pathauto).

govind.maloo’s picture

Status: Needs review » Reviewed & tested by the community

Hi @MegaChriz

This is working as expected. I am ale to import data now. This also fixed the issue https://www.drupal.org/project/feeds/issues/3106819

We can merge this now.

tanubansal’s picture

Yes its working fine and can be moved to RTBC

  • MegaChriz committed a0f3a95 on 8.x-3.x
    Issue #2989279 by MegaChriz, jamesdixon, tanubansal, govind.maloo: Added...
megachriz’s picture

Status: Reviewed & tested by the community » Fixed

Thanks for testing! Committed #20 with coding standards issues fixed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.