欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Drupal Imagfield/Filefield in custom form drupal 6 自定义表单 上传图像字段 有大用


自己亲自做的例子 见  /node-admin/8501
Drupal Imagfield/Filefield in custom form


<?php

function ils_ladda_upp_form() {
    $form['upload'] = array(
        '#method' => 'post',
        '#attributes' => array(
            'enctype' => 'multipart/form-data',
        )
    );
    $form['upload']['album_name'] = array(
        '#type' => 'textfield',
        '#title' => t('Albumnamn'),
        '#required' => 1
    );
    $form['upload']['album_location'] = array(
        '#type' => 'textfield',
        '#title' => t('Plats'),
    );
    $form['upload']['album_date'] = array(
        '#type' => 'date',
        '#title' => t('Datum'),
        '#required' => 1,
        '#suffix' => '(då bilderna togs)'
    );
    $form['upload']['album_description'] = array(
        '#type' => 'textarea',
        '#title' => t('Beskrivning'),
        '#resizable' => false,
    );
    $form['upload']['school'] = array(
        '#type' => 'hierarchical_select',
        '#title' => t('Skola & Klass'),
        '#size' => 1,
        '#required' => 1,
        '#config' => array(
            'module' => 'hs_taxonomy',
            'params' => array(
            'vid' => 1,
            ),
            'save_lineage'    => 0,
            'enforce_deepest' => 0,
            'entity_count'    => 0,
            'require_entity'  => 0,
            'resizable'       => 0,
            'level_labels' => array(
            'status' => 0,
            'labels' => array(
                0 => t('Main category'),
                1 => t('Subcategory'),
                2 => t('Third level category'),
            ),
        ),
            'dropbox' => array(
            'status'   => 0,
            'title'    => t('All selections'),
            'limit'    => 0,
            'reset_hs' => 1,
        ),
            'editability' => array(
            'status'           => 0,
            'item_types'       => array(),
            'allowed_levels'   => array(
            0 => 0,
            1 => 0,
            2 => 1,
            ),
            'allow_new_levels' => 0,
            'max_levels'       => 3,
        ),
            # These settings cannot be configured through the UI: they can only be
            # overridden through code.
            'animation_delay'    => 400,
            'special_items'      => array(),
            'render_flat_select' => 0,
            'path'               => 'hierarchical_select_json',
        ),
        #'#default_value' => '83',
    );
    $form['upload']['file'] = array(
        '#type' => 'file',
        '#title' => t('Bild'),
    );
    $form['upload']['name'] = array(
        '#type' => 'textfield',
        '#required' => true,
        '#title' => t('Ditt namn')
    );
    $form['upload']['submit'] = array('#type' => 'submit', '#value' => t('Ladda upp'));
    return $form['upload'];
}
?>



Is it possible to insert a CCK Filefield/imagefield in the form? If so, how do i do it?

Drupal v. 6.15

Regards,
Joar


 

2 Answers 正确答案

  

You should use: // drupal 6 试试 这个也有大用 

正确答案 

 有大用
 

D6: http://drupal.org/project/upload_element upload_element type

D7: managed_file type // drupal 7 试试

Instead of CCK filefield, it is almost the same (ajax, ahah upload) & you can implement it without hacking CCK

shareimprove this answer



在您自己的自定义表单上显示CCK Filefield或Imagefield Upload Widget

 

花了大量的搜索找到这一个的解决方案。随着节点画廊 3.x的分公司,我们需要一种方法来快速的图像添加到现有的画廊。我们可以显示整个节点的形式,但是有很多事情在表单上,我们可以使用默认值99%的时间。我们只需要填写三个字段:Title,Caption和imagefield本身。要使用相同的图像字段窗口小部件在您自己的字段上的节点添加字段处理所有的辛苦工作,首先在hook_menu中创建一个处理程序,如下所示:

$items['node/%node_gallery_gallery/upload'] = array(
    'title' => 'Upload New Image',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('node_gallery_upload_image_form', 1),
    'access callback' => 'node_gallery_user_access',
    'access arguments' => array('upload', 1),
    'file' => 'node_gallery.pages.inc',
    'type' => MENU_LOCAL_TASK,
  );

然后,在node_gallery.pages.inc中,创建执行工作的表单函数:

