私は問題があります。このコードでノードフィールドにファイルを添付しようとしています
function calculator_page_form_validate($form, &$form_state) {
$file = file_save_upload('file', array(
// Validate extensions.
'file_validate_extensions' => array(),
));
// If the file passed validation:
if ($file)
{
// Move the file into the Drupal file system.
if ($file = file_move($file, 'public://cunning_files', FILE_EXISTS_RENAME)) {
// Save the file for use in the submit handler.
$form_state['storage']['file'] = $file;
}
else
{
form_set_error('file', t("Failed to write the uploaded file to the site's file folder."));
}
}
else
{
form_set_error('file', t('No file was uploaded.'));
}
}
function calculator_page_form_submit($form, &$form_state)
{
$file = $form_state['storage']['file'];
// We are done with the file, remove it from storage.
unset($form_state['storage']['file']);
// Make the storage of the file permanent.
$file->status = FILE_STATUS_PERMANENT;
// Save file status.
file_save($file);
$file->display = 1;
$file->description = "";
// Set a response to the user.
drupal_set_message(t('The form has been submitted and the image has been saved, filename: @filename.', array('@filename' => $file->filename)));
global $user;
$node = new stdClass();
$node->title = $file->filename;
$node->type = "file";
//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
$node->comment = 0; // 0 = comments disabled, 1 = read only, 2 = read/write
// Term reference (taxonomy) field
//$node->field_tag[$node->language][]['tag'] = $form_state['values']['tag'];
add_terms_for_node($form_state['values']['tag'], &$node);
$file->display = 1;
$file->description = "";
$node->field_file[$node->language][0] = (array)$file;
// 'node' is default,
// Other possible values are "user" and "taxonomy_term"
$node = node_submit($node); // Prepare node for saving
node_save($node);
//drupal_set_message( "Node with nid " . $node->nid . " saved!\n");
}
何が悪いのですか?私の貧しい英語を助けてくれてありがとう、それは私の自然言語ではありません。
間違いを見つけました。誤ってfield_fileをfield_fieleと名付けました。生後4時間だ。コードは正しく動作します。