I'm using data from JSON to create new nodes on my Drupal 8 website. So, I have a createNode
method that create these nodes.
Here is the code I'm using
protected function createNode($json) {
foreach ($json as $row) {
$node = Node::create(array(
'type' => 'product',
'langcode' => 'en',
'uid' => '1',
'status' => 1,
'title' => $row['title'],
'field_code' => $row['code'],
'field_manufacture' => $row['manufacture'],
'field_product_type' => $row['type'],
'field_barcode' => $row['barcode'],
'field_packages' => $row['packages'],
'field_pallet' => $row['pallet'],
));
$node->save();
}
}
The problem is with field_manufacture
and field_product_type
, those are entity reference field to taxonomy term. I'm getting term name and I need to match it in vocabulary or add it if doesn't exist.
Any idea how to do that?