如何以编程方式创建带有日期和图像字段的节点?
我知道我可以在Drupal 7中使用以下代码来做到这一点。
global $user;
$node = new stdClass();
$node->title = "YOUR TITLE";
$node->type = "YOUR_NODE_TYPE";
node_object_prepare($node); // Sets some defaults. Invokes hook_prepare() and hook_node_prepare().
$node->language = LANGUAGE_NONE; // Or e.g. 'en' if locale is enabled
$node->uid = $user->uid;
$node->status = 1; //(1 or 0): published or not
$node->promote = 0; //(1 or 0): promoted to front page
//image field
$existing_filepath = "/home/nzcodarnoc/sites/default/files/imported/picture.jpg"
$new_filepath = "public://picture.jpg"
// Create the file object
$drupal_file = file_save_data(file_get_contents($existing_filepath), $new_filepath);
$drupal_file->alt = $node->title;
$drupal_file->title = $node->title;
// Assign the file object to the node, as an array
$node->field_my_file[$node->language][0] = get_object_vars($drupal_file);
//date field
$node->birth_date[LANGUAGE_NONE][0]['value'] = time();
$node = node_submit($node); // Prepare node for saving
node_save($node);
Drupal 8的等效代码是什么?
请检查此线程drupal.org/node/178506
—
Aryashree Pritikrishna 2015年
Node :: create($ node_data_array)-> save()
—
Eyal
@Eyal,请提供更多详细信息。这个问题将来会被提及过多
—
Yusef