class ContentImport in Content Import 8.3
Same name and namespace in other branches
- 8.9 src/Form/ContentImport.php \Drupal\contentimport\Form\ContentImport
- 8 src/Form/ContentImport.php \Drupal\contentimport\Form\ContentImport
- 8.4 src/Form/ContentImport.php \Drupal\contentimport\Form\ContentImport
Configure Content Import settings for this site.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\contentimport\Form\ContentImport
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of ContentImport
1 string reference to 'ContentImport'
File
- src/
Form/ ContentImport.php, line 18
Namespace
Drupal\contentimport\FormView source
class ContentImport extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'contentimport';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'contentimport.settings',
];
}
/**
* Content Import Form.
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$contentTypes = ContentImportController::getAllContentTypes();
$selected = 0;
$form['contentimport_contenttype'] = [
'#type' => 'select',
'#title' => $this
->t('Select Content Type'),
'#options' => $contentTypes,
'#default_value' => $selected,
];
$form['file_upload'] = [
'#type' => 'file',
'#title' => $this
->t('Import CSV File'),
'#size' => 40,
'#description' => $this
->t('Select the CSV file to be imported.'),
'#required' => FALSE,
'#autoupload' => TRUE,
'#upload_validators' => [
'file_validate_extensions' => [
'csv',
],
],
];
$form['loglink'] = [
'#type' => 'link',
'#title' => $this
->t('Check Log..'),
'#url' => Url::fromUri('base:sites/default/files/contentimportlog.txt'),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Import'),
'#button_type' => 'primary',
];
return parent::buildForm($form, $form_state);
}
/**
* Content Import Form Submission.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$contentType = $form_state
->getValue('contentimport_contenttype');
ContentImport::createNode($_FILES, $contentType);
}
/**
* To get all Content Type Fields.
*/
public function getFields($contentType) {
$fields = [];
foreach (\Drupal::entityManager()
->getFieldDefinitions('node', $contentType) as $field_definition) {
if (!empty($field_definition
->getTargetBundle())) {
$fields['name'][] = $field_definition
->getName();
$fields['type'][] = $field_definition
->getType();
$fields['setting'][] = $field_definition
->getSettings();
}
}
return $fields;
}
/**
* To get Reference field ids.
*/
public function getTermReference($voc, $terms) {
$vocName = strtolower($voc);
$vid = preg_replace('@[^a-z0-9_]+@', '_', $vocName);
$vocabularies = Vocabulary::loadMultiple();
/* Create Vocabulary if it is not exists */
if (!isset($vocabularies[$vid])) {
ContentImport::createVoc($vid, $voc);
}
$termArray = array_map('trim', explode(',', $terms));
$termIds = [];
foreach ($termArray as $term) {
$term_id = ContentImport::getTermId($term, $vid);
if (empty($term_id)) {
$term_id = ContentImport::createTerm($voc, $term, $vid);
}
$termIds[]['target_id'] = $term_id;
}
return $termIds;
}
/**
* To Create Terms if it is not available.
*/
public function createVoc($vid, $voc) {
$vocabulary = Vocabulary::create([
'vid' => $vid,
'machine_name' => $vid,
'name' => $voc,
]);
$vocabulary
->save();
}
/**
* To Create Terms if it is not available.
*/
public function createTerm($voc, $term, $vid) {
Term::create([
'parent' => [
$voc,
],
'name' => $term,
'vid' => $vid,
])
->save();
$termId = ContentImport::getTermId($term, $vid);
return $termId;
}
/**
* To get Termid available.
*/
public function getTermId($term, $vid) {
$termRes = db_query('SELECT n.tid FROM {taxonomy_term_field_data} n WHERE n.name = :uid AND n.vid = :vid', [
':uid' => $term,
':vid' => $vid,
]);
foreach ($termRes as $val) {
$term_id = $val->tid;
}
return $term_id;
}
/**
* To get node available.
*/
public function getNodeId($title) {
$nodeReference = array();
$db = \Drupal::database();
foreach ($title as $key => $value) {
$query = $db
->select('node_field_data', 'n');
$query
->fields('n', array(
'nid',
));
$nodeId = $query
->condition('n.title', trim($value))
->execute()
->fetchField();
$nodeReference[$key]['target_id'] = $nodeId;
}
return $nodeReference;
}
/**
* To get user information based on emailIds.
*/
public static function getUserInfo($userArray) {
$uids = [];
foreach ($userArray as $usermail) {
if (filter_var($usermail, FILTER_VALIDATE_EMAIL)) {
$users = \Drupal::entityTypeManager()
->getStorage('user')
->loadByProperties([
'mail' => $usermail,
]);
}
else {
$users = \Drupal::entityTypeManager()
->getStorage('user')
->loadByProperties([
'name' => $usermail,
]);
}
$user = reset($users);
if ($user) {
$uids[] = $user
->id();
}
else {
$user = User::create();
$user->uid = '';
$user
->setUsername($usermail);
$user
->setEmail($usermail);
$user
->set("init", $usermail);
$user
->enforceIsNew();
$user
->activate();
$user
->save();
$users = \Drupal::entityTypeManager()
->getStorage('user')
->loadByProperties([
'mail' => $usermail,
]);
$uids[] = $user
->id();
}
}
return $uids;
}
/**
* To import data as Content type nodes.
*/
public function createNode($filedata, $contentType) {
drupal_flush_all_caches();
global $base_url;
$logFileName = "contentimportlog.txt";
$logFile = fopen("sites/default/files/" . $logFileName, "w") or die("There is no permission to create log file. Please give permission for sites/default/file!");
$fields = ContentImport::getFields($contentType);
$fieldNames = $fields['name'];
$fieldTypes = $fields['type'];
$fieldSettings = $fields['setting'];
// Code for import csv file.
$mimetype = 1;
if ($mimetype) {
$location = $filedata['files']['tmp_name']['file_upload'];
if (($handle = fopen($location, "r")) !== FALSE) {
$keyIndex = [];
$index = 0;
$logVariationFields = "***************************** Content Import Begins ************************************ \n \n ";
// $node = \Drupal\node\Entity\Node::load(117);
while (($data = fgetcsv($handle)) !== FALSE) {
$index++;
if ($index < 2) {
array_push($fieldNames, 'title');
array_push($fieldTypes, 'text');
array_push($fieldNames, 'langcode');
array_push($fieldTypes, 'lang');
if (!isset($data['langcode'])) {
$logVariationFields .= "Langcode missing --- Assuming EN as default langcode.. Import continues \n \n";
$data[count($data)] = 'langcode';
}
foreach ($fieldNames as $fieldValues) {
$i = 0;
foreach ($data as $dataValues) {
if ($fieldValues == $dataValues) {
$logVariationFields .= "Data Type : " . $fieldValues . " Matches \n";
$keyIndex[$fieldValues] = $i;
}
$i++;
}
}
continue;
}
if (!isset($keyIndex['title']) || !isset($keyIndex['langcode'])) {
drupal_set_message($this
->t('title or langcode is missing in CSV file. Please add these fields and import again'), 'error');
$url = $base_url . "/admin/config/content/contentimport";
header('Location:' . $url);
exit;
}
$logVariationFields .= "********************************* Importing node **************************** \n \n";
for ($f = 0; $f < count($fieldNames); $f++) {
switch ($fieldTypes[$f]) {
case 'image':
$logVariationFields .= "Importing Image (" . trim($data[$keyIndex[$fieldNames[$f]]]) . ") :: ";
if (!empty($data[$keyIndex[$fieldNames[$f]]])) {
$imgIndex = trim($data[$keyIndex[$fieldNames[$f]]]);
$files = glob('sites/default/files/' . $contentType . '/images/' . $imgIndex);
$fileExists = file_exists('sites/default/files/' . $imgIndex);
if (!$fileExists) {
$images = [];
foreach ($files as $file_name) {
$image = File::create([
'uri' => 'public://' . $contentType . '/images/' . basename($file_name),
]);
$image
->save();
$images[basename($file_name)] = $image;
$imageId = $images[basename($file_name)]
->id();
$imageName = basename($file_name);
}
$nodeArray[$fieldNames[$f]] = [
[
'target_id' => $imageId,
'alt' => $nodeArray['title'],
'title' => $nodeArray['title'],
],
];
$logVariationFields .= "Image uploaded successfully \n ";
}
}
$logVariationFields .= " Success \n";
break;
case 'entity_reference':
$logVariationFields .= "Importing Reference Type (" . $fieldSettings[$f]['target_type'] . ") :: ";
if ($fieldSettings[$f]['target_type'] == 'taxonomy_term') {
$reference = explode(":", $data[$keyIndex[$fieldNames[$f]]]);
if (is_array($reference) && $reference[0] != '') {
$terms = ContentImport::getTermReference($reference[0], $reference[1]);
$nodeArray[$fieldNames[$f]] = $terms;
}
}
elseif ($fieldSettings[$f]['target_type'] == 'user') {
$userArray = explode(', ', $data[$keyIndex[$fieldNames[$f]]]);
$users = ContentImport::getUserInfo($userArray);
$nodeArray[$fieldNames[$f]] = $users;
}
elseif ($fieldSettings[$f]['target_type'] == 'node') {
$nodeArrays = explode(':', $data[$keyIndex[$fieldNames[$f]]]);
$nodeReference1 = ContentImport::getNodeId($nodeArrays);
$nodeArray[$fieldNames[$f]] = $nodeReference1;
}
$logVariationFields .= " Success \n";
break;
case 'text_long':
case 'text':
$logVariationFields .= "Importing Content (" . $fieldNames[$f] . ") :: ";
$nodeArray[$fieldNames[$f]] = [
'value' => $data[$keyIndex[$fieldNames[$f]]],
'format' => 'full_html',
];
$logVariationFields .= " Success \n";
break;
case 'entity_reference_revisions':
case 'text_with_summary':
$logVariationFields .= "Importing Content (" . $fieldNames[$f] . ") :: ";
$nodeArray[$fieldNames[$f]] = [
'summary' => substr(strip_tags($data[$keyIndex[$fieldNames[$f]]]), 0, 100),
'value' => $data[$keyIndex[$fieldNames[$f]]],
'format' => 'full_html',
];
$logVariationFields .= " Success \n";
break;
case 'datetime':
$logVariationFields .= "Importing Datetime (" . $fieldNames[$f] . ") :: ";
$dateArray = explode(':', $data[$keyIndex[$fieldNames[$f]]]);
if (count($dateArray) > 1) {
$dateTimeStamp = strtotime($data[$keyIndex[$fieldNames[$f]]]);
$newDateString = date('Y-m-d\\TH:i:s', $dateTimeStamp);
}
else {
$dateTimeStamp = strtotime($data[$keyIndex[$fieldNames[$f]]]);
$newDateString = date('Y-m-d', $dateTimeStamp);
}
$nodeArray[$fieldNames[$f]] = [
"value" => $newDateString,
];
$logVariationFields .= " Success \n";
break;
case 'timestamp':
$logVariationFields .= "Importing Content (" . $fieldNames[$f] . ") :: ";
$nodeArray[$fieldNames[$f]] = [
"value" => $data[$keyIndex[$fieldNames[$f]]],
];
$logVariationFields .= " Success \n";
break;
case 'boolean':
$logVariationFields .= "Importing Boolean (" . $fieldNames[$f] . ") :: ";
$nodeArray[$fieldNames[$f]] = $data[$keyIndex[$fieldNames[$f]]] == 'On' || $data[$keyIndex[$fieldNames[$f]]] == 'Yes' || $data[$keyIndex[$fieldNames[$f]]] == 'on' || $data[$keyIndex[$fieldNames[$f]]] == 'yes' ? 1 : 0;
$logVariationFields .= " Success \n";
break;
case 'langcode':
$logVariationFields .= "Importing Langcode (" . $fieldNames[$f] . ") :: ";
$nodeArray[$fieldNames[$f]] = $data[$keyIndex[$fieldNames[$f]]] != '' ? $data[$keyIndex[$fieldNames[$f]]] : 'en';
$logVariationFields .= " Success \n";
break;
case 'entity_reference_revisions':
/* echo "Target type: ".$fieldSettings[$f]['target_type']."\n";
$paraArray = explode(':', $data[$keyIndex[$fieldNames[$f]]]);
print_r($paraArray);
$node = \Drupal\node\Entity\Node::load(74);
echo "<pre>";
print_r($node);
echo "</pre>";
die('Node'); */
break;
default:
$nodeArray[$fieldNames[$f]] = $data[$keyIndex[$fieldNames[$f]]];
break;
}
}
if (!isset($nodeArray['langcode'])) {
$nodeArray['langcode'] = 'en';
}
$nodeArray['type'] = strtolower($contentType);
$nodeArray['uid'] = 1;
$nodeArray['promote'] = 0;
$nodeArray['sticky'] = 0;
if ($nodeArray['title']['value'] != '') {
$node = Node::create($nodeArray);
$node
->save();
$logVariationFields .= "********************* Node Imported successfully ********************* \n\n";
fwrite($logFile, $logVariationFields);
}
}
fclose($handle);
$url = $base_url . "/admin/content";
header('Location:' . $url);
exit;
}
}
//die('test');
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
13 |
ConfigFormBase:: |
public | function | Constructs a \Drupal\system\ConfigFormBase object. | 11 |
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
ContentImport:: |
public | function |
Content Import Form. Overrides ConfigFormBase:: |
|
ContentImport:: |
public | function | To import data as Content type nodes. | |
ContentImport:: |
public | function | To Create Terms if it is not available. | |
ContentImport:: |
public | function | To Create Terms if it is not available. | |
ContentImport:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
ContentImport:: |
public | function | To get all Content Type Fields. | |
ContentImport:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ContentImport:: |
public | function | To get node available. | |
ContentImport:: |
public | function | To get Termid available. | |
ContentImport:: |
public | function | To get Reference field ids. | |
ContentImport:: |
public static | function | To get user information based on emailIds. | |
ContentImport:: |
public | function |
Content Import Form Submission. Overrides ConfigFormBase:: |
|
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 | 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. | |
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. |