How do I programmatically set a taxonomy term for a node?

How do I programmatically set a taxonomy term for a node?

I got some difficulties setting a taxonomy for a node. Currently I am using this code.

$node = Node::create(array(
    'type' => 'detailseite',
    'title' => $data[$headlineIndex],
    'langcode' => 'de',
    'uid' => '1',
    'status' => 1,
    'field_headline' => $data[$headlineIndex],
    'field_intro' => $data[$introIndex] . $categories[$data[$categoryIndex]],
    'created' => $data[$dateIndex],
    'field_autor' => $data[$autorIndex],
    'field_teaser_text' => $data[$shortIndex],
    'field_fields' => array(),
));
$node->save();

Which works fine so far (below an image of the fields of “detailseite”).

How do I programmatically set a taxonomy term for a node?

Everything gets set correctly and I can find the newly created nodes in the admin area that work correctly.
Now I need to set ‘field_meldungstyp’ which is an Entity-Reference to a Taxonomy:

enter image description here

which already contains three entries:

I want to get the node(s) I created programmatically to have “field_meldungstyp” set to “Artikel” of the “Meldungstyp”-Vocabulary.

I already tried adding:

'field_meldungstyp' => array( 'tid' => 19 ),

where 19 is the tid of “Artikel” of the earlier mentioned vocabulary. And that’s where I’m stuck.
Also https ://www.drupal.org/node/2402811 didn’t work for me. As well as

$node->field_meldungstyp[$node->language][]['tid'] = 19;

didn’t work either.

simply adding

$node = Node::create(array(
    ...
    'field_meldungstyp' => 19,
    ...
));

results in crashing with the following error:

DrupalCoreEntityEntityStorageException: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: ‘datetime’ for column ‘created’ at row 1: INSERT INTO {taxonomy_index} (nid, tid, status, sticky, created) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4); Array ( [:db_insert_placeholder_0] => 343 [:db_insert_placeholder_1] => 19 [:db_insert_placeholder_2] => 1 [:db_insert_placeholder_3] => 0 [:db_insert_placeholder_4] => datetime ) in DrupalCoreEntitySqlSqlContentEntityStorage->save() (line 770 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).
DrupalCoreDatabaseStatement->execute(Array, Array) (Line: 610)
DrupalCoreDatabaseConnection->query(‘INSERT INTO {taxonomy_index} (nid, tid, status, sticky, created) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4)’, Array, Array) (Line: 81)
DrupalCoreDatabaseDrivermysqlConnection->query(‘INSERT INTO {taxonomy_index} (nid, tid, status, sticky, created) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4)’, Array, Array) (Line: 32)
DrupalCoreDatabaseDrivermysqlInsert->execute() (Line: 371)
DrupalCoreDatabaseQueryMerge->execute() (Line: 525)
taxonomy_build_node_index(Object) (Line: 479)
taxonomy_node_insert(Object)
call_user_func_array(‘taxonomy_node_insert’, Array) (Line: 402)
DrupalCoreExtensionModuleHandler->invokeAll(‘node_insert’, Array) (Line: 167)
DrupalCoreEntityEntityStorageBase->invokeHook(‘insert’, Object) (Line: 418)
DrupalCoreEntityContentEntityStorageBase->invokeHook(‘insert’, Object) (Line: 470)
DrupalCoreEntityEntityStorageBase->doPostSave(Object, ) (Line: 304)
DrupalCoreEntityContentEntityStorageBase->doPostSave(Object, ) (Line: 395)
DrupalCoreEntityEntityStorageBase->save(Object) (Line: 761)
DrupalCoreEntitySqlSqlContentEntityStorage->save(Object) (Line: 364)
DrupalCoreEntityEntity->save() (Line: 46)
Drupalawo_importerControllerT3ImporterController->import()
call_user_func_array(Array, Array) (Line: 123)
DrupalCoreEventSubscriberEarlyRenderingControllerWrapperSubscriber->DrupalCoreEventSubscriber{closure}() (Line: 574)
DrupalCoreRenderRenderer->executeInRenderContext(Object, Object) (Line: 124)
DrupalCoreEventSubscriberEarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
DrupalCoreEventSubscriberEarlyRenderingControllerWrapperSubscriber->DrupalCoreEventSubscriber{closure}()
call_user_func_array(Object, Array) (Line: 144)
SymfonyComponentHttpKernelHttpKernel->handleRaw(Object, 1) (Line: 64)
SymfonyComponentHttpKernelHttpKernel->handle(Object, 1, 1) (Line: 57)
DrupalCoreStackMiddlewareSession->handle(Object, 1, 1) (Line: 47)
DrupalCoreStackMiddlewareKernelPreHandle->handle(Object, 1, 1) (Line: 98)
Drupalpage_cacheStackMiddlewarePageCache->pass(Object, 1, 1) (Line: 77)
Drupalpage_cacheStackMiddlewarePageCache->handle(Object, 1, 1) (Line: 47)
DrupalCoreStackMiddlewareReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
DrupalCoreStackMiddlewareNegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
StackStackedHttpKernel->handle(Object, 1, 1) (Line: 628)
DrupalCoreDrupalKernel->handle(Object) (Line: 19)


Go to Source of this post
Author Of this post: Eternal Black
Title Of post: How do I programmatically set a taxonomy term for a node?
Author Link: {authorlink}