Account created on 2 September 2010, about 14 years ago
#

Recent comments

🇷🇺Russia kazah

Sorry, the problem was in Views Ajax History module.

Everything works.

🇷🇺Russia kazah

Just create your custom module as usual and paste that code in module file.

🇷🇺Russia kazah

Hello, @liquidcms!
Look at my last post: https://www.drupal.org/project/feeds/issues/2978757#comment-15161715 Support importing content with referenced paragraphs Active

You can modify it for your own easy.

🇷🇺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.

🇷🇺Russia kazah

Thank you!

I'm waiting for the release of the boost to compare.

🇷🇺Russia kazah

Will boost provide better perfomance than warmer module?

🇷🇺Russia kazah

Yes, you are right.

Thank you in advance.

🇷🇺Russia kazah

For those who need to show thumbnails in preview (for already uploaded images):

1. use this patch:

https://git.drupalcode.org/project/webform_dropzonejs/-/merge_requests/3/diffs

2.
Navigate to /webform_dropzonejs/src/Plugin/WebformElement/WebformDropzonejs.php

Declare your style, i.e. thumbnail: $style = \Drupal\image\Entity\ImageStyle::load('thumbnail');

Replace 'path' => $file->getFileUri()), to 'path' => \Drupal::service('file_url_generator')->transformRelative($style->buildUrl($file->getFileUri())),

🇷🇺Russia kazah

Still errors of unknown namespaces...

🇷🇺Russia kazah

1.
To solve this
 

{{ drupal_view('myview', 'block_1') }}

I found that one views field with aggregated option breaks cache metadata. I remove it and now it works.

2.
I solve this issue:

{{ drupal_entity('webform', 'myform') }}

by adding lazyload to webform block (or you can install last version of webform).

🇷🇺Russia kazah

Thanks for the answer.

I just wanted to speed up the output of the module as best I can.

I used boost on Drupal 6 and it worked very well despite a bunch of settings.

Now Drupal 9 lacks this module.

Thanks for your work.

Production build 0.71.5 2024