Support importing content with referenced paragraphs

Created on 11 June 2018, over 6 years ago
Updated 1 February 2024, 10 months ago

Within my project (a database for natural medicine) I have to store a lot of data with use of paragraphs and in some cases nested paragraphs.

It looks like:

Medical drug
- Various fields
- Rev-Reference to Paragraphs-1 (multiple): "Ingredient / substance" (Content Reference Field) | "Amount" (Text Field) | "Remark" (Text Field)

In other cases I use nested paragraphs

Content-Type
- Various fields
- Rev-Reference to Paragraphs-2: Taxonomy Reference Field | Rev-Reference to Paragraphs-3 (which has also Entity Reference Fields and others)

It would facilitate my work a lot If I could import these data from excisting files via csv import.
And I guess the same kind of structure appear in drupal shop applications maybe using the module Field Collections (which has only minor support in Drupal 8)

If it would be helpful for you we can discuss possible options here.

:-) :-)

Feature request
Status

Active

Version

3.0

Component

Code

Created by

🇩🇪Germany ksc

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇷🇺Russia kazah

    I have some specific use case, as I wrote above in #45 Support importing content with referenced paragraphs Active :

    So how I manage to solve it.
    If someone could provide the easier solution, you're welcome.

    My specification:

    1. Need to create paragraphs at the same time as nodes are imported.
    2. Paragraph has two fields: title (Entity reference) and quantity (integer).
    3. Csv file has this structure:
    ***Vendor code***Title***Berries
    ***n1***Cake 1***Strawberry, 2;Raspberry 1

    1. Create custom module: paragraphs_import
    2. Create paragraphs_import.services.yml with:

    services:
      paragraphs_import.berries:
        class: Drupal\paragraphs_import\EventSubscriber\Berries
        tags:
          - { name: event_subscriber }	

    3. Create Berries.php in

    paragraphs_import/src/EventSubscriber
    

    with:

    ** all comments in code

    <?php
    
    namespace Drupal\paragraphs_import\EventSubscriber;
    
    use Drupal\feeds\Event\EntityEvent;
    use Drupal\feeds\Event\FeedsEvents;
    use Drupal\feeds\Event\ParseEvent;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    
    /**
     * Reacts on articles being processed.
     */
    class Berries implements EventSubscriberInterface {
    
    	/**
    	* {@inheritdoc}
    	*/
    	public static function getSubscribedEvents() {
    		$events[FeedsEvents::PARSE][] = ['afterParse', FeedsEvents::AFTER];
    		$events[FeedsEvents::PROCESS_ENTITY_PRESAVE][] = 'presave';
    		return $events;
    	}
    
    	/**
    	* Act on parser result.
    	*/
    	public function afterParse(ParseEvent $event) {
    		/** @var \Drupal\feeds\FeedInterface */
    		$feed = $event->getFeed();
    		/** @var \Drupal\feeds\Result\ParserResultInterface */
    		$parser_result = $event->getParserResult();
    
    		// Check if this is the feed type we want to manipulate.
    		// For i.e. my feed id cakes
    		if ($feed->getType()->id() !== 'cakes') {
    			// Not the feed type that we are interested in. Abort.
    			return;
    		}
    
    		/** @var \Drupal\feeds\Feeds\Item\ItemInterface */
    		foreach ($parser_result as $item) {
    			/**
    			* Get an item's value. Return result:
    			*
    			* array [
    			*  0 => "Strawberry,2"
    			*  1 => "Raspberry,1"
    			* ]
    			*/			
    			$berries = $item->get('berries');
    			
    			/**
    			* Separate values by comma. Return result:
    			*
    			* array [
    			*  0 => array [
    			*	0 => "Strawberry"
    			*	1 => "2"
    			*  ]
    			*  1 => array [
    			*	0 => "Raspberry"
    			*	1 => "1"
    			*  ]
    			* ]
    			*/
    			foreach ($berries as $key => $value) {
    				$explode_list[] = explode(',', $value);
    			}
    
    			/**
    			* Change title of referenced field with it nid
    			*
    			* array [
    			*  0 => array [
    			*	0 => "2082"
    			*	1 => "20"
    			*  ]
    			*  1 => array [
    			*	0 => "2085"
    			*	1 => "10"
    			*  ]
    			* ]
    			*/
    			$berries_array = array();
    			foreach ($explode_list as $key => $value) {
    				// get node id by title, filtered by type
    				$nid = \Drupal::entityQuery('node')
    				->condition('title', $value[0])
    				->condition('type', 'berries')
    				->execute();
    				
    				// get only first result
    				$value[0] = array_shift($nid);
    
    				// make array global to use in presave function
    				global $berries_array;
    				$berries_array[] = $value;
    			}		
    		}
    	}
    
    	/**
    	* Set corresponding values to paragraph fields.
    	*/	
    	public function presave(EntityEvent $event) {
    		$entity = $event->getEntity();
    		
    		// Load our paragraphs from field berries in node
    		$paragraph_berries = $entity->field_berries->referencedEntities();
    
    		foreach ($paragraph_berries as $key => $p) {
    			// Get global array setted in afterParse function
    			global $berries_array;
    			
    			// match values from array with fields in paragraphs
    			foreach ($berries_array[$key] as $k => $v) {
    				if ($k == 0) {
    					$p->set('field_title', $v);
    				}
    				if ($k == 1) {
    					$p->set('field_quantity', $v);
    				}
    			}
    		}
    	}
    
    }

    4. In Feeds Tamper need to add plugin EXPLODE with ; *semicolumn* to our paragraph.

  • 🇮🇳India darshanb87 Gujarat, Ahmedabad

    Hello @MegaChriz,

    I am on Drupal 10.2.2 version. I am using feeds and paragraph modules to import paragraph data. I followed #2 (option 1) and #3 and indeed its working fine. However, when I checked table node__field_(paragraph_field_name) in database. I find that every time I run the feed import that data are inserting in this table keeping old data as it is. Actual expected behavior should be previous old data should be deleted and only new data should remain in this table. There should be no orphan data in this table which leads increasing database size.

    Anyone else facing this problem in table node__field_(paragraph_field_name)?
    Is there any solution that this should not happen?

  • 🇦🇺Australia interlated

    @irinaz I'm happy to attempt to merge the changes for paragraph references. The changes were pretty large, so what do you think is the best way?

Production build 0.71.5 2024