5

カスタム フォームの送信に基づいてノードを作成しようとしています。アップロードされる画像を除いて、すべてがうまく機能します。

それらをうまくキャプチャして、フォーム オブジェクト キャッシュに設定できます。データを関数に渡してノードを作成すると、次のエラーが発生します。

「その名前のファイルが存在しないため、指定されたファイルをコピーできませんでした。正しいファイル名を指定したことを確認してください。」

一度に 1 つまたは 2 つの画像しか送信しないのに、エラーが何度も表示されます。

これが私が使用しているコードです。$uploads が渡され、前のステップで file_save_upload() から返されたファイル オブジェクトの配列です。

if (isset($uploads)) {
    foreach ($uploads as $upload) {
      if (isset($upload)) {
        $file = new stdClass;
        $file->uid = 1;
        $file->uri = $upload->filepath;
        $file->filemime = file_get_mimetype($upload->uri);
        $file->status = 1;  

        $file = file_copy($file, 'public://images');

        $node->field_image[$node->language][] = (array) $file;
      }
    }
  }

  node_save($node);

私もこれを試しました:

if (isset($uploads)) {
    foreach ($uploads as $upload) {
        $upload->status = 1;  

        file_save($upload);

        $node->field_image[$node->language][] = (array) $upload;
      }
    }
  }

  node_save($node);

2 つ目は、URI フィールドで MySQL の重複キー エラーを引き起こします。これらの例はどちらもチュートリアルで見ましたが、どちらも機能していませんか?

4

3 に答える 3

5

Drupal 7 の場合、私はこれをかなりいじり、エンティティ メタデータ ラッパーを使用することが最善の方法 (そして私が作業できる唯一の方法) であることを発見しました。

私は次のように管理されたファイルフォーム要素を使用しました:

  // Add file upload widget
  // Use the #managed_file FAPI element to upload a document.
  $form['response_document'] = array(
    '#title' => t('Attach a response document'),
    '#type' => 'managed_file',
    '#description' => t('Please use the Choose file button to attach a response document<br><strong>Allowed extensions: pdf doc docx</strong>.'),
    '#upload_validators' => array('file_validate_extensions' => array('pdf doc docx')),
    '#upload_location' => 'public://my_destination/response_documents/',
  );

また、フォームの $node オブジェクトを値として渡します

$form['node'] = array('#type' => 'value', '#value' => $node);

次に、送信ハンドラーで次のことを行うだけです。

  $values = $form_state['values'];
  $node = $values['node'];
  // Load the file and save it as a permanent file, attach it to our $node.
  $file = file_load($values['response_document']);
  if ($file) {
    $file->status = FILE_STATUS_PERMANENT;
    file_save($file);

    // Attach the file to the node.
    $wrapper = entity_metadata_wrapper('node', $node);
    $wrapper->field_response_files[] = array(
      'fid' => $file->fid,
      'display' => TRUE,
      'description' => $file->filename,
    );
    node_save($node);
  }
于 2014-10-23T02:55:19.587 に答える
5

あなたのコードを使用して、ファイル フィールドのファイルをコンテンツ (私の場合は「ドキュメント」) にアップロードしましたが、うまくいきました。コードに field_document_file 'display' の値を追加するだけでした。これが私が使用した正確なスクリプトです:

<?php
// Bootstrap Drupal
define('DRUPAL_ROOT', getcwd());
require_once './includes/bootstrap.inc';
require_once './includes/file.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// Construct the new node object.
$path = 'Documents/document1.doc';
$filetitle = 'test';
$filename = 'document1.doc';

$node = new StdClass();

$file_temp = file_get_contents($path);

//Saves a file to the specified destination and creates a database entry.
$file_temp = file_save_data($file_temp, 'public://' . $filename, FILE_EXISTS_RENAME);

$node->title = $filetitle;
$node->body[LANGUAGE_NONE][0]['value'] = "The body of test upload document.\n\nAdditional Information";
$node->uid = 1;
$node->status = 1;
$node->type = 'document';
$node->language = 'und';
$node->field_document_files = array(
'und' => array(
    0 => array(
        'fid' => $file_temp->fid,
        'filename' => $file_temp->filename,
        'filemime' => $file_temp->filemime,
        'uid' => 1,
        'uri' => $file_temp->uri,
        'status' => 1,
        'display' => 1
    )
)
);
$node->field_taxonomy = array('und' => array(
0 => array(
    'tid' => 76
)
));
node_save($node);
?>
于 2012-06-01T13:53:14.463 に答える
1

ケビン、それはコメントの下のhttp://drupal.org/node/201594の下にある Drupal ドキュメントで見つけたものです。しかし、まったくわかりません。私もやってみましたので、わかる方教えてください。

$path = './sites/default/files/test.jpg';
$filetitle = 'test';
$filename = 'test.jpg';

$node = new StdClass();

$file_temp = file_get_contents($path);
$file_temp = file_save_data($file_temp, 'public://' . $filename, FILE_EXISTS_RENAME);

$node->title = $filetitle;
$node->uid = 1;
$node->status = 1;
$node->type = '[content_type]';
$node->language = 'und';
$node->field_images = array(
'und' => array(
    0 => array(
        'fid' => $file_temp->fid,
        'filename' => $file_temp->filename,
        'filemime' => $file_temp->filemime,
        'uid' => 1,
        'uri' => $file_temp->uri,
        'status' => 1
    )
)
);
$node->field_taxonomy = array('und' => array(
0 => array(
    'tid' => 76
)
));
node_save($node);
于 2011-10-04T04:31:19.710 に答える