0

Drupal 7 で (テキストのみの) ファイルを生成し、それを特定のノードに自動的にアタッチする方法を教えてください。

モジュールでこれを行う方法はありますか、または何らかの方法でコードを使用する必要があります (これは難しいようです)。

4

2 に答える 2

1

最初に drupal file_save_dataを使用してファイルを作成します

$data = 'Your text data';     // Text data to be saved to file.
$filename = 'filename.txt';   // Filename
$file = file_save_data($data,'public://' .$filename);

$file には、ノードにアタッチできるファイル オブジェクトが含まれます。ファイル フィールドを作成してノード タイプにアタッチしたとします。「field_custom_file」としましょう

次に、アタッチするノードをロードし、ファイル オブジェクトを配列にキャストしてフィールドにアタッチします。

$node = node_load($nid);  // $nid is the id of the node where you want to attach the file.
$node->field_custom_file[LANGUAGE_NONE][] = (array)$file;
node_save($node);
于 2013-03-01T23:05:30.050 に答える
0
$file = (object) array(
    'uid' => 1,
    'uri' => $filepath,
    'filemime' => file_get_mimetype($filepath),
    'status' => 1,
    'display' => 1,
);
$file = file_copy($file, 'public://');
$node->field_file['und'][0] = $file;
于 2013-03-01T15:25:02.957 に答える