【发布时间】:2015-01-16 17:22:28
【问题描述】:
我有保存图片的节点。有一项任务是提供在每个节点中选择图片的类型和大小以进一步下载到最终用户设备的能力。 为此,我想在每个节点下附加一个表单,以允许用户选择他们需要的类型和大小。用户选择它并放置提交按钮后,我想处理用户的请求,在服务器端生成他们需要的图片,并以某种方式让他的浏览器下载它。但我不知道如何实现这一点,因为我不了解 Drupal 7 中的客户端-服务器交换机制。我知道基本的 Form API,但它对我没有帮助。 那么,你有什么建议?
编辑 1
这是我的模块testmodule.module的源代码
<?php
function testmodule_custom_form($form, &$form_state) {
$form=array();
$form['image_types'] = array(
'#type' => 'radios',
'#title' => t('Select image type to download'),
'#options' => array(0 => t('SVG'), 1 => t('PNG'), 2 => t('ICON')),
);
$form['image_sizes'] = array(
'#type' => 'radios',
'#title' => t('Select image size'),
'#options' => array(0 => t('100x100'), 1 => t('200x200')),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Download'),
);
return $form;
}
function testmodule_custom_form_submit($form, &$form_state) {
drupal_set_message('IT WORKS!!!');
}
function testmodule_node_view($node, $view_mode, $langcode) {
if($node->type === 'article') {
$my_form = drupal_get_form('testmodule_custom_form', $node);
$node->content['attached_form'] = array(
'#markup' => drupal_render($my_form),
'#weight' => 99,
);
}
}
function testmodule_node_presave($node) {
$node->body[$node->language][0]['format'] = 'full_html';
if ($node->field_image_svg[$node->language])
{
$file = file_load($node->field_image_svg[$node->language][0]['fid']);
$image = new Imagick(drupal_realpath($file->uri));
$image->setImageFormat("png");
$destination = 'public://pngimages/'.$file->filename.'.png';
$image->writeImage(drupal_realpath($destination));
$data = file_get_contents(drupal_realpath($destination));
$file = file_save_data($data, $destination, FILE_EXISTS_REPLACE);
$node->field_image[$node->language][0] = (array)$file;
}
}
?>
【问题讨论】:
标签: forms drupal drupal-7 client-server submit