class SimpleNodeConfirmImportForm in Simple Node Importer 8
Defines a confirmation form to confirm deletion of something by id.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfirmFormBase implements ConfirmFormInterface
- class \Drupal\simple_node_importer\Form\SimpleNodeConfirmImportForm
- class \Drupal\Core\Form\ConfirmFormBase implements ConfirmFormInterface
Expanded class hierarchy of SimpleNodeConfirmImportForm
1 string reference to 'SimpleNodeConfirmImportForm'
File
- src/
Form/ SimpleNodeConfirmImportForm.php, line 20
Namespace
Drupal\simple_node_importer\FormView source
class SimpleNodeConfirmImportForm extends ConfirmFormBase {
protected $services;
protected $sessionVariable;
protected $sessionManager;
protected $currentUser;
protected $entityTypeManager;
/**
* Confirmation form for the start node import.
*
* @param Drupal\simple_node_importer\Services\GetServices $getServices
* Constructs a Drupal\simple_node_importer\Services object.
* @param Drupal\Core\TempStore\PrivateTempStoreFactory $sessionVariable
* Constructs a Drupal\Core\TempStore\PrivateTempStoreFactory object.
* @param Drupal\Core\Session\SessionManagerInterface $session_manager
* Constructs a Drupal\Core\Session\SessionManagerInterface object.
* @param Drupal\Core\Session\AccountInterface $current_user
* Constructs a Drupal\Core\Session\AccountInterface object.
* @param Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Constructs a Drupal\Core\Entity\EntityTypeManagerInterface object.
*/
public function __construct(GetServices $getServices, PrivateTempStoreFactory $sessionVariable, SessionManagerInterface $session_manager, AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager) {
$this->services = $getServices;
$this->sessionVariable = $sessionVariable
->get('simple_node_importer');
$this->sessionManager = $session_manager;
$this->currentUser = $current_user;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, string $type = NULL, NodeInterface $node = NULL) {
$this->node = $node;
$form['snp_nid'] = [
'#type' => 'hidden',
'#value' => $node
->id(),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Remove unnecessary values.
$form_state
->cleanValues();
$node_storage = $this->entityTypeManager
->getStorage('node');
$file_storage = $this->entityTypeManager
->getStorage('file');
$snp_nid = $form_state
->getValue('snp_nid');
$node = $node_storage
->load($snp_nid);
$bundleType = $node
->get('field_select_content_type')
->getValue()[0]['value'];
$map_values = $this->sessionVariable
->get('mapvalues');
$fid = $node
->get('field_upload_csv')
->getValue()[0]['target_id'];
$file = $file_storage
->load($fid);
$csv_uri = $file
->getFileUri();
$handle = fopen($csv_uri, 'r');
$columns = [];
$columns = array_values($this->services
->simpleNodeImporterGetAllColumnHeaders($csv_uri));
$record = [];
$map_fields = array_keys($map_values);
$i = 1;
while ($row = fgetcsv($handle)) {
if ($i == 1) {
$i++;
continue;
}
foreach ($row as $k => $field) {
$column1 = str_replace(' ', '_', strtolower($columns[$k]));
foreach ($map_fields as $field_name) {
if ($map_values[$field_name] == $column1) {
$record[$field_name] = trim($field);
}
else {
if (is_array($map_values[$field_name])) {
$multiple_fields = array_keys($map_values[$field_name]);
foreach ($multiple_fields as $j => $m_fields) {
if ($m_fields == $column1) {
if (!empty($field)) {
$record[$field_name][$j] = trim($field);
}
else {
$record[$field_name][$j] = NULL;
}
}
}
}
}
}
}
$record['nid'] = $node
->id();
$record['type'] = $bundleType;
$records[] = $record;
}
// Preapring batch parmeters to be execute.
$batch = [
'title' => t('Importing content to :bundleType.', [
':bundleType' => $bundleType,
]),
'operations' => [
[
'\\Drupal\\simple_node_importer\\Controller\\NodeImportController::simpleNodeCreate',
[
$records,
],
],
],
'finished' => '\\Drupal\\simple_node_importer\\Controller\\NodeImportController::nodeImportBatchFinished',
'init_message' => t('Initialializing content importing.'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('Node creation has encountered an error.'),
];
// Set the batch operation.
batch_set($batch);
fclose($handle);
}
/**
* {@inheritdoc}
*/
public function getFormId() : string {
return "simple_node_confirm_importing_form";
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
$bundleType = $this->node
->get('field_select_content_type')
->getValue()[0]['value'];
$nid = $this->node
->id();
$parameters = [
'option' => $bundleType,
'node' => $nid,
];
return new Url('simple_node_importer.node_mapping_form', $parameters);
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
$critical_info = new FormattableMarkup('<p class="@class">If email id\'s provided in the "Authored By" column of your CSV match the existing users in the system, then data will be automatically imported. If not, the users will have to be created before importing the data.</p><p>Do you want to continue?</p>', [
"@class" => "confirmation-info",
]);
return $this
->t("@critical_info", [
"@critical_info" => $critical_info,
]);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('snp.get_services'), $container
->get('user.private_tempstore'), $container
->get('session_manager'), $container
->get('current_user'), $container
->get('entity_type.manager'));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfirmFormBase:: |
public | function |
Returns a caption for the link which cancels the action. Overrides ConfirmFormInterface:: |
1 |
ConfirmFormBase:: |
public | function |
Returns a caption for the button that confirms the action. Overrides ConfirmFormInterface:: |
20 |
ConfirmFormBase:: |
public | function |
Returns additional text to display as a description. Overrides ConfirmFormInterface:: |
11 |
ConfirmFormBase:: |
public | function |
Returns the internal name used to refer to the confirmation item. Overrides ConfirmFormInterface:: |
|
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 |
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. | |
SimpleNodeConfirmImportForm:: |
protected | property | ||
SimpleNodeConfirmImportForm:: |
protected | property | ||
SimpleNodeConfirmImportForm:: |
protected | property | ||
SimpleNodeConfirmImportForm:: |
protected | property | ||
SimpleNodeConfirmImportForm:: |
protected | property | ||
SimpleNodeConfirmImportForm:: |
public | function |
Form constructor. Overrides ConfirmFormBase:: |
|
SimpleNodeConfirmImportForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
SimpleNodeConfirmImportForm:: |
public | function |
Returns the route to go to if the user cancels the action. Overrides ConfirmFormInterface:: |
|
SimpleNodeConfirmImportForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
SimpleNodeConfirmImportForm:: |
public | function |
Returns the question to ask the user. Overrides ConfirmFormInterface:: |
|
SimpleNodeConfirmImportForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
SimpleNodeConfirmImportForm:: |
public | function | Confirmation form for the start node import. | |
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. |