Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
I'm creating a feed type, and I have a target field where I always want the same value and it's not available in the source.
I'm new to Feeds, trying to use it for a case where I need to create nodes from RSS items. The target node type has a required term field which should always be set to a specific value for these RSS-imported nodes. I would like to specify that term in the Mapping form but there I can only choose from a predefined set of RSS fields. Since the field is required (and should probably stay that way, in my case), importing always fails because entity validation fails.
Proposed resolution
- Allow sources to have settings? (So I could subclass SyndicationParser and add a "Custom value" option)
- Use event or alter hook to allow modification after processing but before validation?
Comments
Comment #2
ArlaAdding proposed resolution
Comment #3
MegaChriz CreditAttribution: MegaChriz as a volunteer commentedIn the D7 version of Feeds that could be done with the "Set default value" plugin from Feeds Tamper, but that module hasn't been ported to D8 yet.
In the D7 version of Feeds also you had a hook called
hook_feeds_parser_sources_alter()
, with which you could define additional custom sources (Feeds Tamper implemented this hook also). Not sure if this hook has been ported to the D8 version of Feeds. Some hooks were converted to events. See feeds/src/Event. If you know how the event system works in D8, perhaps you could make use of that.Comment #4
ArlaThanks for the hints. I could actually achieve what I wanted by subclassing the Entity Reference target class with an extra "Default value" setting (https://gist.github.com/arildm/12bcfe44e62763ad9a9e25cbfd015321) and in the mapping form I just leave the source selector empty.
Comment #5
twistor CreditAttribution: twistor as a volunteer commentedWould using the default value handling that core fields already have work for this?
Comment #6
ArlaUsing the field default value is not satisfactory in my use case. I created a feed type to import from various interrelated sources, and I want to set a category term reference to something that represents this source. I have other content using the same category field, but it should not assume the same value by default.
Comment #7
MegaChriz CreditAttribution: MegaChriz as a volunteer commentedTagging with "feeds tamper". As soon as that module has been ported, the request in this issue can probably be accomplished with that module.
Comment #8
apmsooner CreditAttribution: apmsooner commentedSince feed types are entities and fieldable, is there an intent to allow fields from the feed entity itself be mapped? It seems that would then allow a default value pulled directly from the feed entity. I could see a good example being an entity reference field on the feed that references a company node. Lets say the feed is designed to import nodes of type "contact" which itself has an entity reference to a company node. Newly imported contacts would inherit the company reference from the feed source. I've seen alot of need for this scenario over the course of feeds evolving history which various sub modules have attempted to provide a solution for.
Comment #9
MegaChriz CreditAttribution: MegaChriz as a volunteer commented@apmsooner
I think that to be able to use fields on the feeds entity as source would be an useful addition!
Comment #10
john.money CreditAttribution: john.money at Acquia commentedThanks @Arla for #4. Starting from that base, was able to create a utility TextStripTags class.
Comment #11
MegaChriz CreditAttribution: MegaChriz as a volunteer commentedFeeds Tamper can now handle this use case.
Comment #12
juampynr CreditAttribution: juampynr at Lullabot commentedIn case it helps, I did this the following way for a field that was not present in the feed data but I needed to set a value for it.
1. Download and install Feeds Tamper.
2. Edit a Feed's mappings.
3. Scroll to the bottom. At the target column, select the field that you want to set. Wait for the Source column to update.
4. At the source column, select "New xpath source" and enter anything that make clear that is just for this field. For example, if the field name is field_myfield, then call this myfield_default_value
5. Save your changes, then click on the Tamper tab at the top.
6. Find myfield_default_value at the bottom and add a "Set value or default value" plugin with the configuration that you need. Save.
7. Test the new mapping by deleting feeds items and importing them again.