You are here

public function FeedsProcessor::process in Feeds 8.2

Process the result of the parsing stage.

Parameters

FeedsSource $source: Source information about this import.

FeedsParserResult $parser_result: The result of the parsing stage.

File

lib/Drupal/feeds/Plugin/FeedsProcessor.php, line 179
Contains FeedsProcessor and related classes.

Class

FeedsProcessor
Abstract class, defines interface for processors.

Namespace

Drupal\feeds\Plugin

Code

public function process(FeedsSource $source, FeedsParserResult $parser_result) {
  $state = $source
    ->state(FEEDS_PROCESS);
  while ($item = $parser_result
    ->shiftItem()) {

    // Check if this item already exists.
    $entity_id = $this
      ->existingEntityId($source, $parser_result);
    $skip_existing = $this->config['update_existing'] == FEEDS_SKIP_EXISTING;
    module_invoke_all('feeds_before_update', $source, $item, $entity_id);

    // If it exists, and we are not updating, pass onto the next item.
    if ($entity_id && $skip_existing) {
      continue;
    }
    $hash = $this
      ->hash($item);
    $changed = $hash !== $this
      ->getHash($entity_id);
    $force_update = $this->config['skip_hash_check'];

    // Do not proceed if the item exists, has not changed, and we're not
    // forcing the update.
    if ($entity_id && !$changed && !$force_update) {
      continue;
    }
    try {

      // Load an existing entity.
      if ($entity_id) {
        $entity = $this
          ->entityLoad($source, $entity_id);

        // The feeds_item table is always updated with the info for the most
        // recently processed entity. The only carryover is the entity_id.
        $this
          ->newItemInfo($entity, $source->feed_nid, $hash);
        $entity->feeds_item->entity_id = $entity_id;
        $entity->feeds_item->is_new = FALSE;
      }
      else {
        $entity = $this
          ->newEntity($source);
        $this
          ->newItemInfo($entity, $source->feed_nid, $hash);
      }

      // Set property and field values.
      $this
        ->map($source, $parser_result, $entity);
      $this
        ->entityValidate($entity);

      // Allow modules to alter the entity before saving.
      module_invoke_all('feeds_presave', $source, $entity, $item, $entity_id);
      if (module_exists('rules')) {
        rules_invoke_event('feeds_import_' . $source
          ->importer()->id, $entity);
      }

      // Enable modules to skip saving at all.
      if (!empty($entity->feeds_item->skip)) {
        continue;
      }

      // This will throw an exception on failure.
      $this
        ->entitySaveAccess($entity);
      $this
        ->entitySave($entity);

      // Allow modules to perform operations using the saved entity data.
      // $entity contains the updated entity after saving.
      module_invoke_all('feeds_after_save', $source, $entity, $item, $entity_id);

      // Track progress.
      if (empty($entity_id)) {
        $state->created++;
      }
      else {
        $state->updated++;
      }
    } catch (Exception $e) {
      $state->failed++;
      drupal_set_message($e
        ->getMessage(), 'warning');
      $message = $this
        ->createLogMessage($e, $entity, $item);
      $source
        ->log('import', $message, array(), WATCHDOG_ERROR);
    }
  }

  // Set messages if we're done.
  if ($source
    ->progressImporting() != FEEDS_BATCH_COMPLETE) {
    return;
  }
  $info = $this
    ->entityInfo();
  $tokens = array(
    '@entity' => strtolower($info['label']),
    '@entities' => strtolower($info['label plural']),
  );
  $messages = array();
  if ($state->created) {
    $messages[] = array(
      'message' => format_plural($state->created, 'Created @number @entity.', 'Created @number @entities.', array(
        '@number' => $state->created,
      ) + $tokens),
    );
  }
  if ($state->updated) {
    $messages[] = array(
      'message' => format_plural($state->updated, 'Updated @number @entity.', 'Updated @number @entities.', array(
        '@number' => $state->updated,
      ) + $tokens),
    );
  }
  if ($state->failed) {
    $messages[] = array(
      'message' => format_plural($state->failed, 'Failed importing @number @entity.', 'Failed importing @number @entities.', array(
        '@number' => $state->failed,
      ) + $tokens),
      'level' => WATCHDOG_ERROR,
    );
  }
  if (empty($messages)) {
    $messages[] = array(
      'message' => t('There are no new @entities.', array(
        '@entities' => strtolower($info['label plural']),
      )),
    );
  }
  foreach ($messages as $message) {
    drupal_set_message($message['message']);
    $source
      ->log('import', $message['message'], array(), isset($message['level']) ? $message['level'] : WATCHDOG_INFO);
  }
}