You are here

contentimport.module in Content Import 8.9

Module file for Contentimport.

File

contentimport.module
View source
<?php

/**
 * @file
 * Module file for Contentimport.
 */
use Drupal\node\Entity\Node;
use Drupal\file\Entity\File;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\Entity\Term;
use Drupal\user\Entity\User;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Url;

/**
 * Implements hook_preprocess_page().
 */
function contentimport_preprocess_page(&$variables) {
  $variables['page']['#cache']['contexts'][] = 'route';
  $urlRoute = \Drupal::routeMatch()
    ->getRouteName();
  if ($urlRoute == 'contentimport.admin_settings') {
    $variables['#attached']['library'][] = 'contentimport/common-styling';
  }
}

/**
 * To get all Content Type Fields.
 */
function get_fields($content_type) {
  $fields = [];
  foreach (\Drupal::service('entity_field.manager')
    ->getFieldDefinitions('node', $content_type) 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.
 */
function get_term_reference($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])) {
    create_voc($vid, $voc);
  }
  $termArray = array_map('trim', explode(',', $terms));
  $termIds = [];
  foreach ($termArray as $term) {
    $term_id = get_term_id($term, $vid);
    if (empty($term_id)) {
      $term_id = create_term($voc, $term, $vid);
    }
    $termIds[]['target_id'] = $term_id;
  }
  return $termIds;
}

/**
 * To Create Terms if it is not available.
 */
function create_voc($vid, $voc) {
  $vocabulary = Vocabulary::create([
    'vid' => $vid,
    'machine_name' => $vid,
    'name' => $voc,
  ]);
  $vocabulary
    ->save();
}

/**
 * To Create Terms if it is not available.
 */
function create_term($voc, $term, $vid) {
  Term::create([
    'parent' => [
      $voc,
    ],
    'name' => $term,
    'vid' => $vid,
  ])
    ->save();
  $termId = get_term_id($term, $vid);
  return $termId;
}

/**
 * To get Termid available.
 */
function get_term_id($term, $vid) {
  $query = \Drupal::database()
    ->select('taxonomy_term_field_data', 't');
  $query
    ->fields('t', [
    'tid',
  ]);
  $query
    ->condition('t.vid', $vid);
  $query
    ->condition('t.name', $term);
  $termRes = $query
    ->execute()
    ->fetchAll();
  foreach ($termRes as $val) {
    $term_id = $val->tid;
  }
  return $term_id;
}

/**
 * To get node available.
 */
function get_node_id($title) {
  $nodeReference = [];
  $db = \Drupal::database();
  foreach ($title as $key => $value) {
    $query = $db
      ->select('node_field_data', 'n');
    $query
      ->fields('n', [
      'nid',
    ]);
    $nodeId = $query
      ->condition('n.title', trim($value))
      ->execute()
      ->fetchField();
    $nodeReference[$key]['target_id'] = $nodeId;
  }
  return $nodeReference;
}

/**
 * To get user id.
 */
function get_user_id($name) {
  $user_id = \Drupal::database()
    ->select('users_field_data', 'u')
    ->fields('u', [
    'uid',
  ])
    ->condition('u.name', trim($name))
    ->execute()
    ->fetchField();
  return $user_id;
}

/**
 * To get user information based on emailIds.
 */
function get_user_info($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 check node exists or not.
 */
function node_exists($nid) {
  $node_count = \Drupal::database()
    ->select('node_field_data', 'n')
    ->fields('n', [
    'nid',
  ])
    ->condition('n.nid', $nid)
    ->execute()
    ->fetchField();
  return $node_count;
}

/**
 * To throw error messages.
 */
function error_message($message) {
  \Drupal::messenger()
    ->addError($message);
  $url = Url::fromUri('internal:/admin/config/content/contentimport');
  $destination = $url
    ->toString();
  $response = new RedirectResponse($destination, 301);
  return $response
    ->send();
}

/**
 * To callback after import completion.
 */
function import_success() {
  $url = Url::fromUri('internal:/admin/content');
  $destination = $url
    ->toString();
  $response = new RedirectResponse($destination, 301);
  $response
    ->send();
}

/**
 * To import data as Content type nodes.
 */
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');
}

Functions

Namesort descending Description
contentimport_preprocess_page Implements hook_preprocess_page().
create_term To Create Terms if it is not available.
create_voc To Create Terms if it is not available.
error_message To throw error messages.
get_fields To get all Content Type Fields.
get_node_id To get node available.
get_term_id To get Termid available.
get_term_reference To get Reference field ids.
get_user_id To get user id.
get_user_info To get user information based on emailIds.
import_node To import data as Content type nodes.
import_success To callback after import completion.
node_exists To check node exists or not.