1

I am building a website using Drupal 7. In my website there is a form for user to add some content.

In the form there is a field of type "Text Field" (core). The "Number of values" for this text field is "unlimited". Thus, when the form appear in the first time,there is only one text field and a button- "add another item". Then users can add more items / fields just by clicking on the button. the problem is that every clicking takes time to process, and this is not best-practice for my opinion.

I want that always will appear at least 4 fields on the form (and not one).
If the users will want, they will be able to just click on the button and "add another item" (regularly).

How can I can display 4 text fields (and not only one)?

here is a screen-shot for example: enter image description here

2 Answers 2

0

Check Multivalue Settings Module

Warning: this module is still very buggy. Actually, it only works in very few use cases. I suggest to only attempt using it with core fields (not fields from contrib modules). Even this may not work. I'm working on fixing things, but don't have much time. Help is welcome.

Functionality:

Number of values allowed
Number of values required
Number of empty widgets to display initially (Drupal usually displays all fields for non-unlimited fields. This allows you to only display a few, followed by the 'Add More' button, which will disappear after the maximum number of fields has been filled.)
Number of empty widgets to display on edit forms after the field contains some values (Drupal usually displays one extra)

Though the module is buggy but can be a good starting point for building a custom solution or patching the same module.

1
  • I thank you for your help. The problem that my site is on production, and soon users will start to visit it. if this module is problematic - it is not the ideal solution. is there a code I can write on form to make it work?
    – EB84
    Commented Mar 22, 2014 at 10:37
0
    Better to Write your own custom form

    **for example**

        function ajax_example_menu() {
          $items = array();
        $items['examples/ajax_example/dependent_dropdown_degrades'] = array(
            'title' => 'Dependent dropdown (with graceful degradation)',
            'page callback' => 'drupal_get_form',
            'page arguments' => array('ajax_example_dependent_dropdown_degrades'),
            'access callback' => TRUE,
            'weight' => 5,
            'file' => 'ajax_example_graceful_degradation.inc',
          );
        return $items;
        }
    /**
     * Form with 'add more' and 'remove' buttons.
     **/
    function ajax_example_add_more($form, &$form_state, $no_js_use = FALSE) {
      $form['description'] = array(
        '#markup' => '<div>' . t('This example shows an add-more and a remove-last button. The <a href="!ajax">AJAX version</a> does it without page reloads; the <a href="!multistep">non-js version</a> is the same code but simulates a non-javascript environment, showing it with page reloads.',
          array('!ajax' => url('examples/ajax_example/add_more'), '!multistep' => url('examples/ajax_example/add_more_no_js')))
        . '</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('People coming to the picnic'),
        // 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'] = 2;
      }
      for ($i = 0; $i < $form_state['num_names']; $i++) {
        $form['names_fieldset']['name'][$i] = array(
          '#type' => 'textfield',
          '#title' => t('Name'),
        );
      }
      $form['names_fieldset']['add_name'] = array(
        '#type' => 'submit',
        '#value' => t('Add one more'),
        '#submit' => array('ajax_example_add_more_add_one'),
        // See the examples in ajax_example.module for more details on the
        // properties of #ajax.
        '#ajax' => array(
          'callback' => 'ajax_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('ajax_example_add_more_remove_one'),
          '#ajax' => array(
            'callback' => 'ajax_example_add_more_callback',
            'wrapper' => 'names-fieldset-wrapper',
          ),
        );
      }
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
      );

      // This simply allows us to demonstrate no-javascript use without
      // actually turning off javascript in the browser. Removing the #ajax
      // element turns off AJAX behaviors on that element and as a result
      // ajax.js doesn't get loaded.
      // For demonstration only! You don't need this.
      if ($no_js_use) {
        // Remove the #ajax from the above, so ajax.js won't be loaded.
        if (!empty($form['names_fieldset']['remove_name']['#ajax'])) {
          unset($form['names_fieldset']['remove_name']['#ajax']);
        }
        unset($form['names_fieldset']['add_name']['#ajax']);
      }

      return $form;
    }

    main thing u need to change code to get initially four text fields is 
    ****$form_state['num_names'] = 4;****

U can add your other fields in the same change accordingly as u need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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