You are here

public function GetServices::buildNodeData in Simple Node Importer 8

1 call to GetServices::buildNodeData()
GetServices::checkFieldWidget in src/Services/GetServices.php
Checks the widget type of each field.

File

src/Services/GetServices.php, line 722

Class

GetServices

Namespace

Drupal\simple_node_importer\Services

Code

public function buildNodeData($data, $fieldType, $fieldSetting = NULL) {
  $i = 0;
  $fieldTypes = [
    'number_integer',
    'number_float',
  ];
  $textFieldTypes = [
    'string_long',
    'string',
  ];

  // $dateformat = ($fieldSetting == 'datetime') ? 'Y-m-d\TH:i:s' : 'Y-m-d';.
  $dataRow = [];
  if (is_array($data) && !empty($data)) {
    foreach ($data as $value) {
      if (in_array($fieldType, [
        'image',
        'file',
      ]) && !empty($value) && filter_var($value, FILTER_VALIDATE_URL)) {

        // Code for image/file field..
        $file = system_retrieve_file($value, NULL, TRUE, FILE_EXISTS_REPLACE);
        $dataRow[$i]['target_id'] = !empty($file) ? $file
          ->id() : NULL;
      }
      elseif ($fieldType == 'datetime') {
        if (!empty($value)) {
          if ($this
            ->validateDateExpression($value)) {
            $dataRow[$i]['value'] = $fieldSetting == 'datetime' ? date_format(date_create($value), 'Y-m-d\\TH:i:s') : date_format(date_create($value), 'Y-m-d');
          }
          else {
            return FALSE;
          }
        }
      }
      elseif (in_array($fieldType, $fieldTypes)) {
        $dataRow[$i]['value'] = $value;
      }
      elseif ($fieldType == 'link') {
        $dataRow[$i]['uri'] = $value;
      }
      elseif (in_array($fieldType, $textFieldTypes)) {
        $dataRow[$i]['value'] = utf8_encode(trim($value));
      }
      else {

        // code...
        $dataRow[$i] = trim($value);
      }
      $i++;
    }
  }
  elseif (!empty($data)) {
    if (in_array($fieldType, [
      'image',
      'file',
    ]) && !empty($data) && filter_var($data, FILTER_VALIDATE_URL)) {

      // Code for image/file field..
      $file = system_retrieve_file($data, NULL, TRUE, FILE_EXISTS_REPLACE);
      $dataRow[0]['target_id'] = !empty($file) ? $file
        ->id() : NULL;
    }
    elseif ($fieldType == 'datetime') {
      if ($this
        ->validateDateExpression($data)) {
        $dataRow['value'] = $fieldSetting == 'datetime' ? date_format(date_create($data), 'Y-m-d\\TH:i:s') : date_format(date_create($data), 'Y-m-d');
      }
      else {
        return FALSE;
      }
    }
    elseif (in_array($fieldType, $fieldTypes)) {
      $dataRow[0]['value'] = $data;
    }
    elseif ($fieldType == 'link') {
      $dataRow['uri'] = $data;
    }
    elseif (in_array($fieldType, $textFieldTypes)) {
      $dataRow[0]['value'] = utf8_encode(trim($data));
    }
    else {

      // code...
      $dataRow = trim($data);
    }
  }
  return $dataRow;
}