public function EntityProcessorBase::process in Feeds 8.3
Processes the results from a parser.
Parameters
\Drupal\feeds\FeedInterface $feed: The feed being imported.
\Drupal\feeds\Feeds\Item\ItemInterface $item: The item to process.
\Drupal\feeds\StateInterface $state: The state object.
Overrides ProcessorInterface::process
File
- src/
Feeds/ Processor/ EntityProcessorBase.php, line 128
Class
- EntityProcessorBase
- Defines a base entity processor.
Namespace
Drupal\feeds\Feeds\ProcessorCode
public function process(FeedInterface $feed, ItemInterface $item, StateInterface $state) {
// Initialize clean list if needed.
$clean_state = $feed
->getState(StateInterface::CLEAN);
if (!$clean_state
->initiated()) {
$this
->initCleanList($feed, $clean_state);
}
$skip_new = $this->configuration['insert_new'] == static::SKIP_NEW;
$existing_entity_id = $this
->existingEntityId($feed, $item);
$skip_existing = $this->configuration['update_existing'] == static::SKIP_EXISTING;
// If the entity is an existing entity it must be removed from the clean
// list.
if ($existing_entity_id) {
$clean_state
->removeItem($existing_entity_id);
}
// Bulk load existing entities to save on db queries.
if ($skip_existing && $existing_entity_id || !$existing_entity_id && $skip_new) {
$state->skipped++;
return;
}
// Delay building a new entity until necessary.
if ($existing_entity_id) {
$entity = $this->storageController
->load($existing_entity_id);
}
$hash = $this
->hash($item);
$changed = $existing_entity_id && $hash !== $entity
->get('feeds_item')->hash;
// Do not proceed if the item exists, has not changed, and we're not
// forcing the update.
if ($existing_entity_id && !$changed && !$this->configuration['skip_hash_check']) {
$state->skipped++;
return;
}
// Build a new entity.
if (!$existing_entity_id && !$skip_new) {
$entity = $this
->newEntity($feed);
}
try {
// Set feeds_item values.
$feeds_item = $entity
->get('feeds_item');
$feeds_item->target_id = $feed
->id();
$feeds_item->hash = $hash;
// Set field values.
$this
->map($feed, $entity, $item);
// Validate the entity.
$feed
->dispatchEntityEvent(FeedsEvents::PROCESS_ENTITY_PREVALIDATE, $entity, $item);
$this
->entityValidate($entity);
// Dispatch presave event.
$feed
->dispatchEntityEvent(FeedsEvents::PROCESS_ENTITY_PRESAVE, $entity, $item);
// This will throw an exception on failure.
$this
->entitySaveAccess($entity);
// Set imported time.
$entity
->get('feeds_item')->imported = \Drupal::service('datetime.time')
->getRequestTime();
// And... Save! We made it.
$this->storageController
->save($entity);
// Dispatch postsave event.
$feed
->dispatchEntityEvent(FeedsEvents::PROCESS_ENTITY_POSTSAVE, $entity, $item);
// Track progress.
$existing_entity_id ? $state->updated++ : $state->created++;
} catch (EmptyFeedException $e) {
// Not an error.
$state->skipped++;
} catch (ValidationException $e) {
$state->failed++;
$state
->setMessage($e
->getFormattedMessage(), 'warning');
} catch (\Exception $e) {
$state->failed++;
$state
->setMessage($e
->getMessage(), 'warning');
}
}