Importing Paragraph Content with Feeds in Drupal 7

As I write this, there isn’t an established way to import data into Paragraphs using Feeds.

This solution only caters to my particular fields, but it should serve as an example of one way to import HTML content into a basic “textarea” paragraph.

Goal

My goal was to import HTML content into a Basic page content type as a basic textarea paragraph item. The original data is from a Drupal 7 typical “body” field, and I want to import the data into a paragraphs-based body field.

Environment Overview

My site has a Basic page content type with a paragraphs field (multi-value) having machine name field_s_paragraph and label of Body. This field allows for Basic Paragraph paragraphs. Basic Paragraph is a paragraph bundle with a single textarea field having machine name field_para_body. Here’s what this looks like:

2016-02-02_13-37-22

Feeds Configuration

I’ve created a feed to import Basic page nodes. This feed uses the Node processor, configured to “Update existing nodes.”

Using the target defined in the code below, I’ve assigned the “body” field in my source data to the target Basic paragraph for field_s_paragraph. As I said earlier, this is specific to my particular needs. You should change as needed.

twentyfifteen_guid is just the node ID from the 2015 website. We’re importing into the new 2016 website.

Screenshot_2016-02-02_14-58-20

How It Works

Feeds will temporarily store the value from the source field (body in my case) as paragraph_basic_temp using the newly-defined target. When we process new items we check for a value in that field. If there is one, we do some cleanup of the HTML, then create a new Basic Paragraph whose textarea contains the HTML. Lastly, we associate this new entity with the parent Basic page node.

When we re-process (update) nodes, we also check if the paragraph_basic_temp field exists. If it’s not empty, there are paragraphs in the node already, the first paragraph is a Basic Paragraph, and that paragraph is not the same as what we already have, we update the first paragraph entity to use the new HTML. This is specifically how I needed this to operate. You may need to adjust according to your needs.

Code

Here’s the code to make all of this happen:

 

Tagged with: , , ,
Posted in Development
3 comments on “Importing Paragraph Content with Feeds in Drupal 7
  1. Phil B says:

    Thank you for your example it has helped me a ton! I have my implementation working, but am curious why mymodule_create_attach_field_s_paragraph_basic_paragraph() does need to return $node.

    It seems like the value of $node is being updated properly after the function is called but I don’t see anywhere it being passed by reference, so am confused how that part is working.

    Thank you again.

  2. Phil B says:

    I think I figured it out, field_attach_update(‘node’, $node); saves the paragraph to an existing node, so returning it to hook_node_insert isn’t necessary.

    Thanks again!

  3. alex vier says:

    hmmmmm,

    does it make a difference if you would use ‘hook_feed_presave’ instead of ‘_node_update’ and ‘_node_insert’?

    the outcome would be the same right?
    just having all the stuff in one function instead..

Leave a Reply

Your email address will not be published. Required fields are marked *

*