By iStryker


2019-05-09 19:34:39 8 Comments

In Drupal 7, When you set a field "Number of values" to unlimited, it will show you 1 field on the creation page with an add more button. How would one go about so it shows 5 empty fields on the creation page?

2 comments

@iStryker 2019-05-14 18:23:44

With Stef help, I figured it out for D7

/**
 * Implements hook_form_alter().
 */
function MYMODULE_form_alter(&$form, &$form_state, $form_id)
  if ($form_id == 'MYNODE_node_form') {
    $item = $form['field_NAME'][LANGUAGE_NONE][0];
    // Add a minimum of 5 fields.
    for ($n=1; $n<5; $n++) {
    if(!isset($form['field_NAME'][LANGUAGE_NONE][$n])) {
      $form['field_NAME'][LANGUAGE_NONE][$n] = $item;
    }
  }
}

Now the only problem is it'll grab field 1, so if field 1 has a value in it, it'll get copied, to the 2nd, then 3rd, etc. If I figure that out I will update my answer. For now, I hope this helps others.

@Stef Van Looveren 2019-05-14 14:32:03

Have tested this in Drupal 8 but I think it is the same for drupal 7.

if($form_id == 'node_MYCONTENTTYPE_form') {
  $item = $form['FIELD_MY_FIELD_NAME']['widget'][0];
  for ($n=1; $n<5; $n++) {
    if(!isset($form['FIELD_MY_FIELD_NAME']['widget'][$n])) {
      $form['FIELD_MY_FIELD_NAME']['widget'][$n] = $item;
    }
  }
}

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Custom form with multiple textarea fields

  • 2016-08-07 12:22:42
  • Fever
  • 453 View
  • 1 Score
  • 1 Answer
  • Tags:   7 forms

0 Answered Questions

Custom Field API with multiple values - form generates two fields by default

  • 2017-07-06 14:15:24
  • David
  • 168 View
  • 1 Score
  • 0 Answer
  • Tags:   7 entities hooks

0 Answered Questions

How to store data in multiple "field collection table" fields

  • 2016-09-16 13:29:55
  • user66981
  • 266 View
  • 1 Score
  • 0 Answer
  • Tags:   7 services json

2 Answered Questions

[SOLVED] Make drupal cck field behave as "an add more button"?

  • 2013-05-06 06:14:04
  • kenpeter
  • 607 View
  • 1 Score
  • 2 Answer
  • Tags:   7 entities

1 Answered Questions

Display 3 items by default in a multivalue field?

  • 2016-03-21 09:50:53
  • Ahmad
  • 48 View
  • 2 Score
  • 1 Answer
  • Tags:   7 entities

2 Answered Questions

[SOLVED] Programmatically creating multivalue field with a button of add another

  • 2016-03-04 06:25:40
  • Ahmad
  • 1222 View
  • 1 Score
  • 2 Answer
  • Tags:   7 entities

2 Answered Questions

[SOLVED] Productivity tool to help me add multiple fields at once

  • 2015-04-06 20:25:14
  • joejam
  • 708 View
  • 3 Score
  • 2 Answer
  • Tags:   7 entities

1 Answered Questions

[SOLVED] Unlimited value fields numbered

  • 2014-05-14 17:00:54
  • Andrew Morris
  • 193 View
  • 0 Score
  • 1 Answer
  • Tags:   7 entities

1 Answered Questions

[SOLVED] Form field with support for multiple values

  • 2012-05-09 22:07:27
  • mtrolle
  • 6472 View
  • 3 Score
  • 1 Answer
  • Tags:   forms 7

1 Answered Questions

[SOLVED] CCK field value count is never less than 1

  • 2011-03-22 00:05:31
  • user688
  • 265 View
  • 2 Score
  • 1 Answer
  • Tags:   7 entities

Sponsored Content