You are here

protected function FeedImportProcessor::createEntity in Feed Import 8

Creates a new entity.

Parameters

mixed $item: Item to be mapped to entity

Return value

object An array containing the entity or NULL

1 call to FeedImportProcessor::createEntity()
FeedImportProcessor::process in feed_import_base/src/FeedImportProcessor.php
Processes the import.

File

feed_import_base/src/FeedImportProcessor.php, line 688

Class

FeedImportProcessor
Class that processess the import.

Namespace

Drupal\feed_import_base

Code

protected function &createEntity(&$item) {

  // Create the entity.
  $entity = $this->baseEntity;

  // Set a ref for current entity.
  $this->current =& $entity;

  // Check for unique id of item.
  if ($this->uniq === NULL) {

    // No hash, so not monitored.
    $entity[static::TEMP_HASH] = NULL;
  }
  else {

    // Get uniq.
    $uniq = $this->reader
      ->map($item, $this->uniq);
    if (is_array($uniq)) {
      $uniq = isset($uniq[0]) ? $uniq[0] : reset($uniq);
    }

    // Check if uniq alter callback exists.
    if ($this->uniqAlter) {
      $uniq = call_user_func($this->uniqAlter, $uniq);
    }

    // Create hash.
    $entity[static::TEMP_HASH] = $this->hashes
      ->hash($uniq);
  }

  // Set entity fields.
  foreach ($this->fields as $name => &$field) {

    // Get filtered field value.
    $val = NULL;
    $i = -1;
    while (++$i < $field['paths_count']) {

      // Get value for specified path.
      $val = $this->reader
        ->map($item, $field['paths'][$i]);

      // Check if passes prefilter.
      if ($field['prefilters']) {
        $this->prefilteredValue = $this->filters
          ->apply("prefilters", $name, $val);
        if (!$this
          ->hasContent($this->prefilteredValue)) {
          $val = $this->prefilteredValue = NULL;

          // If item doesn't pass prefilter than go to next path.
          continue;
        }
      }
      if ($this
        ->hasContent($val)) {

        // We have content, check for filters.
        if ($field['filters']) {
          $val = $this->filters
            ->apply("filters", $name, $val);
          if (!$this
            ->hasContent($val)) {
            $val = NULL;
          }
        }

        // We matched one path.
        break;
      }
      else {
        $val = NULL;
      }
    }

    // Check if default action is needed.
    if ($val === NULL) {
      switch ($field['default_action']) {

        // Provide default value.
        // This is also default action.
        case static::ACTION_DEFAULT_VALUE:
        default:
          $val = $field['default_value'];
          break;

        // Provide filtered default value.
        case static::ACTION_DEFAULT_FILTERED_VALUE:
          $val = $this->filters
            ->apply("filters", $name, $field['default_value']);
          break;

        // Skip this item by returning NULL.
        case static::ACTION_SKIP_ITEM:
          $this->report['skipped']++;
          $this->current = $this->prefilteredValue = NULL;
          return $this->NULL;

        // Don't add this field to entity.
        case static::ACTION_IGNORE_FIELD:
          $this->prefilteredValue = NULL;
          continue 2;
      }
    }

    // Remove prefiltered value.
    $this->prefilteredValue = NULL;

    // Set field value in entity.
    $this
      ->attachField($entity, $field, $val);

    // Not needed anymore.
    unset($val);
  }

  // Remove current reference.
  unset($this->current);
  $this->current = NULL;

  // Return the entity array.
  return $entity;
}