Code
<?php
$bundle = 'story';
$default_langcode = 'eu';
$translation_langcode = 'es';
$vid = 'tags';
$uid = 1;
$taxonomy_terms = self::getAllTerms($vid);
$author = \Drupal::entityTypeManager()->getStorage('user')->load($uid);
$taxonomy_term = $sailkapen_terms[$tid] ?? NULL;
// check if exists
$nodes = \Drupal::entityTypeManager()
->getListBuilder('node')
->getStorage()
->loadByProperties([
'field_[key]' => $key,
]);
// create or load node
if($nodes) {
$node = array_shift($nodes);
} else {
$values = array(
// 'nid' => $nid,
'langcode' => $default_langcode,
'type' => $bundle,
'uid' => $uid,
'status' => 1,
'promote' => 0,
'title' => $title,
'field_signatura' => $signatura,
);
$node = \Drupal\node\Entity\Node::create($values);
$node->save();
}
// create or load translation
if( !$node->hasTranslation($translation_langcode) ) {
$node_tr = $node->addTranslation($translation_langcode);
$node_tr->uid = $uid;
} else {
$node_tr = $node->getTranslation($translation_langcode);
}
$node_tr->body->format = 'basic_html';
$node_tr->body->value = $pp[2];
$node_tr->title = self::getTitleFromDescription($pp[2]);
$node_tr->setOwnerId($author);
$node_tr->save();
if(!empty($body_content)) {
$node->body = array(
'summary' => NULL,
'value' => $body_content,
'format' => 'basic_html' // 'full_html'...
);
}
// set plain text value
if( !empty($field_value) ) {
$node->field_hasiera->setValue( $field_value );
}
// set entity reference value
if(!empty($sailkapen)) {
$node->field_field_key->setValue(['target_id' => $tid]);
}
$node->field_raw->setValue($r);
$node->save();
?>