[support] Building "add more" feature for managed_file form field type

Kayode Odeyemi dreyemi at gmail.com
Sat Sep 17 17:07:01 UTC 2011


Hello,

I'm trying to provide the ability to upload more than one file in the
configuration of one of my custom blocks.
I got the basics, but everytime I click on upload button, I get this:

*Notice*: Undefined index: uec_core_socials_block_image in *
uec_core_socials_block_image_validate()
**Notice*: Undefined index: files_fieldset in
*uec_core_socials_block_image_validate()
*
*
*I don't know why I'm getting this because the index is there and works for
other blocks I have created that way.* *I'm also following the ajax_examples
module in the examples module.
*
*Below is the snippet I'm working with:

function uec_core_block_configure($delta = '') {
  $form = array();
    switch($delta) {*
*case 'uec_core_socials':
                $form['#tree'] = TRUE;
                $form['files_fieldset'] = array(
                    '#type' => 'fieldset',
                    '#title' => t('Upload social icons'),
                    // Set up the wrapper so that AJAX will be able to
replace the fieldset.
                    '#prefix' => '<div id="names-fieldset-wrapper">',
                    '#suffix' => '</div>',
                );
                // Build the fieldset with the proper number of files. We'll
use
                // $form_state['num_files'] to determine the number of
managed_file fields to build.
                if (empty($form_state['num_files'])) {
                    $form_state['num_files'] = 1;
                }
                // provide ability to add more files
                for ($i = 0; $i < $form_state['num_files']; $i++) {

$form['files_fieldset']['uec_core_socials_block_image'][$i] = array(
                        '#type' => 'managed_file',
                        '#title' => t('Image'),
                        '#description' => t('Upload an image to represent
social icons, allowed extensions: jpg, jpeg, png, gif'),
                        '#default_value' =>
variable_get('uec_core_socials_block_image',  ''),
                        '#element_validate' =>
array('uec_core_socials_block_image_validate'),
                        '#upload_location' => 'public://' //If this is not
set, the file will be uploaded to temporary dir. Then use file_move to move
it to drupal default file system location
                    );
                }
                $form['files_fieldset']['add_file'] = array(
                    '#type' => 'submit',
                    '#value' => t('Add one more'),
                    '#submit' => array('uec_core_socials_add_more_add_one'),
                    // See the examples in ajax_example.module for more
details on the
                    // properties of #ajax.
                    '#ajax' => array(
                        'callback' => 'uec_core_socials_add_more_callback',
                        'wrapper' => 'names-fieldset-wrapper',
                    ),
                );
                break;
    }
    return $form;
}


/**
 * Callback for both ajax-enabled buttons.
 *
 * Selects and returns the fieldset with the names in it.
 */
function uec_core_socials_add_more_callback($form, $form_state) {
  return $form['files_fieldset'];
}

/**
 * Submit handler for the "add-one-more" button.
 *
 * Increments the max counter and causes a rebuild.
 */
function uec_core_socials_add_more_add_one($form, &$form_state) {
  $form_state['num_files']++;
  $form_state['rebuild'] = TRUE;
}

function uec_core_socials_block_image_validate($form, &$form_state) {
    variable_set('uec_core_socials_block_image',
$form_state['values']['files_fieldset']['uec_core_socials_block_image']);
    //variable_set('uec_core_socials_block_image',
$form_state['values']['uec_core_socials_block_image']);
}

I'll like to know what I'm doing wrong.
-- 
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/support/attachments/20110917/1494ec54/attachment.html 


More information about the support mailing list