function node_gallery_upload_image_form($form_state, $gallery) {
  $imagetype = 'node_gallery_image';
  $form_id = $imagetype . '_node_form';
  
  module_load_include('inc', 'content', 'includes/content.node_form');
  $field = content_fields('field_node_gallery_image',$imagetype);
  
  $form['title'] = array(
    '#title' => t('Title'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#weight' => -10,
  );
  $form['body'] = array(
    '#title' => t('Caption'),
    '#type' => 'textarea',
    '#weight' => -9,
  );
  $form['type'] = array(
    '#type' => 'value',
    '#value' => $imagetype,
  );
  $form['gid'] = array(
    '#type' => 'value',
    '#value' => $gallery->nid,
  );
  $form['#field_info']['field_node_gallery_image'] = $field;
  $form['#field_info']['field_node_gallery_image']['#required'] = TRUE;
  $form += (array) content_field_form($form, $form_state, $field);
  
  $form['submit'] = array('#type' => 'submit', '#weight' => 10, '#value' => 'Save');
  
  return $form;
}

这很简单,直到28-30行。这三行设置表单数组,然后将content_field_form()的结果附加到我们现有的表单。仍然,很容易,但我不能找到任何文档如何做到这一点。为了防万一你好奇,这里是提交处理程序的形式。

function node_gallery_upload_image_form_submit($form, &$form_state) {
  global $user;
  $image = new stdClass;
  $image->uid = $user->uid;
  $image->name = (isset($user->name) ? $user->name : '');
  $values = $form_state['values'];
  foreach ($values as $key => $value) {
    $image->$key = $value;
  }
  node_gallery_image_save($image);
}

没有什么新的。最终的结果是一个漂亮的外观,简洁的形式,使您可以快速上传图片到图库。甜!

Display a CCK Filefield or Imagefield Upload Widget on Your Own Custom Form

 

Took a fair amount of googling around to find the solution to this one. With the Node Gallery 3.x branch, we needed a way to quickly add an image to an existing gallery. We could have displayed the whole node form, but there’s a lot of things on that form that we can just use the defaults for 99% of the time. We need just three fields filled in: Title, Caption, and the imagefield itself. To use the same imagefield widget that handles all the hard work for you on the node add field on your own field, first create a handler in hook_menu such as this:

$items['node/%node_gallery_gallery/upload'] = array(
    'title' => 'Upload New Image',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('node_gallery_upload_image_form', 1),
    'access callback' => 'node_gallery_user_access',
    'access arguments' => array('upload', 1),
    'file' => 'node_gallery.pages.inc',
    'type' => MENU_LOCAL_TASK,
  );

Then, in node_gallery.pages.inc, you create the form function that does the work:

function node_gallery_upload_image_form($form_state, $gallery) {
  $imagetype = 'node_gallery_image';
  $form_id = $imagetype . '_node_form';
  
  module_load_include('inc', 'content', 'includes/content.node_form');
  $field = content_fields('field_node_gallery_image',$imagetype);
  
  $form['title'] = array(
    '#title' => t('Title'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#weight' => -10,
  );
  $form['body'] = array(
    '#title' => t('Caption'),
    '#type' => 'textarea',
    '#weight' => -9,
  );
  $form['type'] = array(
    '#type' => 'value',
    '#value' => $imagetype,
  );
  $form['gid'] = array(
    '#type' => 'value',
    '#value' => $gallery->nid,
  );
  $form['#field_info']['field_node_gallery_image'] = $field;
  $form['#field_info']['field_node_gallery_image']['#required'] = TRUE;
  $form += (array) content_field_form($form, $form_state, $field);
  
  $form['submit'] = array('#type' => 'submit', '#weight' => 10, '#value' => 'Save');
  
  return $form;
}

This is pretty straightforward, up until lines 28 - 30. Those three lines setup the form array and then append the results from content_field_form() to our existing form. Still, very easy, but I wasn’t able to find any documentation on how to do this. Just in case you’re curious, here’s the submit handler for that form.

function node_gallery_upload_image_form_submit($form, &$form_state) {
  global $user;
  $image = new stdClass;
  $image->uid = $user->uid;
  $image->name = (isset($user->name) ? $user->name : '');
  $values = $form_state['values'];
  foreach ($values as $key => $value) {
    $image->$key = $value;
  }
  node_gallery_image_save($image);
}

Nothing new there. The end result is a nice looking, concise form that allows you to quickly upload an image to a gallery. Sweet!

Comments


 
 
Avatar
Join the discussion…

 

 
 
  •  
     
    Avatar

     

    Hi everyone, I want to tell you about a great new up and
    coming artiste called Emotan.
    Her music is fantastic and very special. Please like her page to join and
    listen to her music
    www.facebook.com/emotan3
    Or watch a video on youtube www.youtube.com/em0tan   and leave your comments and fed backs. Thanks
    see you there.

     

     
      
    •  

    •  

  •  
     
    Avatar

    First of All you should filter out user's comments. @Naveed you are doing your business at wrong place.

     
    •  

    •  

来自  http://sysadminsjourney.com/content/2010/01/26/display-cck-filefield-or-imagefield-upload-widget-you...

普通分类: