Managed file form element

The other obligatory method we need to implement is getConfigurationForm(), by which we define the form elements needed to configure this particular plugin. Here, we will create the file field allowing users to upload the CSV file:

/**
 * {@inheritdoc}
 */
public function getConfigurationForm(\Drupal\products\Entity\ImporterInterface $importer) {
  $form = [];
  $config = $importer->getPluginConfiguration();
  $form['file'] = [
    '#type' => 'managed_file',
    '#default_value' => isset($config['file']) ? $config['file'] : '',
    '#title' => $this->t('File'),
    '#description' => $this->t('The CSV file containing the product records.'),
    '#required' => TRUE,
  ];

  return $form;
}

The form element type is called managed_file (implemented ...

Get Drupal 8 Module Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.