drupal 7 - 通过代码将文件附加到节点
我想将一个文件关联到一个节点。到目前为止,一切都很好。创建一个cck类型的文件,问题就解决了。但我不能这样做,我不希望用户选择文件。相关文件已在系统中。 我尝试将文件放置为 #default_value 字段并使用 hook_form_FORM_ID_alter 隐藏它,但失败了。
function my_module_form_node_form_alter(&$form, $form_state, $form_id) {
if(isset($form['type']) && isset($form['#node'])) {
$type = $form['#node']->type;
if(stripos($type, 'node-type') === FALSE)
return;
switch($type) :
case 'node-type_xyz':
$fid = arg(3);
$file = file_load($fid);
// make a cck field_invoice a hidden field
$form['field_invoice']['#prefix'] = '<div style="display:none;">';
$form['field_invoice']['#suffix'] = '</div>';
$form['field_company']['und'][0]['value']['#default_value'] = 'ABC';
$form['field_account_number']['und'][0]['value']['#default_value'] = '09879';
break;
endswitch;
}
}
有人有什么建议吗?
I wanted to associate a file to a node. so far so good. create a cck type file, and the problem was solved. but I can not do this, I do not want the user to choose the file. the file in question is already in the system.
I have tried to place the file as # default_value field and hide it with the hook_form_FORM_ID_alter, but failed.
function my_module_form_node_form_alter(&$form, $form_state, $form_id) {
if(isset($form['type']) && isset($form['#node'])) {
$type = $form['#node']->type;
if(stripos($type, 'node-type') === FALSE)
return;
switch($type) :
case 'node-type_xyz':
$fid = arg(3);
$file = file_load($fid);
// make a cck field_invoice a hidden field
$form['field_invoice']['#prefix'] = '<div style="display:none;">';
$form['field_invoice']['#suffix'] = '</div>';
$form['field_company']['und'][0]['value']['#default_value'] = 'ABC';
$form['field_account_number']['und'][0]['value']['#default_value'] = '09879';
break;
endswitch;
}
}
anyone have any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用#prefix 和#suffix 来隐藏它。相反,将 #access 设置为 false - 这样,人们就无法摆弄表单。您可以在 hook_nodeapi 或提交函数中设置值,或者将类型设置为“value”并将#value 设置为文件。
Don't use #prefix and #suffix to hide it. Instead, set #access to false - that way, people can't fiddle with the form. You can set the value in hook_nodeapi or a submit function, or set the type to 'value' and the #value to your file.