在Drupal的形式工作7(Working with forms in Drupal 7)

2019-10-21 07:18发布

我有节点保持画面。 有提供给选择类型和尺寸的图片中的每个节点用于进一步下载到最终用户设备的能力的任务。 要做到这一点,我想每一个连接节点下的表单,允许用户选择一个类型,他们所需要的尺寸。 用户后,选择它,并把提交按钮我要处理用户的请求,产生他们需要在服务器端,让他莫名其妙浏览器下载它的图片。 但我不知道如何实现这一点,因为我不明白,在Drupal 7客户端 - 服务器交换的机制,我知道基本形式的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;  
    }
}
?>  

Answer 1:

这样做的一个方法是创建一个表单回调函数的自定义模块。

代码示例:

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
}

然后,以这种形式附加到节点,创建一个hook_node_view实现。

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,
        );
    }
}

希望这可以让你开始。 我强烈建议你的时间越来越熟悉Drupal的形式API和创建自定义模块,这将使你的生活轻松了许多:)。



Answer 2:

另一种方法,我通常喜欢,就是用程序上的块使用默认值。

我已经实现了一个在你的代码和清理一些项目带来更好的为符合编码标准。

/**
 * Implements hook_form().
 */
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;
}

/**
 * Implements hook_form_submit().
 */
function testmodule_custom_form_submit($form, &$form_state) {
  drupal_set_message('IT WORKS!!!');
}

/**
 * Implements hook_block_info().
 */
function testmodule_block_info() {
  $blocks = array();

  $blocks['image_download'] = array(
    'info' => t('Node - Image download'),
    'status' => 1,
    'region' => 'content',
    'visibility' => BLOCK_VISIBILITY_LISTED,
    'pages' => 'node/*',
    'cache' => DRUPAL_CACHE_PER_PAGE,
    'weight' => 1,
  );

  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function testmodule_block_view($delta = '') {
  $n = menu_get_object();
  if (!$n || $n->type != 'only_particular_content_type') {
    return FALSE;
  }

  if ($delta == 'image_download') {
    return array(
      'content' => drupal_get_form('testmodule_custom_form'),
    );
  }

  return FALSE;
}

/**
 * Implements hook_node_presave().
 */
function testmodule_node_presave($node) {
  $node->body[$node->language][0]['format']  = 'full_html';
  $field_image_svg = field_get_items('node', $node, 'field_image_svg');
  if (!$field_image_svg) {
    return;
  }

  $file = file_load($field_image_svg[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;

}


文章来源: Working with forms in Drupal 7