0

我有一个脚本,它可以从我们的冲浪网络摄像头中获取最新图像,它保存了一个照片类型的节点。在该内容类型中,我有一个实体字段,它引用膨胀大小(内容类型 swell_data)。我可以添加一个静态实体 id 没问题:

// Reference Swell Data ID 176821 
$node->field_buoy_ref[$node->language][0]['target_id'] = 176821;

如何动态查找由 swell_data 内容类型创建的最新节点(使用 CRON 作业每小时添加一次)并使用该值来代替?供您参考,这是我正在使用的当前脚本(感谢FooNinja

define('DRUPAL_ROOT', getcwd());
require_once 'includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$bodytext = "This was the view from the Camera at ".date('gA')." on ".date('l F jS Y');

$node = new stdClass(); // Create a new node object
$node->type = "photos"; // Or page, or whatever content type you like
node_object_prepare($node); 

$node->title = "Caswell Camera ". date('D M j G:i');
$node->language = LANGUAGE_NONE; 

// Author
$node->name = "Gower Live"; // UID of the author of the node; or use $node->name
// Publish Node
$node->status = 1;
//Set Body Text
$node->body[$node->language][0]['value']   = $bodytext;
$node->body[$node->language][0]['summary'] = text_summary($bodytext);
$node->body[$node->language][0]['format']  = 'filtered_html';

//Set Image Location (Caswell)
$node->field_photo_location[$node->language][0]['lat'] = 51.570195982718374;
$node->field_photo_location[$node->language][0]['lng'] = -4.030849980797484;

// Set the Photos date
$node->field_image_date[$node->language][0]['value'] = date("Y-m-d H:i:s");

// From the Webcam
$node->field_webcam_photo[$node->language][0]['value'] = "Caswell";

// Reference Wave Buoy
$node->field_buoy_ref[$node->language][0]['target_id'] = 176821; // I'd this value dynamically pulled from the DB

// Attach Latest Webcam Image
$file_path = drupal_realpath('images/webcams/caswell-webcam.jpg');
$file = (object) array(
            'uid' => 1,
            'uri' => $file_path,
            'filemime' => file_get_mimetype($file_path),
            'status' => 1,
);
// You can specify a subdirectory, e.g. public://foo/
$file = file_copy($file, 'public://');
$node->field_user_photo[$node->language][0] = (array) $file;

$path = 'node_created_on' . date('YmdHis');
$node->path = array('alias' => $path);

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

谢谢

4

1 回答 1

0

I added the following below the Bootstrap Line and bingo.

$result = db_query("SELECT nid, title, created FROM {node} WHERE type = :type AND title =:title", array(
  ':type' => 'wavebuoy_data',
  ':title' => "Buoy ". date('D M j gA'),
));
foreach($result as $row) {
$wave_buoy_ref_value = $row->nid;
//print_r($row);
}

Then when building the node, added the following:

// Reference Wave Buoy
$node->field_buoy_ref[$node->language][0]['target_id'] = $wave_buoy_ref_value;

Lee

于 2012-11-06T21:43:51.423 回答