1

I want a field that upon getting a value in a text field (eg. 1, 2 anything) shows respective fields. If user inputs 5, then five fields will appear (similar to multiple values of a field, unlimited)

How can I make this possible? There is a conditional module that provides UI for state API but is that enough for the working?

3
  • Conditional module can only work for the case all the fields are known.
    – Jimmy Ko
    Commented Oct 17, 2016 at 9:34
  • Drupal core more or less has this out of the box. When you select a field to have unlimited values, you get the feature. The only thing is that you don't select the amount on before hand, but you get the add another button. Why aren't you just using that?
    – googletorp
    Commented Oct 17, 2016 at 12:17
  • This is my my use case Commented Oct 18, 2016 at 16:24

3 Answers 3

1
+50

I think some of the options mentioned in this thread is kind of what you want maybe not the approach you wanted. For a form to optionally display more form elements you'll have to use state API or some sort of ajax form.

Take this form element for instance:

$form['advanced']['s3fs_use_cname'] = array(
    '#type'          => 'checkbox',
    '#title'         => t('Use a CNAME'),
    '#default_value' => _s3fs_get_setting('use_cname'),
    '#description'   => t('Serve files from a custom domain by using an appropriately named bucket, e.g. "mybucket.mydomain.com".'),
);

$form['advanced']['s3fs_cname_settings_fieldset'] = array(
        '#type'   => 'fieldset',
        '#title'  => t('CNAME Settings'),
        '#states' => array(
        'visible' => array(
            ':input[id=edit-s3fs-use-cname]' => array('checked' => TRUE),
        ),
    ),
    's3fs_domain' => array(
        '#type'          => 'textfield',
        '#title'         => t('CDN Domain Name'),
        '#default_value' => _s3fs_get_setting('domain'),
        '#description'   => t('If serving files from CloudFront, the bucket name can differ from the domain name.'),
    ),
);

This example from the s3fs module uses a check box to display a form input field. If we look at the documtation for drupal_process_states() we can see that as a remote conditions we can use value as a determining factor. So we can almost accomplish what you're asking from here. We would first need a input field

$form['field_instance_value'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Number of fields'),
    '#default_value' => NULL,
    '#description'   => t('This serves as the number of fields to display below.'),
);

This field is our instance control. On it's own with default values it won't do much. I set it to default to NULL in this case so we can determine if the user has filled in a value or not. Next we'll need to control the group of fields and each additional field. This might be better to do using an ajax form since you can dynamically add as many fields as you like based on the number but i won't cover that.

$form['field_instances'] = array(
    '#type'   => 'fieldset',
    '#title'  => t('Field Instances'),
    '#states' => array(
        'visible' => array(
            ':input[id=edit-field_instance_value]' => array('filled' => TRUE),
        )
    ),
);

$form['field_instances']['field_instance_one'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Field Instance one'),
    '#states' => array(
        'visible' => array(
            ':input[id=edit-field_instance_value]' => array('value' => 1),
        )
    ),
);

$form['field_instances']['field_instance_two'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Field Instance two'),
    '#states' => array(
        'visible' => array(
            ':input[id=edit-field_instance_value]' => array('value' => 2),
        )
    ),
);

So from the above example you can see that we have two control states, the first being on the fieldset containing the field instances and the second being on the instances themselves. The idea here is that we want to make sure that the instances can be seen if there is a value in the field, then we want to only show the field instances that the form 'field_instance_value' element specifies. You could even put in a default value here if the value of the input is empty or something else.

Keep in mind this code isn't tested just what i've come up with based on your question and years of working with Drupal's Form API. Below are some references i used.

1
  • 1
    Brilliant Answer. Commented Oct 22, 2016 at 3:58
1

You can ref Examples for Developers Module. The module comes with various sub-modules for developers to start with.

Similar kind of functionality is already provided as an example in "AJAX Example" module in Examples module. i.e. User has been given a dropdown field with predefined numbers to render those many fields. Check screenshot below: Generate Checkboxes

Hope this helps.

2
  • I will try that Commented Oct 18, 2016 at 16:25
  • Tried this but it's not working for file widget. Can you shed some more light on that bhai? Commented Oct 21, 2016 at 15:22
0

assuming that the fields you want to repeat are text fields.

//the form

function module_form($form,&$form_state){


    $form['first_box']=array(
    '#type' => 'textfield',
    '#title' => t('enter the number'),
    '#ajax' => array(
    'callback' => 'module_callback',
    'wrapper' => 'my-wrapper',
),
);

$form['multiple_box']=array(
    '#prefix' => '<div id="my-wrapper">',
    '#suffix' => '</div>',
);
return $form;
}
 function module_callback($form,&$form_state){
    $temp=$form_state['values']['first_box'];
    $page=array();
    while($temp>0){
        $page[]=array(
        '#type' => 'textfield',
        '#title' => t('name'),
        );
        $temp=$temp-1;
}
    return $page;
}
2
  • I don't think it is the answer. The question is asking about the multi value field on entity add / editor form.
    – Jimmy Ko
    Commented Oct 18, 2016 at 12:58
  • Tried this, but as I said I need file widget to appear multiple times. Plus as I asked the references are to same field (say certificate), all added files should be referenced to that certificate field. Any help? Commented Oct 21, 2016 at 15:22

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.