1

I used this post and this post to get started but I was trying to use only the entity API.

Here is my code. So far the node creation works but I'm not able to save the node under the correct tid.

global $user;
$properties = array(
'type' => 'forum',
'uid' => $user->uid,
'status' => 0
); 
$entity = entity_create('node', $properties);
$wrapper = entity_metadata_wrapper('node', $entity);
$wrapper->title = "My new forum topic";
$terms =  taxonomy_get_term_by_name ('General discussion','forums');

//$wrapper->taxonomy = $terms; //Doesnt work
//$wrapper->forum_tid = $terms->tid; //Doesnt work
//$wrapper->taxonomy_forums = $terms->tid; //Doesnt work

$wrapper->save();
2
  • $wrapper->taxonomy_forums = $terms->tid or $wrapper->taxonomy_forums->set($terms->tid); should work fine, assuming $terms->tid is valid
    – Clive
    Nov 28 '15 at 18:33
  • Thanks Clive, you've always got the right answer or a much needed sanity check!!
    – John R
    Nov 28 '15 at 18:50
1

$wrapper->taxonomy_forums = reset($terms)->tid; works!! taxonomy_get_term_by_name() returns an array of objects keyed with extra dimension (I think its keyed by the tid). The wrapper's save method will call forum_node_presave($node) but will look in the wrong dimension for the tid if you don't get rid of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.