Problem/Motivation
Some values need to be the same for every piece of content to be imported. One way of achieving this is by using the Feeds Tamper plugin "Default value". However, this will not suffice in these cases:
- When you have multiple feeds to import and the "default" value differs per feed.
- You want your editors that are allowed to create feeds to specify the default value themselves.
A part of this functionality is already available. It is however not fully implemented yet. It is already possible to add fields to a feed type, but these are not selectable as mapping sources yet.
Proposed resolution
Complete the implementation of the FeedsSource plugin type.
Remaining tasks
Add a test for importing from an entity reference field.- Include improvements suggested in #24.
Original report by PunamShelke
Hi,
I am working on this feature, very soon i will update the patch...
Needs suggestions
The scenario is - I needs to import content under content type Faq so for that I have created one csv with question and answer only..
But I have some other entity reference fields under faq for this, I created same entity reference fields under the feeds type (Manage Fields) and i am importing faq csv, that should be import along with reference field value.
For This -
- Created Faq Content type with some entity reference fields, question and anwser field
- Created Feeds Type and same entity reference field as having in faq
- Mapping fields created under the feeds type with faq fields
- Created csv with question and answer value
- On feed import page filling the import form and uploading csv file
- After import, faq getting created with all the data provided in csv as well as terms
| Comment | File | Size | Author |
|---|---|---|---|
| #28 | interdiff-2911491-27-28.txt | 4.66 KB | megachriz |
| #28 | feeds-source-plugin-support-2911491-28.patch | 12.91 KB | megachriz |
| #27 | interdiff-2911491-26-27.txt | 4.83 KB | megachriz |
| #27 | feeds-source-plugin-support-2911491-27.patch | 10.67 KB | megachriz |
| #26 | interdiff-2911491-15-26.txt | 4.56 KB | megachriz |
Comments
Comment #2
punamshelkeComment #3
megachrizThis is a useful feature request. It has been requested for the D7 version as well: #1074662: Inherit properties from parent feed node (taxonomy, author, OG, language).
Steps to implement this:
hook_feeds_parser_sources_alter()in the D7 version of Feeds.Comment #4
megachrizA perhaps better approach would be using a new type of plugin, called FeedsSource (to mirror this with FeedsTarget plugins). There is already one such plugin available in the Feeds code base, but I don't think it is currently used.
Comment #5
punamshelkeHi,
Yes your are right FeedsSource is their and i am using the same.....
its working prefect,
I need to check for some cases and it will be complete...
Comment #6
punamshelkeHi,
Here is the patch for mapping the fields under the managed field of feed type....
Currently, We have to add machine name of field manually and its working fine...
This is the field I have created under the manage fields under feeds Type

Mapping of newly created field

Comment #7
punamshelkeComment #8
megachrizA source value from an item is allowed to be empty.
Could you implement this feature as a FeedsSource plugin? I think that a start for this feature was made in \Drupal\feeds\Feeds\Source\BasicFieldSource. I see that
\Drupal\feeds\Entity\FeedType::getMappingSources()already tries to instantiate Feeds source plugins.Comment #9
punamshelkeI have implemented the changes, This one is modified patch, Now all fields under feeds it will come in source drop-down under mapping tab..
Comment #10
punamshelkeI have implemented the changes, This one is modified patch, Now all fields under feeds it will come in source drop-down under mapping tab..
Comment #11
punamshelkeThis is the final patch including SourceFields and mapping of that fields..
Comment #12
punamshelkeComment #14
megachrizThe code should ask the source plugin for the value.
And
$item->get($source)is allowed to return an empty result. So it shouldn't ask the source plugin based on the condition of an item returning an empty result.I think the prefix 'Feed: ' for the label should be preserved, to make clear it comes from a field on the feed entity.
Field names don't have to start with 'field'. They can have any name. It's only if you add fields via the UI they start with 'field_' by default.
Comment #15
megachrizThis patch might do the trick. I have not tested if data from the feed entity is processed correctly, I just had a use case for using a custom FeedsSource plugin.
Comment #16
punamshelkelooks good...,
Comment #17
sealionking commentedwould you please update the release with these patches? @MegaChriz
Comment #18
megachriz@sealionking
It would be a good idea indeed. Have you tested if values from fields on the feed type are properly mapped? I did not test that yet.
Comment #19
sealionking commentedno,when I patched it, my site encountered unexpected problem when I go to mapping page
I don‘t know how to patch it properly.
I just patched the patch below mannually.
https://www.drupal.org/files/issues/feeds-source-plugin-support-2911491-...
Comment #20
nanc2 commentedAfter patch #15 the mapping page not working.
Is there any other way to map the custom fields? I tried "add new source" but didn't work.
Comment #21
andypostThere's something with class hierarchy somehow
Looks plugin should know the type of feed, but I could be wrong
I think plugin should know which
$feedit bounded, same time it looks strange that you pass source togetSourcePlugin()and later the same source to plugin method.Guess feed & source could be protected properties in plugin
instead t() netter use new TranslatableMarkup() for upcoming test coverage
docblock also needs update
Comment #22
nanc2 commentedSo is it possible to use the feed type fields as mapping source?
I am not a programmer and I have spent a lot of time trying to find a solution. I can easily do this with Drupal 7.
What is the right way to use add "New source..." ? I tried to use the feed type fields machine name but it didn't work.
Comment #23
megachriz@nanc2
If the patch doesn't allow you to use feed type fields as mapping sources, then indeed it is not possible right now.
@andypost
I'm trying to understand your comments.
What do you mean with this? The static method
sources()from a source plugin already receives the feed type and based on that it can return a list of sources or not.A source plugin can deliver multiple sources. When
getSourceElement()is called, it needs to know for which source it should return a value.$this->t()should be used.getSourceElement()is missing docs for the parameter$item.Here is a sample implementation of a Feeds source plugin. This source plugin is used to set default values for some fields (the main source doesn't deliver values for these) and to set a value based on an other value. It delivers three sources and it only delivers them for feed types which name starts with 'product'. The fields that these sources are mapped to come from the RNG module.
You see that for 'study:registration_type' and 'study:min_registrants' it always delivers the same value. For 'study:max_registrants' it calculates a value.
The solution could also have been implemented with Feeds Tamper, but that module wasn't ready yet when above code was written. I made more source plugins, but the one above is the simplest example.
Comment #24
andypostI mean that
\Drupal\feeds\Entity\FeedType::getSourcePlugin()is not part of interface & not documented.As I see it creates new plugin object depending on source argument & keep ref to feed type
So this source could be stored in plugin instance like
feed_typestored in plugin configcould be changed to
This way plugin instance will always know from which type it created and for which source
Comment #25
andypostSo the code above will become
Comment #26
megachrizWorked on a fix and test for this issue. Improvement suggestions from #24 not yet included. I hope to look at that later.
The test checks if a custom text field on the feed type can be used as source. It would be good to have another test for an entity reference field. So this still needs work.
I updated the issue summary as well.
Comment #27
megachrizAnd a test for a taxonomy reference field.
Comment #28
megachrizThis implements the suggestion from #24. Thanks @andypost for your suggestion.
Comment #30
megachrizCommitted #28.