function import_node in Content Import 8.9
To import data as Content type nodes.
1 string reference to 'import_node'
- ContentImport::submitForm in src/
Form/ ContentImport.php - Content Import Form Submission.
File
- ./
contentimport.module, line 221 - Module file for Contentimport.
Code
function import_node($filedata, $content_type, $import_type) {
drupal_flush_all_caches();
$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 = get_fields($content_type);
$fieldNames = $fields['name'];
$fieldTypes = $fields['type'];
$fieldSettings = $fields['setting'];
// Code for import csv file.
$mimetype = 1;
if ($mimetype) {
$location = $filedata->destination;
if (($handle = fopen($location, "r")) !== FALSE) {
$keyIndex = [];
$index = 0;
$logVariationFields = "***************************** Content Import Begins ************************************ \n \n ";
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');
array_push($fieldNames, 'author');
array_push($fieldTypes, 'authored_by');
if ($import_type == '2') {
array_push($fieldNames, 'nodeid');
array_push($fieldTypes, 'id');
}
if (array_search('langcode', $data) === FALSE) {
$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 ($import_type == '2') {
if (!isset($keyIndex['title']) || !isset($keyIndex['langcode']) || !isset($keyIndex['nodeid'])) {
error_message(t('nodeid or title or langcode is missing in CSV file. These fields are mandatory to update existing nodes.'));
}
}
else {
if (!isset($keyIndex['title']) || !isset($keyIndex['langcode'])) {
error_message(t('title or langcode is missing in CSV file. These fields are mandatory to create new nodes.'));
}
}
$logVariationFields .= "********************************* Importing node **************************** \n \n";
// Default Language.
$nodeArray['langcode'] = 'en';
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/' . $content_type . '/images/' . $imgIndex);
$fileExists = file_exists('sites/default/files/' . $imgIndex);
if (!$fileExists) {
$images = [];
foreach ($files as $file_name) {
$image = File::create([
'uri' => 'public://' . $content_type . '/images/' . basename($file_name),
]);
$image
->save();
$images[basename($file_name)] = $image;
$imageId = $images[basename($file_name)]
->id();
}
$nodeArray[$fieldNames[$f]] = [
[
'target_id' => $imageId,
'alt' => $images['title'],
'title' => $images['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') {
$target_bundles = $fieldSettings[$f]['handler_settings']['target_bundles'];
// If vocabulary field settings target is single, assume it.
if (count($target_bundles) == 1 && !empty($data[$keyIndex[$fieldNames[$f]]])) {
$terms = get_term_reference($target_bundles[key($target_bundles)], $data[$keyIndex[$fieldNames[$f]]]);
}
else {
$reference = explode(":", $data[$keyIndex[$fieldNames[$f]]]);
if (is_array($reference) && $reference[0] != '') {
$terms = get_term_reference($reference[0], $reference[1]);
}
}
if (!empty($terms)) {
$nodeArray[$fieldNames[$f]] = $terms;
}
}
elseif ($fieldSettings[$f]['target_type'] == 'user') {
$userArray = explode(', ', $data[$keyIndex[$fieldNames[$f]]]);
$users = get_user_info($userArray);
$nodeArray[$fieldNames[$f]] = $users;
}
elseif ($fieldSettings[$f]['target_type'] == 'node') {
$nodeArrays = explode(':', $data[$keyIndex[$fieldNames[$f]]]);
$nodeReference1 = get_node_id($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 'geolocation':
$logVariationFields .= "Importing Geolocation Field (" . $fieldNames[$f] . ") :: ";
$geoArray = explode(";", $data[$keyIndex[$fieldNames[$f]]]);
if (count($geoArray) > 0) {
$geoMultiArray = [];
for ($g = 0; $g < count($geoArray); $g++) {
$latlng = explode(",", $geoArray[$g]);
for ($l = 0; $l < count($latlng); $l++) {
$latlng[$l] = floatval(preg_replace("/\\[^0-9,.]/", "", $latlng[$l]));
}
array_push($geoMultiArray, [
'lat' => $latlng[0],
'lng' => $latlng[1],
]);
}
$nodeArray[$fieldNames[$f]] = $geoMultiArray;
}
else {
$latlng = explode(",", $data[$keyIndex[$fieldNames[$f]]]);
for ($l = 0; $l < count($latlng); $l++) {
$latlng[$l] = floatval(preg_replace("/\\[^0-9,.]/", "", $latlng[$l]));
}
$nodeArray[$fieldNames[$f]] = [
'lat' => $latlng[0],
'lng' => $latlng[1],
];
}
$logVariationFields .= " Success \n";
break;
case 'entity_reference_revisions':
/* In Progress */
break;
case 'list_string':
$logVariationFields .= "Importing Content (" . $fieldNames[$f] . ") :: ";
$listArray = explode(",", $data[$keyIndex[$fieldNames[$f]]]);
array_walk($listArray, 'trim');
$nodeArray[$fieldNames[$f]] = $listArray;
$logVariationFields .= " Success \n";
break;
case 'geofield':
$logVariationFields .= "Importing Geofield Field (" . $fieldNames[$f] . ") :: ";
if (!empty(trim($data[$keyIndex[$fieldNames[$f]]]))) {
$geoFieldArray = explode(";", trim($data[$keyIndex[$fieldNames[$f]]]));
if (count($geoFieldArray) > 0) {
$geoFieldMultiArray = [];
for ($g = 0; $g < count($geoFieldArray); $g++) {
$latlng = explode(",", $geoFieldArray[$g]);
for ($l = 0; $l < count($latlng); $l++) {
$latlng[$l] = floatval($latlng[$l]);
}
array_push($geoFieldMultiArray, \Drupal::service('geofield.wkt_generator')
->WktBuildPoint([
trim($latlng[1]),
trim($latlng[0]),
]));
}
$nodeArray[$fieldNames[$f]] = $geoFieldMultiArray;
}
else {
$latlng = explode(",", trim($data[$keyIndex[$fieldNames[$f]]]));
for ($l = 0; $l < count($latlng); $l++) {
$latlng[$l] = floatval($latlng[$l]);
}
$lonlat = \Drupal::service('geofield.wkt_generator')
->WktBuildPoint([
trim($latlng[1]),
trim($latlng[0]),
]);
$nodeArray[$fieldNames[$f]] = $lonlat;
}
$logVariationFields .= " Success \n";
}
break;
case 'authored_by':
$logVariationFields .= "Importing Content (" . $fieldNames[$f] . ") :: ";
$user_id = get_user_id($data[$keyIndex[$fieldNames[$f]]]);
$nodeArray['uid'] = $user_id > 0 ? $user_id : \Drupal::currentUser()
->id();
$logVariationFields .= " Success \n";
break;
case 'id':
$logVariationFields .= "Importing Content (" . $fieldNames[$f] . ") :: ";
$nodeid = node_exists($data[$keyIndex[$fieldNames[$f]]]);
if ($nodeid > 0) {
$nodeArray['nid'] = $nodeid;
}
else {
error_message(t('Nodeid') . ' ' . $data[$keyIndex[$fieldNames[$f]]] . ' ' . t('does not exists in the system.'));
}
$logVariationFields .= " Success \n";
break;
default:
$nodeArray[$fieldNames[$f]] = $data[$keyIndex[$fieldNames[$f]]];
break;
}
}
$nodeArray['type'] = strtolower($content_type);
$nodeArray['promote'] = 0;
$nodeArray['sticky'] = 0;
// Updating existing node based on nodeid.
if ($import_type == '2') {
$node = Node::load($data[$keyIndex['nodeid']]);
foreach ($nodeArray as $key => $value) {
$node
->set($key, $value);
}
$node
->save();
}
elseif ($nodeArray['title']['value'] != '') {
// Creating new node.
$node = Node::create($nodeArray);
$node
->save();
}
$logVariationFields .= "********************* Node Imported successfully ********************* \n\n";
fwrite($logFile, $logVariationFields);
$nodeArray = [];
}
fclose($handle);
}
}
//die('test');
}