问题描述
我有节点保存图片。提供在每个节点中选择图像的类型和尺寸以进一步下载到最终用户设备的任务。为此,我想在每个节点下附加一个表单,以允许用户选择所需的类型和大小。用户选择它并放置提交按钮后,我想处理用户的请求,在服务器端生成所需的图片,并以某种方式他的浏览器进行下载。但是我不知道如何实现这一点,因为我不了解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('选择要下载的图像类型') ,
'#options'=> array(0 => t('SVG'),1 => t('PNG'),2 => t('ICON'),
);
$ form ['image_sizes'] = array(
'#type'=>'radios',
'#title'=> t('选择图像大小'),
'#options'=>数组(0 => t('100x100'),1 => t('200x200'))
);
$ form ['submit'] = array(
'#type'=>'submit',
'#value'=> t('下载'),
);
return $ form;
}
函数testmodule_custom_form_submit($ form,& $ form_state){
drupal_set_message('IT WORKS !!!');
}
函数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,
);
}
}
函数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;
}
}
?>
一种方法是创建一个自定义模块使用表单回调函数。
代码示例:
函数YOUR_MODULE_custom_form ($ form,& $ form_state){
//你的表单控制和输入到这里
}
函数YOUR_MODULE_custom_form_submit($ form,& $ form_state){
//您的表单提交处理程序。
//您的图像调整大小的逻辑和任何其他逻辑应该在这里
}
然后为了将此表单附加到节点,请创建一个实现。
函数YOUR_MODULE_node_view ($ node,$ view_mode,$ langcode)
{
if($ node-> type ==='YOUR_CONTENT_TYPE'){
$ my_form = drupal_get_form('YOUR_MODULE_custom_form',$ node );
$ node-> content ['your_attached_form'] = array(
'#markup'=> drupal_render($ my_form),
'#weight'=> 99,
);
}
}
希望这能让你开始。我强烈建议您花点时间熟悉Drupal表单API并创建自定义模块,这将使您的生活更容易:)。
I have nodes keeping a picture. There is a task to provide an ability to select a type and a size of the picture in every node for further downloading to the end user device.To do this, I want to attach a form under every node to allow user select a type and a size they need. After user select it and put submit-button I want to process user's request, to generate the picture they need on the server side and make somehow his browser to download it. But I have no idea how to achieve this because I don't understand a mechanism of client-server exchange in Drupal 7. I know basic Form API, but it does not help me.So, what can you advice?
EDIT 1
This is a source code of my module 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;
}
}
?>
One way to do this is to create a custom module with a form callback function.
Code sample:
function YOUR_MODULE_custom_form($form, &$form_state) {
// your form controls and inputs go here
}
function YOUR_MODULE_custom_form_submit($form, &$form_state) {
// your form submission handler.
// your image resizing logic and any other logic should go here
}
Then in order to attach this form to nodes, create a hook_node_view
implementation.
function YOUR_MODULE_node_view($node, $view_mode, $langcode)
{
if($node->type === 'YOUR_CONTENT_TYPE') {
$my_form = drupal_get_form('YOUR_MODULE_custom_form', $node);
$node->content['your_attached_form'] = array(
'#markup' => drupal_render($my_form),
'#weight' => 99,
);
}
}
Hope this gets you started. I highly recommend taking your time getting familiar with Drupal form API and creating custom modules, that would make your life a lot easier :).
这篇关于在Drupal 7中使用表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!