You are here

public function ImportForm::buildForm in Term CSV Export Import 8.3

Same name and namespace in other branches
  1. 8 src/Form/ImportForm.php \Drupal\term_csv_export_import\Form\ImportForm::buildForm()
  2. 8.2 src/Form/ImportForm.php \Drupal\term_csv_export_import\Form\ImportForm::buildForm()

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/ImportForm.php, line 70

Class

ImportForm
Class ImportForm.

Namespace

Drupal\term_csv_export_import\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['#title'] = $this
    ->t('CSV Term Import');
  switch ($this->step) {
    case 1:
      $form['input'] = [
        '#type' => 'textarea',
        '#title' => $this
          ->t('Input'),
        '#description' => $this
          ->t('<p><strong>See CSV Export for an example.</strong></p><p>Enter in the form of: <pre>"name,status,description,format,weight,parent_name,[any_additional_fields];</pre><pre>name,status,description,format,weight,parent_name[;parent_name1;parent_name2;...],[any_additional_fields]"</pre> or <pre>"tid,uuid,name,status,revision_id,description,format,weight,parent_name[;parent_name1;parent_name2;...],parent_tid[;parent_tid1;parent_tid2;...],[any_additional_fields];</pre><pre>tid,uuid,name,status,revision_id,description,format,weight,parent_name,parent_tid,[any_additional_fields]"</pre> Note that <em>[any_additional_fields]</em> are optional and are stringified using <a href="http://www.php.net/http_build_query">http_build_query</a>.</p><p>If you need to export from Drupal 7, you can import and modify the view d7exportview.txt found at the root directory of this project</p>'),
      ];
      $form['preserve_vocabularies'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Preserve Vocabularies on existing terms.'),
      ];
      $form['preserve_tids'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Preserve existing terms. This will prevent a term id collision if importing from another install.'),
      ];
      $vocabularies = taxonomy_vocabulary_get_names();
      $vocabularies['create_new'] = 'create_new';
      $form['vocabulary'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Taxonomy'),
        '#options' => $vocabularies,
      ];
      $value = $this
        ->t('Next');
      break;
    case 2:
      $form['name'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Name'),
        '#maxlength' => 255,
        '#required' => TRUE,
      ];
      $form['vid'] = [
        '#type' => 'machine_name',
        '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
        '#machine_name' => [
          'exists' => [
            $this,
            'exists',
          ],
          'source' => [
            'name',
          ],
        ],
      ];
      $form['#title'] .= ' - ' . $this
        ->t('Create New Vocabulary');
      $value = $this
        ->t('Create Vocabulary');
      break;
    case 3:
      $preserve = '';
      if ($this->userInput['preserve_tids']) {
        $preserve .= " and preserve existing terms";
      }
      if ($this->userInput['preserve_vocabularies']) {
        $preserve .= " and preserve vocabularies on existing terms";
      }
      $has_header = stripos($this->userInput['input'], "name,status,description__value,description__format,weight,parent_name");
      $term_count = count(array_filter(preg_split('/\\r\\n|\\r|\\n/', $this->userInput['input'])));
      if ($has_header !== FALSE) {
        $term_count = $term_count - 1;
      }
      $form['#title'] .= ' - ' . $this
        ->t('Are you sure you want to copy @count_terms terms into the vocabulary @vocabulary@preserve_vocabularies?', [
        '@count_terms' => $term_count,
        '@vocabulary' => $this->userInput['vocabulary'],
        '@preserve_vocabularies' => $preserve,
      ]);
      $value = $this
        ->t('Import');
      break;
  }
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $value,
  ];
  return $form;
}