Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By ykarthikvarma on
Hello,
I have been trying to add couple of fields dynamically in a custom form in Drupal 8 but din't have luck till now. Could anyone please help me out !
The following is the ways I have tried with no luck
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#tree'] = TRUE;
$form['names_fieldset'] = array(
'#type' => 'fieldset',
'#prefix' => '<div id="names-fieldset-wrapper">',
'#suffix' => '</div>',
);
$num_names = $form_state->getValue('num_names');
if (empty($num_names)) {
$num_names = 1;
}
for ($i = 0; $i < $num_names; $i++) {
$form['names_fieldset'][$i] = array(
'#prefix' => '<div class="two-col">',
'#suffix' => '</div>'
);
$form['names_fieldset'][$i]['from'] = array(
'#type' => 'textfield',
'#title' => 'From',
);
$form['names_fieldset'][$i]['to'] = array(
'#type' => 'textfield',
'#title' => 'To',
);
}
$form['names_fieldset']['more_fields'] = array(
'#type' => 'submit',
'#value' => t('Add one'),
'#submit' => array($this, 'modulename_add_more_add_one'),
'#ajax' => array(
'callback' => array($this, 'modulename_add_more_callback'),
'wrapper' => 'names-fieldset-wrapper',
),
);
if ($num_names > 1) {
$form['names_fieldset']['less_fields'] = array(
'#type' => 'submit',
'#value' => t('Remove one'),
'#submit' => array($this, 'modulename_add_more_remove_one'),
'#ajax' => array(
'callback' => array($this, 'modulename_add_more_callback'),
'wrapper' => 'names-fieldset-wrapper',
),
);
}
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#submit' => array($this, 'CustomSubmit'),
);
return $form;
}
public function CustomSubmit(array &$form, FormStateInterface $form_state) {
drupal_set_message("Custom Submit triggered");
}
public function modulename_add_more_callback(array &$form, FormStateInterface $form_state) {
return $form['names_fieldset'];
}
public function modulename_add_more_add_one(array &$form, FormStateInterface $form_state) {
$num_names++;
$form_state['rebuild'] = TRUE;
}
public function modulename_add_more_remove_one(array &$form, FormStateInterface $form_state) {
if ($num_names > 1) {
$num_names--;
}
$form_state['rebuild'] = TRUE;
}
Reference Used : Single field dynamically added in form in Drupal 8 (Provided by Drupal Contrib)
http://www.drupalcontrib.org/api/drupal/contributions%21examples%21form_example%21form_example_tutorial.inc/function/form_example_tutorial_9/8
Reference Used : Set of fields dynamically added in form in Drupal 7 (Provided by Drupal Contrib)
http://lovewithbug.com/technology/drupal/how-to-use-drupal-ajax-form-for-multiple-fields-with-add-more-concept/
Couple of the first issues using similar dynamic fields as mentioned in the article is using "$form_state['num_names']"
Error: Cannot use object of type Drupal\\Core\\Form\\FormState as array
&
PHP Fatal error: Can't use method return value in write context
Comments
Any ideas on the issue I
Any ideas on the issue I posted ?
You have these erros because
You have these erros because you try to use form_state as an Array, that is the drupal 7 way. in Drupal 8, the form_state is an Object, so you need to use the method get().
exemple : $form_state->get('form_element_you_want')
Thanks man !
Thanks man !
@ykarthikvarma did you ever
@ykarthikvarma did you ever get this script working?