I try to create a custom field type module. i added managed file field inside my hook_field_widget_form. my codes work correctly. but it stores images temporarily into file managed table. i want to store images permanently. i know that it is possible through submit handler function in usual drupal forms. how can i add a submit handler function for managed file field inside hook_field_widget_form. i have written my codes below.

<?php
function my_imgupload_field_type_field_info() {
  return array(
   
'my_image' => array(
     
'label' => t('Image Field'),
     
'description' => t('Custom image Field'),
     
'default_widget' => 'my_image_widget',
     
'default_formatter' => 'my_image_formatter',
    ),
  );
}

function
my_imgupload_field_type_field_widget_info() {
  return array(
   
'my_image_widget' => array(
     
'label' => t('Image Default'),
     
'field types' => array('my_image'),
    ),
  );
}

function
my_imgupload_field_type_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
 
 
$default_format  = array_shift(filter_formats($GLOBALS['user']))->format;
 
$field_name = $instance['field_name'];
 
$item =& $items[$delta];
  switch(
$instance['widget']['type']) {
    case
'my_image_widget':
     
$element['img1_upload'] = array(
       
'#title' => t('Image'),
       
'#type' => 'managed_file',
       
'#upload_location' => 'public://multifield_images/',
       
'#default_value' => isset($item['img1_upload']) ? $item['img1_upload'] : 0,
       
'#theme' => 'image_multifield_multitype',
      );
  }
  return
$element;
}

function
my_imgupload_field_type_theme() {
  return array(
   
'image_multifield_multitype' => array(
     
'render element' => 'element',
    ),
  );
}

function
theme_image_multifield_multitype($variables) {
 
$element = $variables['element'];
 
$output = '';
 
$base = drupal_render_children($element); // renders element as usual
 
if($element['fid']['#value'] != 0) {
   
$output .= '<div class="multifield-thumbnail">';
   
$output .= theme('image_style', array('style_name' => 'thumbnail', 'path' => file_load($element['fid']['#value'])->uri, 'getsize' => FALSE));
   
$output .= '</div>';
  }
 
 }
 
function
my_imgupload_field_type_field_is_empty($item, $field) {
 
$has_stuff = FALSE;
 
  if (!empty(
$item['img1_upload'])) {
   
$has_stuff = TRUE;
  }
  return !
$has_stuff;
}
?>

Please help me to fix this problem. i am expecting reply from someone.

Comments

Jaypan’s picture

You can add your submit handler in hook_form_alter().

najubudeen’s picture

can you help me how to add submit handler by using hook_form_alter. i am new field api module. Please give me a solution to fix this.

najubudeen

Jaypan’s picture

<?php
function mymodule_form_alter($form, &$form_state, $form_id)
{
  if(
$form_id == 'whichever_form_i_want_to_modify')
  {
   
$form['#submit'][] = 'my_custom_submit_function';
  }
}
?>

Note that if the form submit handlers are attached to a submit button, you will need to add your handler to that button rather than the form. It will be something like this:

<?php
function mymodule_form_alter($form, &$form_state, $form_id)
{
  if(
$form_id == 'whichever_form_i_want_to_modify')
  {
   
$form['submit_button']['#submit'][] = 'my_custom_submit_function';
  }
}
?>

You'll need to examine the $form array to find out if the submit handler(s) are attached to a button or not, and what the path to that button is.

najubudeen’s picture

i tried something like below for test. it didn't add submit handler. i want the submit handler to all content type in which my field type exists. i don't want for particular content type like below. however i tried for test. it didn't add submit handler.

<?php
function my_imgupload_field_type_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
 
$default_format  = array_shift(filter_formats($GLOBALS['user']))->format;
 
$field_name = $instance['field_name'];
 
$item =& $items[$delta];
  switch(
$instance['widget']['type']) {
    case
'my_image_widget':
     
$element['img1_upload'] = array(
       
'#title' => t('Image'),
       
'#type' => 'managed_file',
       
'#upload_location' => 'public://multifield_images/',
       
'#default_value' => isset($item['img1_upload']) ? $item['img1_upload'] : 0,
       
'#theme' => 'image_multifield_multitype',
      );
  }
  return
$element;
}
function
my_imgupload_field_type_theme() {
  return array(
   
'image_multifield_multitype' => array(
     
'render element' => 'element',
    ),
  );
}
function
theme_image_multifield_multitype($variables) {
 
$element = $variables['element'];
 
$output = '';
 
$base = drupal_render_children($element); // renders element as usual
 
if($element['fid']['#value'] != 0) {
   
$output .= '<div class="multifield-thumbnail">';
   
$output .= theme('image_style', array('style_name' => 'thumbnail', 'path' => file_load($element['fid']['#value'])->uri, 'getsize' => FALSE));
   
$output .= '</div>';
  }
 }

function
my_imgupload_field_type_form_alter($form, &$form_state, $form_id){
// i don't want submit handler for particular content type like this. the submit handler should be available for all content type.
if($form_id == 'feld_type_node_form'){
   
$form['#submit'][] = array('my_custom_submit_function');
  }
}

function
my_custom_submit_function($form, &$form_state){
   
drupal_set_message('works.');
}
?>

najubudeen

Jaypan’s picture

Node forms follow the second format in my post, with the submit handlers attached to the submit button. You'll need to attach the submit handler to the submit button, not the form.