You are here

public function ContentImport::createNode in Content Import 8

Same name and namespace in other branches
  1. 8.3 src/Form/ContentImport.php \Drupal\contentimport\Form\ContentImport::createNode()
  2. 8.4 src/Form/ContentImport.php \Drupal\contentimport\Form\ContentImport::createNode()

To import data as Content type nodes.

1 call to ContentImport::createNode()
ContentImport::submitForm in src/Form/ContentImport.php
Form submission handler.

File

src/Form/ContentImport.php, line 202
Contains \Drupal\contentimport\Form\ContentImport.

Class

ContentImport
Configure Content Import settings for this site.

Namespace

Drupal\contentimport\Form

Code

public function createNode($contentType) {
  global $base_url;
  $loc = db_query('SELECT file_managed.uri FROM file_managed ORDER BY file_managed.fid DESC limit 1', array());
  foreach ($loc as $val) {
    $location = $val->uri;

    // To get location of the csv file imported
  }
  $mimetype = mime_content_type($location);
  $fields = ContentImport::getFields($contentType);
  $fieldNames = $fields['name'];
  $fieldTypes = $fields['type'];
  $fieldSettings = $fields['setting'];
  $files = glob('sites/default/files/' . $contentType . '/images/*.*');
  $images = [];
  foreach ($files as $file_name) {
    file_unmanaged_copy($file_name, 'sites/default/files/' . $contentType . '/images/' . basename($file_name));
    $image = File::create(array(
      'uri' => 'public://' . $contentType . '/images/' . basename($file_name),
    ));
    $image
      ->save();
    $images[basename($file_name)] = $image;
  }
  if ($mimetype == "text/plain") {

    //Code for import csv file
    if (($handle = fopen($location, "r")) !== FALSE) {
      $nodeData = [];
      $keyIndex = [];
      $index = 0;
      while (($data = fgetcsv($handle)) !== FALSE) {
        $index++;
        if ($index < 2) {
          array_push($fieldNames, 'title');
          array_push($fieldTypes, 'text');
          foreach ($fieldNames as $fieldValues) {
            $i = 0;
            foreach ($data as $dataValues) {
              if ($fieldValues == $dataValues) {
                $keyIndex[$fieldValues] = $i;
              }
              $i++;
            }
          }
          continue;
        }
        for ($f = 0; $f < count($fieldNames); $f++) {
          switch ($fieldTypes[$f]) {
            case 'image':
              if (!empty($images[$data[$keyIndex[$fieldNames[$f]]]])) {
                $nodeArray[$fieldNames[$f]] = array(
                  array(
                    'target_id' => $images[$data[$keyIndex[$fieldNames[$f]]]]
                      ->id(),
                  ),
                );
              }
              break;
            case 'entity_reference':
              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;
                }
              }
              else {
                if ($fieldSettings[$f]['target_type'] == 'user') {
                  $userArray = explode(', ', $data[$keyIndex[$fieldNames[$f]]]);
                  $users = ContentImport::getUserInfo($userArray);
                  $nodeArray[$fieldNames[$f]] = $users;
                }
              }
              break;
            case 'text_with_summary':
            case 'text_long':
            case 'text':
              $nodeArray[$fieldNames[$f]] = [
                'value' => $data[$keyIndex[$fieldNames[$f]]],
                'format' => 'full_html',
              ];
              break;
            case 'datetime':
              $dateTime = \DateTime::createFromFormat('Y-m-d h:i:s', $data[$keyIndex[$fieldNames[$f]]]);
              $newDateString = $dateTime
                ->format('Y-m-d\\Th:i:s');
              $nodeArray[$fieldNames[$f]] = [
                "value" => $newDateString,
              ];
              break;
            case 'timestamp':
              $nodeArray[$fieldNames[$f]] = [
                "value" => $data[$keyIndex[$fieldNames[$f]]],
              ];
              break;
            case 'boolean':
              $nodeArray[$fieldNames[$f]] = $data[$keyIndex[$fieldNames[$f]]] == 'On' || $data[$keyIndex[$fieldNames[$f]]] == 'Yes' ? 1 : 0;
              break;
            default:
              $nodeArray[$fieldNames[$f]] = $data[$keyIndex[$fieldNames[$f]]];
              break;
          }
        }
        $nodeArray['type'] = strtolower($contentType);
        $nodeArray['uid'] = 1;
        if ($nodeArray['title']['value'] != '') {
          $node = Node::create($nodeArray);
          $node
            ->save();
        }
      }
      fclose($handle);
      $url = $base_url . "/admin/content";
      header('Location:' . $url);
      exit;
    }
  }
}