Warning: Undefined array key "HTTPS" in include() (line 29 of /opt/www/fdv2/drupal.stage/web/sites/default/settings.local.php).
include('/opt/www/fdv2/drupal.stage/web/sites/default/settings.local.php') (Line: 4)
require('/opt/www/fdv2/drupal.stage/web/sites/drupal001.net/settings.php') (Line: 145)
Drupal\Core\Site\Settings::initialize('/opt/www/fdv2/drupal.stage/web', 'sites/drupal001.net', Object) (Line: 1099)
Drupal\Core\DrupalKernel->initializeSettings(Object) (Line: 701)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
自定义模块中添加一个 multiple 元素image,实现多次添加图片 - Drupal大学 - Drupal问答论坛
首页 / 帖子
自定义模块中添加一个 multiple 元素image,实现多次添加图片

在新建了一个模块后,想在这个模块中添加一个上传多张图片的功能,

这里,不是要一次选择多张图片的功能,而是多次选择,每次选一张。这个字段就像定义的multiple字段一样。当第一张图片上完,下面就可以选择另外的一个。

目前,我做的效果只能添加一张如图:代码如下:

if ($delta == 'aboutslidershow') {
    
    $form['image'] = array(
        '#type' => 'media',
        '#tree' => true,
        '#title' => t('Image'),
        '#description' => t('JPG\'s, GIF\'s, and PNG\'s only, 1MB Max Size'),
        '#input' => TRUE,
        '#media_options' => array(
            'global' => array(
                'types' => array('image'), // Example: array('image', 'audio');
                'schemes' => array('http'), // Example: array('http', 'ftp', 'flickr');
            ),
        ),
        '#default_value' => array('fid' => ''), // Change to your default value
        '#multiple' => 12
    );
}
return $form;

======================我想实现的效果类似下图=============================

请教大家这应该如何改进以实现我的需求啊,谢谢。

2个答案
发布于:2014-10-21 18:21

给你找到了一个示例代码,你可以在此基础上实现你的需求。

function example_add_more_form($form, &$form_state)
{
  $form['description'] = array(
    '#markup' => '<div>This is an example of a AJAX Form, where we can use AJAX add more concept in drupal for a set of fields.</div>',
  );

  // Because we have many fields with the same values, we have to set
  // #tree to be able to access them.
  $form['#tree'] = TRUE;
  $form['names_fieldset'] = array(
    '#type' => 'fieldset',
    '#title' => t('Family member details'),
    // 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 names. We'll use
  // $form_state['num_names'] to determine the number of textfields to build.
  if (empty($form_state['num_names'])) {
    $form_state['num_names'] = 1;
  }

  for ($i = 0; $i < $form_state['num_names']; $i++) {

    //lets add all the fields we want in the set  
    /*
    We have the prefix and suffix added here, so that we can do some sort of styling with the form, like display the fields side by side. You may remove    it, but generally we need that when we have a set of fields, hence I thought to keep it here.
    */

    //Parent container
    $form['names_fieldset'][$i] = array(
      '#prefix' => '<div class="two-col">',
      '#suffix' => '</div>'
    );

    //other form elements
    $form['names_fieldset'][$i]['image'] = array(
      '#type' => 'managed_file',
      '#title' => t('First Name'),
      '#prefix' => '<div class="col1">',
      '#suffix' => '</div>'
    );

    //-- Like wise we can add more
  }

  $form['names_fieldset']['add_name'] = array(
    '#type' => 'submit',
    '#value' => t('Add one more'),
    '#submit' => array('example_add_more_add_one'),
    // See the examples in ajax_example.module for more details on the
    // properties of #ajax.
    '#ajax' => array(
      'callback' => 'example_add_more_callback',
      'wrapper' => 'names-fieldset-wrapper',
    ),
  );

  if ($form_state['num_names'] > 1) {
    $form['names_fieldset']['remove_name'] = array(
      '#type' => 'submit',
      '#value' => t('Remove one'),
      '#submit' => array('example_add_more_remove_one'),
      '#ajax' => array(
        'callback' => 'example_add_more_callback',
        'wrapper' => 'names-fieldset-wrapper',
      ),
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

  return $form;

}

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

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

/**
 * Submit handler for the "remove one" button.
 *
 * Decrements the max counter and causes a form rebuild.
 */
function example_add_more_remove_one($form, &$form_state) {
  if ($form_state['num_names'] > 1) {
    $form_state['num_names']--;
  }
  $form_state['rebuild'] = TRUE;
}

/**
 * Final submit handler.
 *
 * Reports what values were finally set.
 */
function example_add_more_form_submit($form, &$form_state) {

  //Process the data of form here and use it

  /*
  #Example usage
  $form_state['input']['name_fieldset'][0,1,2... index number]['fieldname'];
  
  you can always use print_r($form_state); to explore the submited data.
  */

  dpm($form_state);
}


YOYO
发布于:2014-10-21 18:34

感谢楼上的方案,但是DRUPAL除了这种定义数量再循环字段的方法外,还没有类似drupal内置的那种单个字段多内容的更加智能的方法?

在一些网站上也看到类似的方案:http://rapiddg.com/blog/use-ajax-support-multiple-item-values-custom-drupal-7-forms

接着找……