class ImportForm in Term CSV Export Import 8.3
Same name and namespace in other branches
- 8 src/Form/ImportForm.php \Drupal\term_csv_export_import\Form\ImportForm
- 8.2 src/Form/ImportForm.php \Drupal\term_csv_export_import\Form\ImportForm
Class ImportForm.
@package Drupal\term_csv_export_import\Form
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\term_csv_export_import\Form\ImportForm implements FormInterface
Expanded class hierarchy of ImportForm
1 string reference to 'ImportForm'
File
- src/
Form/ ImportForm.php, line 19
Namespace
Drupal\term_csv_export_import\FormView source
class ImportForm extends FormBase implements FormInterface {
/**
* Set a var to make stepthrough form.
*
* @var step
*/
protected $step = 1;
/**
* Keep track of user input.
*
* @var userInput
*/
protected $userInput = [];
/**
* The vocabulary storage.
*
* @var \Drupal\taxonomy\VocabularyStorageInterface
*/
protected $vocabularyStorage;
/**
* Constructs a new vocabulary form.
*
* @param \Drupal\taxonomy\VocabularyStorageInterface $vocabulary_storage
* The vocabulary storage.
*/
public function __construct(VocabularyStorageInterface $vocabulary_storage) {
$this->vocabularyStorage = $vocabulary_storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager')
->getStorage('taxonomy_vocabulary'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'import_form';
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
switch ($this->step) {
case 1:
if ($form_state
->getValue('vocabulary') != 'create_new') {
$this->step++;
$this->userInput['vocabulary'] = $form_state
->getValue('vocabulary');
}
$this->userInput['preserve_vocabularies'] = $form_state
->getValue('preserve_vocabularies');
$this->userInput['preserve_tids'] = $form_state
->getValue('preserve_tids');
$this->userInput['input'] = $form_state
->getValue('input');
$form_state
->setRebuild();
break;
case 2:
$this->vocabulary = $this
->createVocab($form_state
->getValue('vid'), $form_state
->getValue('name'));
$this->userInput['vocabulary'] = $this->vocabulary;
$form_state
->setRebuild();
break;
case 3:
$import = new ImportController($this->userInput['input'], $this->userInput['vocabulary']);
$import
->execute($this->userInput['preserve_vocabularies'], $this->userInput['preserve_tids']);
break;
}
$this->step++;
}
/**
* {@inheritdoc}
*/
public function createVocab($vid, $name) {
$vocabulary = Vocabulary::create([
'vid' => $vid,
'machine_name' => $vid,
'name' => $name,
]);
$vocabulary
->save();
return $vocabulary
->id();
}
/**
* Determines if the vocabulary already exists.
*
* @param string $vid
* The vocabulary ID.
*
* @return bool
* TRUE if the vocabulary exists, FALSE otherwise.
*/
public function exists($vid) {
$action = $this->vocabularyStorage
->load($vid);
return !empty($action);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
ImportForm:: |
protected | property | Set a var to make stepthrough form. | |
ImportForm:: |
protected | property | Keep track of user input. | |
ImportForm:: |
protected | property | The vocabulary storage. | |
ImportForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
ImportForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
ImportForm:: |
public | function | ||
ImportForm:: |
public | function | Determines if the vocabulary already exists. | |
ImportForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ImportForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
ImportForm:: |
public | function | Constructs a new vocabulary form. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |