There is a "Add another item" bottom below the field collection embedded widget. To add multiple items you have to hit the button again and again. Would it be possible to

a) add an option in the settings of the field collection to not only add one item but to specify a number to add with one click,

or

b) show a field near the button where the user can enter a number of items to add and then click the button

?

CommentFileSizeAuthor
#35 drop-button-style.png1.15 KBAnybody
#35 add-X-new-items.png2.62 KBAnybody
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

thomas_rendleman’s picture

That would be a nice feature especially when working with large amounts of data.
Not sure how that would work..?

ndeschildre’s picture

This is outside of this project (field collection), but I figured out this bit of code may help.
You can use that to add your own #submit function in the "Add another item" button && add/remove as many widgets as you want.

/**
 * Implements hook_field_attach_form()
 * We are looking at our t2imageclient widget after the "Add another item" has been added.
 * We are going to alter the behavior of this button, to let us add as many new entries as
 * we want.
 */
function YOURMODULE_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode)
{
  $options = array('language' => field_valid_language($langcode));

  // Merge default options.
  $default_options = array(
    'default' => FALSE,
    'deleted' => FALSE,
    'language' => NULL,
  );
  $options += $default_options;

  list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  $instances = _field_invoke_get_instances($entity_type, $bundle, $options);

  // Iterate through the instances.
  $return = array();
  foreach ($instances as $instance) {
    // field_info_field() is not available for deleted fields, so use
    // field_info_field_by_id().
    $field = field_info_field_by_id($instance['field_id']);
    $field_name = $field['field_name'];

    //If we are looking at our field type and specific widget type, and we are multiple entries
    if($field['type'] == 'TODO_PUT_YOUR_FIELD_TYPE' && 
      $instance['widget']['type'] == 'TODO_PUT_YOUR_WIDGET_TYPE' && 
      $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED)
    {
      //Check just in case the button is here, and add another #submit function
      if(isset($form[$field['field_name']]['und']['add_more']))
      {
        $form[$field['field_name']]['und']['add_more']['#submit'][] =
          'YOURMODULE_field_add_more_submit';
      }
    }
  }
}

/**
 * Our extra "Add another item" #submit handler. Copy pasted the field_add_more_submit
 * function, altered to change the nb of widgets based on our conditions.
 */
function YOURMODULE_field_add_more_submit($form, &$form_state)
{
  //Put here the condition for altering the nb of widgets, e.g. it could be based on a 
  //POST value
  if(true /** TODO */)
  {
    $button = $form_state['triggering_element'];

    // Go one level up in the form, to the widgets container.
    $element = drupal_array_get_nested_value($form, array_slice($button['#array_parents'], 0, -1));
    $field_name = $element['#field_name'];
    $langcode = $element['#language'];
    $parents = $element['#field_parents'];

    // Alter the number of widgets to show. items_count = 0 means 1.
    $field_state = field_form_get_state($parents, $field_name, $langcode, $form_state);
    //Here as an example, I am adding 10
    $field_state['items_count'] += 10;
    //But you can just fix the nb of items, like 5
    $field_state['items_count'] = 5;
    field_form_set_state($parents, $field_name, $langcode, $form_state, $field_state);

    $form_state['rebuild'] = TRUE;
  }
}

Enjoy!

cola’s picture

Hi all

any other ideas? because the variable $form[$field['field_name']]['und']['add_more'] is not set on my form

regards and thx

cola’s picture

solution: use the form_alter to add the submit function

8bitplateau’s picture

building on the excellent reply #2 by ndeschildre.
Just adding a simple select list of numbers to allow adding x number of items


/**
* Implements hook_field_attach_form()
*/

function village_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode){
  $options = array('language' => field_valid_language($langcode));
  // Merge default options.
  $default_options = array(
    'default' => FALSE,
    'deleted' => FALSE,
    'language' => NULL,
  );
  $options += $default_options;
  list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  $instances = _field_invoke_get_instances($entity_type, $bundle, $options);
  // Iterate through the instances.
  $return = array();
  foreach ($instances as $instance) {
    // field_info_field() is not available for deleted fields, so use
    // field_info_field_by_id().
    $field = field_info_field_by_id($instance['field_id']);
    $field_name = $field['field_name'];
    //If we are looking at our field type and specific widget type, and we are multiple entries
    if($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED){
      //Check just in case the button is here, and add another #submit function
      if(isset($form[$field['field_name']]['und']['add_more'])){
        // add a simple select list, this defaults to numb 3
        $form[$field['field_name']]['add_more_number'] = array(
          '#type' => 'select',
          '#title' => t('Add more no.'),
          '#options' => drupal_map_assoc(range(0, 50)),
          '#default_value' => 2,
        );
        $form[$field['field_name']]['und']['add_more']['#submit'][] = 'village_field_add_more_submit';
        $form[$field['field_name']]['und']['add_more']['#value'] = 'Add more rows';
      }
    }
  }
}

function village_field_add_more_submit($form, &$form_state){
  $button = $form_state['triggering_element'];
  // Go one level up in the form, to the widgets container.
  $element = drupal_array_get_nested_value($form, array_slice($button['#array_parents'], 0, -1));
  $field_name = $element['#field_name'];
  $langcode = $element['#language'];
  $parents = $element['#field_parents'];
  // Alter the number of widgets to show. items_count = 0 means 1.
  $field_state = field_form_get_state($parents, $field_name, $langcode, $form_state);
  //get the number from the select
  $numbtoadd = $form[$field_name]['add_more_number']['#value'];
  if($numbtoadd){
    $field_state['items_count'] += $numbtoadd;
    field_form_set_state($parents, $field_name, $langcode, $form_state, $field_state);
    $form_state['rebuild'] = TRUE;
  }
}

note, village_ is the custom module name in this case.
I think this is great oppertunity for a little module! something like field_add_x.module ? :)

El Alemaño’s picture

Issue summary: View changes

Hi digitisation,

I am trying to use your code, and i will like to do a module with that. But right now is not working right. The Add More button is working just one time, after that you can´t use it anymore. Also will be nice if the select could be together with the submit button. I try to do it like that... but it is not working good. (http://screencast.com/t/kL0PtO5boSlz)

Thanks!
El Alemaño

oryx’s picture

Worked perfectly for me (core 7.31), thank you so much! I actually called the module field_add_x ;)

8bitplateau’s picture

haha - good name.
I came across and issue with recursive field collection though (not a surprise) but I'm sure you can figure it out.

jmuzz’s picture

Status: Active » Closed (works as designed)

This is controlled by Drupal's handling of unlimited cardinality field items. A solution could be useful for any field but it is by no means a field collection specific issue.

El Alemaño’s picture

Status: Closed (works as designed) » Active

Hi,

I was using the "patch" but right now I can not delete any field_collection, because the save button of the form is gone when I delete on item of the field collection.
Any idea how I can fix that?

Thanks!

jmuzz’s picture

Project: Field collection »
Version: 7.x-1.x-dev »
davidwbarratt’s picture

Project: » Field collection
Version: » 7.x-1.x-dev

I have no idea what Core 7 is, so moving back to Field Collection.

Pepe Roni’s picture

Project: Field collection »
Version: 7.x-1.x-dev »

As stated in #9 this is an issue of the fields module in core, so it is an Core 7 issue.

In general, it is not a good idea to change metadata of an issue with the comment "I have no idea what ... is, so moving back to ..."!

davidwbarratt’s picture

Project: » Drupal core
Version: » 8.0.x-dev
Component: User interface » field system

Well thanks for your rude comment. The metadata was changed with absolutely no explanation, so I apologize for not being able to read people's minds.

Did you know that "Core 7" is a sandbox?
https://www.drupal.org/node/1244394

I'm assuming that's not what you meant?

Changing to the Drupal core issue queue since now I know what the intention was...

shomari’s picture

#2 and #5... Amazing! Thanks for your help. This is exactly what I needed.

I tweaked one line for my own use so that the change is only applicable to field collections
if($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED && $field['type'] == "field_collection"){

jmuzz’s picture

@davidwbarratt Thanks for updating the incorrect values. If you're confused about the status of an issue then try reading it's comment history as the information will often be there.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

preksha’s picture

#2 and #5 worked for me like a charm ! That's what I was looking for.

lazzyvn’s picture

Hello
I confirm it works with #2 o #5 with edit form but i want add it to field-collection/field-myfield/add/node/xxxx
appear button "add another item" add add multi line to form
how can i do it?

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Rom Soul’s picture

Thank you so much !
I was getting crazy waiting for each new item, now I still have to wait but I can create 50 items at a time :D

So if any beginner (like me) wants this excellent piece of code to work, but wonders where to insert it :
you have to create a new module .
And it's a 5mn job if you have an FTP access.

- create folder site/all/modules/helloworld on your drupal install
- create in your folder the file helloworld.module containing the php code in comment #5
replace in the code "village_" by "helloworld_"
- create another file helloworld.info containing :

name = Hello World
description = Description of what this module does
core = 7.x

- the module is created : activate it from the modules section
- edit a content that uses a field collection to check if it works

(just trying to summarize... official Drupal 7 documentation about creating modules is available with more details)

Kris77’s picture

#2 and #5 worked for me TOO. I use Drupal 7.56...
I also use only with field collection as @shomari.

Thanks to @ndeschildre and @8bitplateau.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

celtech’s picture

#5 is working for me on individual fields. Does anyone know how to get this to work with the Paragraphs module? I need to add multiple Paragraph fields to a node. I am using Drupal 7. Thanks!

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

gorav.singal’s picture

I also faced the same issue. I found a helpful article: https://www.gyanblog.com/gyan/drupal-how-add-multiple-fields-single-button/
I also used Paragraphs module. No coding required. This is just awesome.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Anybody’s picture

Version: 9.5.x-dev » 10.1.x-dev
Issue tags: +Site builder experience (SX), +ux
FileSize
2.62 KB
1.15 KB

Just ran into the same issue and found this one. When having a longer list of items to add, this is really painful and shouldn't be too hard to solve?

While "Add another item" currently has multiplicity of "1", we could instead prefix this button with a number (int) input field, prefilled with "1" in the style of a dropbutton toggle, but instead of havin the arrow part on the right, with a number on the left.

That way it would still be 1 click if only one line is needed, with great UX is more items are needed and it perfectly fits the Drupal UI:

I'm sorry this looks ugly in my screenshot, I'm not a designer ... -.- It should be turned around dropbutton style:

Perhaps someone can sketch that more beautiful? Do you like the idea?

Anybody’s picture

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.