protected function EntityProcessorBase::map in Feeds 8.3
Execute mapping on an item.
This method encapsulates the central mapping functionality. When an item is processed, it is passed through map() where the properties of $source_item are mapped onto $target_item following the processor's mapping configuration.
1 call to EntityProcessorBase::map()
- EntityProcessorBase::process in src/
Feeds/ Processor/ EntityProcessorBase.php - Processes the results from a parser.
File
- src/
Feeds/ Processor/ EntityProcessorBase.php, line 1028
Class
- EntityProcessorBase
- Defines a base entity processor.
Namespace
Drupal\feeds\Feeds\ProcessorCode
protected function map(FeedInterface $feed, EntityInterface $entity, ItemInterface $item) {
$mappings = $this->feedType
->getMappings();
// Mappers add to existing fields rather than replacing them. Hence we need
// to clear target elements of each item before mapping in case we are
// mapping on a prepopulated item such as an existing node.
foreach ($mappings as $delta => $mapping) {
if ($mapping['target'] == 'feeds_item') {
// Skip feeds item as this field gets default values before mapping.
continue;
}
// Clear the target.
$this
->clearTarget($entity, $this->feedType
->getTargetPlugin($delta), $mapping['target']);
}
// Gather all of the values for this item.
$source_values = [];
foreach ($mappings as $delta => $mapping) {
$target = $mapping['target'];
foreach ($mapping['map'] as $column => $source) {
if ($source === '') {
// Skip empty sources.
continue;
}
if (!isset($source_values[$delta][$column])) {
$source_values[$delta][$column] = [];
}
$value = $item
->get($source);
if (!is_array($value)) {
$source_values[$delta][$column][] = $value;
}
else {
$source_values[$delta][$column] = array_merge($source_values[$delta][$column], $value);
}
}
}
// Rearrange values into Drupal's field structure.
$field_values = [];
foreach ($source_values as $field => $field_value) {
$field_values[$field] = [];
foreach ($field_value as $column => $values) {
// Use array_values() here to keep our $delta clean.
foreach (array_values($values) as $delta => $value) {
$field_values[$field][$delta][$column] = $value;
}
}
}
// Set target values.
foreach ($mappings as $delta => $mapping) {
$plugin = $this->feedType
->getTargetPlugin($delta);
// Skip immutable targets for which the entity already has a value.
if (!$plugin
->isMutable() && !$plugin
->isEmpty($feed, $entity, $mapping['target'])) {
continue;
}
if (isset($field_values[$delta])) {
$plugin
->setTarget($feed, $entity, $mapping['target'], $field_values[$delta]);
}
}
return $entity;
}