您可以使用自定义模块中的hook_node_presave()来实现此目的。
在本例中,您的模块名为my_module
,而您的自定义内容类型$node->type
为custom_type
。
您发送到您的Python脚本节点的字段名称是:field_baz
你的Python脚本是:/path/to/my/python_script.py
,它需要一个参数,它是要填充field_baz
附加节点字段的值通过你的python脚本返回的结果是field_data_foo
和field_data_bar
不清楚python脚本的输出是什么,所以这个例子模拟好像输出是JSON字符串。
的例子使用hook_node_presave()来操纵$节点对象被保存之前它。这是在节点数据写入数据库之前处理的。节点对象被视为引用,因此对对象的任何修改都是保存时将使用的内容。
的逻辑将检查!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.
}
}
其他开发者(python脚本)正在等待我的有关我们将如何发送/接收数据的细节。我的默认答案是XML,但这仍然引发了关于如何生成本地xml文件的问题(我确信我可以解决这个问题)。如果我能在这里找到更优雅的解决方案,我宁愿不向他传达一个糟糕的方法。 – 2013-03-06 18:23:50
发送和提取需要在节点创建的同一个请求中完成,还是可以在后台发生?另外,为什么用python编写另一个脚本? – 2013-03-06 18:38:05
它不需要发生在与节点创建相同的请求中。它可能会在(正如我对XML解决方案所设想的)之后立即发生,或者甚至在节点被提交之前发生(老实说,这将是用户友好性最高的选项,因为脚本的目的是填充节点上的一些字段,用户不必)。至于为什么使用python - 我无法回答。我的客户严格委托另一位开发人员编写脚本,因为它不是我们原始设计的一部分。 – 2013-03-06 18:46:43