ExportTermsMiniForm.php in Taxonomy Manager 2.0.x
File
src/Form/ExportTermsMiniForm.php
View source
<?php
namespace Drupal\taxonomy_manager\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\taxonomy\TermStorage;
use Drupal\taxonomy\VocabularyInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ExportTermsMiniForm extends FormBase {
protected $termStorage;
public function __construct(TermStorage $termStorage) {
$this->termStorage = $termStorage;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager')
->getStorage('taxonomy_term'));
}
public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL, $selected_terms = []) {
$form['voc'] = [
'#type' => 'value',
'#value' => $taxonomy_vocabulary,
];
$form['selected_terms']['#tree'] = TRUE;
$tree = $this->termStorage
->loadTree($taxonomy_vocabulary
->id());
$result = [];
foreach ($tree as $term) {
$result[] = str_repeat('-', $term->depth) . $term->name;
}
$desc = 'Term names with hierarchy: Only term names are exported. Child terms are prefixed with dashes.';
$desccsv = 'CSV: The comma-separated-values export has following columns: voc id | term id | term name | description | parent id 1 | ... | parent id n';
$form['exported_data'] = [
'#type' => 'textarea',
'#title' => $this
->t('Exported data'),
'#default_value' => implode("\n", $result),
'#rows' => 12,
'#prefix' => '<div id="export-wrapper">',
'#suffix' => '</div>',
'#description' => $desc,
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$taxonomy_vocabulary = $form_state
->getValue('voc');
$form_state
->setRedirect('taxonomy_manager.admin_vocabulary', [
'taxonomy_vocabulary' => $taxonomy_vocabulary
->id(),
]);
}
public function getFormId() {
return 'taxonomy_manager_export_form_list';
}
}