Blog

Categories

Authors

Create referenced entities dynamically while importing through Feeds 

Have you ever tried to import a bunch of content into Drupal 7 through Feeds, Feeds import, Node Import, etc? Especially when you have several referenced entities? The only standard solution provided by Entity Reference module  is to create feeds importers for each entity (content types, vocabularies, users) and then upload the referenced entities first and then create the referencing entities.  

An even better solution is provided by the Entity Reference Feeds module – It provides target fields “mapping directly to properties of referenced entities”. However, this has a limitation – it works for a depth of level one only i.e. if you have an Entity A having an entityreference field to Entity B and in turn B has an entity reference field referring to Entity C, it wouldn’t work.  

So what we need is a dynamic solution which should create referenced entities on the fly in a single import of data. Here’s what our team has come up with – Create a single import file for all referenced entities and import them at one go.  

Detailed Use Case: 

1. Suppose you have 3 entities City, Hotel and Room. Room has a field field_hotel which refers to Entity Hotel, and Entity Hotel has a field field_city which refers to Entity City. 

2. Create importers for all the entities – first City, then Hotel and then Room. In mapping page of importer for entity Hotel, you need to select the target as "(Entity reference by Entity label - with import) (field_city:importer:city_importer)" as shown in the screenshot below. Similarly create mapping for entity Room. 

3. Create a single CSV with columns from all three importers and upload it against importer of Entity Room. 

4. When entities of type Room are imported, entities of types Hotel and City also get created on the fly. 

Advantages: 

  1. Most of the importers can create referenced entities only till one level but by applying this patch, we will be able to create referenced entities till n-level.

  2. It works for all types of entities – Nodes, Taxonomies and Users. 

  3. It works for all types of Fetchers – File upload and HTTP Fetch 

  4. It gives you flexibility to control which referenced entities would be updated or replaced. (The options can be set on the respective importer configuration).