Drupal 7 で (テキストのみの) ファイルを生成し、それを特定のノードに自動的にアタッチする方法を教えてください。
モジュールでこれを行う方法はありますか、または何らかの方法でコードを使用する必要があります (これは難しいようです)。
最初に 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);
$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;