これは、カスタムモジュールのhook_node_presave()を使用して実現できます。
この例では、モジュールに名前が付けられmy_module
、カスタムコンテンツタイプ$node->type
はcustom_type
です。
Pythonスクリプトに送信するノードのフィールド名は次のとおりです。field_baz
あなたのPythonスクリプトは次のとおりです:/path/to/my/python_script.py
そしてそれは1つの引数を取ります、それはの値ですfield_baz
Pythonスクリプトの戻りによって入力される追加のノードフィールドはfield_data_foo
、field_data_bar
Pythonスクリプトの出力が明確ではなかったため、この例では、出力がJSON文字列であるかのようにシミュレートします。
この例では、hook_node_presave()を使用して、保存する前に$nodeオブジェクトを操作します。これは、ノードデータがデータベースに書き込まれる前に処理されます。ノードオブジェクトは参照として扱われるため、オブジェクトへの変更は、保存時に使用されます。
!isset($node->nid)
これはノードが作成されたときにのみ発生すると述べたため、ロジックはそれをチェックします。ノードが更新されたときにも発生する必要がある場合は、その条件を削除するだけです。
/**
* Implements hook_node_presave().
*/
function my_module_node_presave($node) {
if ($node->type == 'custom_type' && !isset($node->nid)) {
// Get the user value from the node.
// You may have to clean it so it enters the script properly
$baz = $node->field_baz[LANGUAGE_NONE][0]['value'];
// Build the command string
$cmd = '/path/to/my/python_script.py ' . $baz;
// Execute the script and store the output in a variable
$py_output = shell_exec($cmd);
// Assuming the output is a JSON string.
$data = drupal_json_decode($py_output);
// Add the values to the fields
$node->field_data_foo[LANGUAGE_NONE][0]['value'] = $data['foo'];
$node->field_data_bar[LANGUAGE_NONE][0]['value'] = $data['bar'];
// Nothing more to do, the $node object is a reference and
// the object with its new data will be passed on to be saved.
}
}