What I am trying to do is to save a copy of a node object after it is inserted into another Drupal database.
As it can be seen in the following piece of code, my idea is to change the database after a node is inserted. Make a copy of the node and save it. Then, return to the default database.
However, seems like entity storage manager is set to default database in node save function. Therefore, the node is getting saved inside the default database again.
//... mymodule_node_insert $node = Node::load($id); \Drupal\Core\Database\Database::setActiveConnection('external'); $db = \Drupal\Core\Database\Database::getConnection(); // save the node in the shared database // Create node object with attached file. $sharedNode = Node::create([ 'type' => $node->getType();, 'title' => $node->getTitle();, 'body_text' => $node->get('body')->value, ]); $sharedNode->save(); \Drupal\Core\Database\Database::setActiveConnection();
How can I make this work? Am I following a correct logic?
Any helps would be appreciated. Thanks