0

This question already has an answer here:

When I programmatically create a taxonomy term and update node, in hook_node_presave() I have the following code:

$node->field_status_tags[LANGUAGE_NONE][]['tid'] = $value->tid;

I get the following error:

Notice: Array to string conversion in DrupalDefaultEntityController->cacheGet() (line 369 of includes\entity.inc).

This is the code I am using. I'm sure the value for tid is right.

/**
 * Implements hook_node_presave().
 */
function love_layout_node_presave($node) {
  /*
   *if ($node->nid && $node->moderate) {
   *  // Reset votes when node is updated:
   *  $node->score = 0;
   *  $node->users = '';
   *  $node->votes = 0;
   *}
   */
  if($node->type == 'status') {

   $pattern = '/#([^\\#|.]+)#/';
   preg_match_all($pattern, $node->title, $matches, PREG_SET_ORDER);
   $node->field_status_tags[LANGUAGE_NONE] = array();
   foreach ($matches as $key => $value) {

      $term_name = $value[1];
      $term = taxonomy_get_term_by_name($term_name);
      if(!count($term)){
       $term = new stdClass();
       $term->vid = 1;
       $term->name = $term_name;
       taxonomy_term_save($term);
       $term = taxonomy_get_term_by_name($term_name);
       foreach ($term as $value) {
          //dpm($value,$key);
          $node->field_status_tags[LANGUAGE_NONE][]['tid'] = (int) $value->tid;
        }
      }else{
        foreach ($term as $key => $value) {
          //dpm($value,$key);
          $node->field_status_tags[LANGUAGE_NONE][]['tid'] = (int) $value->tid;
        }
      }
   }
   //dpm($node,'love_layout_node_presave-node');
  }
}

marked as duplicate by kiamlaluno Sep 12 '13 at 22:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

Basically I would think that $value->tid when you are using it is not a number but an array. Because I do the same assignment and it works.

So I suspect you are misusing the taxonomy term you created. This is how I created the taxonomy in a function:

function create_new_term($term_name, $vocabulary_id, $language = LANGUAGE_NONE, $i18n_tsid = 0){
  $tid = db_insert('taxonomy_term_data')
  ->fields(array(
    'name'=>$term_name,
    'vid'=>$vocabulary_id,
    'language'=>$language,
    'i18n_tsid'=>$i18n_tsid,
  ))->execute();
  db_insert('taxonomy_term_hierarchy')
    ->fields(array(
      'tid'=>$tid,
      'parent'=>0,
  ))
  ->execute();
  print "Created new taxonomy term tid #".$tid." $term_name in $vocabulary_id<br>"."\n";
  return $tid;
}

And here is how I add that term:

$my_new_node->field_my_term['LANGUAGE_NONE'] [] ['tid'] = $returned_tid;

I hope this helps.

-1

This appears to be the same as one the existing core issues (https://drupal.org/node/1525176).

As of 2013-09-11, there are patches in the issue, but it still remains open.

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