- 🇷🇺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) andquantity
(integer).
3. Csv file has this structure:
***Vendor code***Title***Berries
***n1***Cake 1***Strawberry, 2;Raspberry 11. Create custom module:
paragraphs_import
2. Createparagraphs_import.services.yml
with:services: paragraphs_import.berries: class: Drupal\paragraphs_import\EventSubscriber\Berries tags: - { name: event_subscriber }
3. Create
Berries.php
inparagraphs_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?