Skip to main content
Category:

Example How to Create Drupal8 Node Programmatically that contains two media fields


$node = Node::create([
		  // The node entity bundle.
		  'type' => 'article',
		  'langcode' => 'en',
		  'created' => $created_date,
		  'changed' => $created_date,
		  // The user ID.
		  'uid' => 1,
		  'moderation_state' => 'published',
		  'title' => $title,
		  'field_article_section' => array('target_id'=>$section_target_id),
		  'field_author' => 111,
		  'field_article_main_image' => array('image' => [
			  'target_id' => $file->id()
			  ],
			  'field_media_in_library'=> 1
			),
			
		  'field_article_main_video' => array(
		  	  'field_media_video_embed_field' => $youtubeID,
			  'field_media_in_library'=> 1
			),	
		 
		  'field_article_body_summary' => [
			'summary' => substr(strip_tags($text), 0, 100),
			'value' => $text,
			'format' => 'rich_text'
		  ]
		]);
$node->save();

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook