Showing posts with label Node::create(). Show all posts
Showing posts with label Node::create(). Show all posts

Wednesday 7 October 2020

How to create node programmatically in drupal 8?

Simple way to create node in Drupal 8 is use of Node::create() function. We are going to explain with an example.

Code structure:

$entity_type = 'node';

$values = [

  'nid' => NULL,

  'type' => 'article', // put the content type machine name here..

  'title' => 'Testing title', // title of the node..

  'uid' => 1, //user id for node publisher or use \Drupal::currentUser()->id() for default logged-in user id..

  'status' => TRUE, // published..

  'field_metatags' => serialize([

        'title' => 'Some title',

        'description' => 'Some description.',

      ]),

];

$node = \Drupal::entityTypeManager()->getStorage($entity_type)->create($values);

$node->save();


OR 


We have another code for node creation…

    $nodeArray = array();

$nodeArray = array(

'type' => 'resources',

'langcode' => 'en',

'uid' => '1', //\Drupal::currentUser()->id(),

'status' => '1',

'title' => $row->title,

'created' => strtotime($row->created),

'path' => [

  'alias' => $row->page_url,

  'pathauto' => PathautoState::SKIP,

]

);

$node = \Drupal\node\Entity\Node::create($nodeArray);        

$node->set('body', ['value' => $row->body, 'summary' => '', 'format' => 'full_html']);

$node->set('field_resources_content_type', $row->field_resources_content_type);

$node->set('field_resources_lob', $row->field_resources_lob);

$node->set('field_resources_vidyard_id', $row->field_resources_vidyard_id);

$node->set('field_instrument_description', $row->field_instrument_description);

$node->set('field_source', $row->source);

$node->set('field_expiration_date', $row->field_expiration_date);

$node->set('field_meta_tags', serialize([

  'title' => $row->field_meta_title,

  'description' => $row->field_meta_description,

  'keywords' => $row->field_meta_keywords,

]));

$document_url = $row->field_resources_asset_tagging;

$extfile_info = basename($document_url);

$filename = explode(".", $extfile_info);

list($ext_name, $extension) = explode(".", $extfile_info);

//$name = $filename[0] . '.' . $extension;

        $name = $extfile_info;

        $savepath = str_replace($base_url.'/sites/default/files/','',$document_url); 

$uploaded_file = file_save_data(file_get_contents($document_url), "public://".$savepath, FILE_EXISTS_RENAME);

$file_url = $uploaded_file->getFileUri();

$fid = $uploaded_file->get('fid')->value;

if (!empty($uploaded_file)) {

    $node->set('field_resources_asset_tagging', ['target_id' => $fid]);

} else {

drupal_set_message('file not uploaded for '.$row->title,'warning');

}

$thumbnail_url = $row->field_resources_thumbnail;

if($thumbnail_url!=''){

$extfile_info1 = basename($thumbnail_url);

$filename = explode(".", $extfile_info1);

list($ext_name, $extension) = explode(".", $extfile_info1);

//$name = $filename[0] . '.' . $extension;

$name = $extfile_info1;

$uploaded_thumbnail = file_save_data(file_get_contents($thumbnail_url), "public://importcsv/$name", FILE_EXISTS_RENAME);

$file_url = $uploaded_thumbnail->getFileUri();

$fid = $uploaded_thumbnail->get('fid')->value;

if (!empty($uploaded_thumbnail)) {

$node->set('field_resources_thumbnail', ['target_id' => $fid]);

}

}

        $res_save = $node->save();

Here we are giving an example for node create programmatically in Drupal 8, default fields, entity reference field, text field, list field, image upload, PDF upload in Node::create() code.


Note: If have any suggestions or issue regarding 'How to create node programmatically in drupal 8?' then you can ask by comments. 

How to resolve max execution time error in drupal ?

When you found error regarding 'max_execution_time' exceed, then you can follow steps for resolve this error: Steps:   You can put t...