You are here

public function ExportTermsForm::buildForm in Taxonomy Manager 2.0.x

Same name and namespace in other branches
  1. 8 src/Form/ExportTermsForm.php \Drupal\taxonomy_manager\Form\ExportTermsForm::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/ExportTermsForm.php, line 49

Class

ExportTermsForm
Form for exporting given terms.

Namespace

Drupal\taxonomy_manager\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL, $selected_terms = []) {

  // Cache form state so that we keep the parents in the modal dialog.
  // $form_state->setCached(TRUE);
  $form['voc'] = [
    '#type' => 'value',
    '#value' => $taxonomy_vocabulary,
  ];
  $form['selected_terms']['#tree'] = TRUE;
  $items = [];
  foreach ($selected_terms as $t) {
    $term = $this->termStorage
      ->load($t);
    $items[] = $term
      ->getName();
    $form['selected_terms'][$t] = [
      '#type' => 'value',
      '#value' => $t,
    ];
  }
  if (count($items)) {
    $form['terms'] = [
      '#theme' => 'item_list',
      '#items' => $items,
      '#title' => $this
        ->t('Selected terms for export:'),
    ];
  }

  // Terms to export element.
  $selectedExportType = 'whole';
  $exportType = [
    'whole' => $this
      ->t('Whole Vocabulary'),
    'root' => $this
      ->t('Root level terms only'),
  ];
  if (!empty($selected_terms)) {
    $selectedExportType = 'child';
    $exportType['child'] = $this
      ->t('Child terms of a selected term');
  }
  $form['export_type'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Terms to export'),
    '#options' => $exportType,
    '#default_value' => $selectedExportType,
  ];
  $form['exported_data'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Exported data'),
    '#default_value' => 'dv',
    '#rows' => 6,
    '#prefix' => '<div id="export-wrapper">',
    '#suffix' => '</div>',
  ];
  $actionUrl = Url::fromRoute('taxonomy_manager.admin_vocabulary.export', [
    'taxonomy_vocabulary' => $taxonomy_vocabulary
      ->id(),
  ])
    ->toString();
  $form['#action'] = $actionUrl;
  $form['export'] = [
    '#type' => 'button',
    '#value' => $this
      ->t('Export'),
    '#ajax' => [
      'callback' => '::exportTerms',
      // 'callback' => [get_called_class(), 'exportTerms'],
      'wrapper' => 'export-wrapper',
      'event' => 'click',
      // 'disable-refocus' => FALSE,
      //        'progress' => [
      //          'type' => 'throbber',
      //          'message' => $this->t('Exporting...'),
      //        ],
      'url' => $actionUrl,
      'options' => [
        'query' => [
          FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
        ],
      ],
    ],
  ];
  $form['export']['#ajax']['options']['query'] += \Drupal::request()->query
    ->all();
  return $form;
}