1

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?

0
+50

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;
    }
  }
}
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.