You are here

public function ImporterForm::buildForm in CSV Importer 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/ImporterForm.php, line 111

Class

ImporterForm
Provides CSV importer form.

Namespace

Drupal\csv_importer\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['importer'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'csv-importer',
    ],
  ];
  $form['importer']['entity_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Select entity type'),
    '#required' => TRUE,
    '#options' => $this
      ->getEntityTypeOptions(),
    '#weight' => 0,
    '#ajax' => [
      'callback' => [
        $this,
        'getContentEntityTypesAjaxForm',
      ],
      'wrapper' => 'csv-importer',
      'event' => 'change',
    ],
  ];
  if ($entity_type = $form_state
    ->getValue('entity_type')) {
    if ($options = $this
      ->getEntityTypeBundleOptions($entity_type)) {
      $form['importer']['entity_type_bundle'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Select entity bundle'),
        '#options' => $options,
        '#required' => TRUE,
        '#weight' => 5,
      ];
    }
    $options = $this
      ->getEntityTypeImporterOptions($entity_type);
    $form['importer']['plugin_id'] = [
      '#type' => 'hidden',
      '#value' => key($options),
    ];
    if (count($options) > 1) {
      $form['importer']['plugin_id'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Select importer'),
        '#options' => $options,
        '#default_value' => 0,
        '#weight' => 25,
      ];
    }
    $form['importer']['delimiter'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Select delimiter'),
      '#options' => [
        ',' => ',',
        '~' => '~',
        ';' => ';',
        ':' => ':',
      ],
      '#default_value' => ',',
      '#required' => TRUE,
      '#weight' => 10,
    ];
  }
  $form['importer']['csv'] = [
    '#type' => 'managed_file',
    '#title' => $this
      ->t('Select CSV file'),
    '#required' => TRUE,
    '#autoupload' => TRUE,
    '#upload_validators' => [
      'file_validate_extensions' => [
        'csv',
      ],
    ],
    '#weight' => 10,
  ];
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Import'),
    '#button_type' => 'primary',
  ];
  return $form;
}