You are here

public function ImporterBase::data in CSV Importer 8

Prepare data for import.

Return value

array Prepared data.

Overrides ImporterInterface::data

1 call to ImporterBase::data()
ImporterBase::process in src/Plugin/ImporterBase.php
Run batch operations.

File

src/Plugin/ImporterBase.php, line 75

Class

ImporterBase
Provides a base class for ImporterBase plugins.

Namespace

Drupal\csv_importer\Plugin

Code

public function data() {
  $csv = $this->configuration['csv'];
  $return = [];
  if ($csv && is_array($csv)) {
    $csv_fields = $csv[0];
    unset($csv[0]);
    foreach ($csv as $index => $data) {
      foreach ($data as $key => $content) {
        if (isset($csv_fields[$key])) {
          $content = Unicode::convertToUtf8($content, mb_detect_encoding($content));
          $fields = explode('|', $csv_fields[$key]);
          $field = $fields[0];
          if (count($fields) > 1) {
            foreach ($fields as $key => $in) {
              $return['content'][$index][$field][$in] = $content;
            }
          }
          elseif (isset($return['content'][$index][$field])) {
            $prev = $return['content'][$index][$field];
            $return['content'][$index][$field] = [];
            if (is_array($prev)) {
              $prev[] = $content;
              $return['content'][$index][$field] = $prev;
            }
            else {
              $return['content'][$index][$field][] = $prev;
              $return['content'][$index][$field][] = $content;
            }
          }
          else {
            $return['content'][$index][current($fields)] = $content;
          }
        }
      }
      if (isset($return[$index])) {
        $return['content'][$index] = array_intersect_key($return[$index], array_flip($this->configuration['fields']));
      }
    }
  }
  return $return;
}