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?

Remaining tasks

User interface changes

API changes

Data model changes

Comments

Arla created an issue. See original summary.

Arla’s picture

Issue summary: View changes

Adding proposed resolution

MegaChriz’s picture

In 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.

Arla’s picture

Thanks 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.

twistor’s picture

Title: Custom value as source » Allow targets to provide a default value.

Would using the default value handling that core fields already have work for this?

Arla’s picture

Using 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.

MegaChriz’s picture

Issue tags: +feeds tamper

Tagging with "feeds tamper". As soon as that module has been ported, the request in this issue can probably be accomplished with that module.

apmsooner’s picture

Since 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.

MegaChriz’s picture

@apmsooner
I think that to be able to use fields on the feeds entity as source would be an useful addition!

john.money’s picture

Thanks @Arla for #4. Starting from that base, was able to create a utility TextStripTags class.

MegaChriz’s picture

Status: Active » Closed (works as designed)

Feeds Tamper can now handle this use case.

juampynr’s picture

In 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.