创建节点wuth术语引用标记

时间:2015-10-18 11:34:27

标签: database drupal-7

我试图以编程方式添加节点,使用术语引用自动完成术语窗口小部件(标记)并添加新标记我的代码不起作用

当我从代码中删除标记引用时,我的节点成功添加 我需要为每个节点添加新标签

 <?php

       $bodytext = "body?";

      $node = new stdClass(); // Create a new node object
       $node->type = "programstvradio"; // Or page, or whatever content type you              like
node_object_prepare($node); // Set some default values
// If you update an existing node instead of creating a new one,
// comment out the three lines above and uncomment the following:
// $node = node_load($nid); // ...where $nid is the node id
 $node->title    = "title";
$node->language = LANGUAGE_NONE; // Or e.g. 'en' if locale is enabled
 $node->uid = 1; // UID of the author of the node; or use $node->name
 $node->body[$node->language][0]['value']   = $bodytext;
$node->body[$node->language][0]['format']  = 'filtered_html';
 // I prefer using pathauto, which would override the below path
$path = 'node_created_on' . date('YmdHis');
$node->path = array('alias' => $path);

$node->field_tag_[$node->language][]['value'] ='tagggggggg';
 $node->field_catprogram[$node->language][]['tid'] =12;



$file_path = drupal_realpath('C:\Users\Admin\Desktop\drupal.png'); // Create a File object'); // Create a File object
$file = (object) array(
  'uid' => 1,
  'uri' => $file_path,
  'filemime' => file_get_mimetype($file_path),
  'status' => 1,
); 
$file = file_copy($file, 'public://images'); // Save the file to the root of the files directory. You can specify a subdirectory, for example, 'public://images' 
$node->field_image[LANGUAGE_NONE][0] = (array)$file; //associate the file object with the image field:

if($node = node_submit($node)) { // Prepare node for saving
    node_save($node);
    echo "Node with nid " . $node->nid . " saved!\n";
}
?>``

1 个答案:

答案 0 :(得分:0)

我找到解决方案

$my_term_name = 'gggg';
$term_array = taxonomy_get_term_by_name($my_term_name);
if($term_array == array()){
    //empty term ..
    $term->name = $my_term_name;
    $term->vid = 1;
taxonomy_term_save($term);
$term_array = taxonomy_get_term_by_name($my_term_name); 
}
//get the first index of the array .
foreach ($term_array as $tid => $term_object)break;
$node->field_progna['und'][$tid] = (array)$term_object;