0
  public function buildForm(array $form, FormStateInterface $form_state) {
          $form = parent::buildForm($form, $form_state);
          $config = $this->config('category_dynamic_block.settings');

          $form['section_title'] = array(
            '#type' => 'textfield',
            '#title' => $this->t('Section Title'),
            '#description' => $this->t('Enter a Section Title'),
          );

          $form['term_name'] = array(
            '#type' => 'entity_autocomplete',
            '#target_type' => 'taxonomy_term',
            '#selection_settings' => [
                 'target_bundles' => array('categories'),
            ],
            '#title' => $this->t('Term Name'),
              '#description' => $this->t('Enter a Category Vocab Term Name'),
          );

          $form['page_title'] = array(
            '#type' => 'entity_autocomplete',
            '#target_type' => 'node',
            '#selection_settings' => [
                'target_bundles' => array('article'),
            ],
            '#title' => $this->t('Page Title (' . $i . ')'),
            '#description' => $this->t('Enter Page Title to be displayed'),
          );

          return $form;}

I'm creating a configuration form and I'm trying to find if there is a way in drupal 8 which will allow the user to enter multiple values for $form['page_title'] field.

  • Please be more specific as what you want to do. Do you want to add more fields to your form or do you want to save multiple values to an entity reference field? Or both? –  Jan 08 '18 at 15:00
  • I want to be able to save multiple values to an entity reference field in an config form. – Srijeeta Ghosh Jan 10 '18 at 05:21
  • How? Your form currently does not allow the adding of multiple values? Do you want to add more fields for more values? Do you want someone to add multiple values in one field seperated by a symbol like "," or ...? –  Jan 10 '18 at 09:43
  • We can use any of the two ways, but that field($form['page_title']) should be able to store more than one value. – Srijeeta Ghosh Jan 11 '18 at 10:12

1 Answers1

-1

This question (unlimited text fields with form api) may be what you are looking for: https://drupal.stackexchange.com/questions/208012/unlimited-textfields-with-form-api

Basically you'll need to add some ajax:

'#ajax' => [
  'callback' => array($this, 'addMultipleItems'),
  'event' => 'change',
  'progress' => array(
    'type' => 'throbber',
    'message' => t('Adding another item...'),
  ),
],
cfox
  • 431
  • 2
  • 6
  • 18