You are here

public function EntityReference::setTarget in Feeds 8.3

Sets the values on an object.

Parameters

\Drupal\feeds\FeedInterface $feed: The feed object.

\Drupal\Core\Entity\EntityInterface $entity: The target object.

string $target: The name of the target to set.

array $values: A list of values to set on the target.

Overrides FieldTargetBase::setTarget

File

src/Feeds/Target/EntityReference.php, line 112

Class

EntityReference
Defines an entity reference mapper.

Namespace

Drupal\feeds\Feeds\Target

Code

public function setTarget(FeedInterface $feed, EntityInterface $entity, $field_name, array $raw_values) {
  $values = [];
  foreach ($raw_values as $delta => $columns) {
    try {
      $this
        ->prepareValue($delta, $columns);
      $values[] = $columns;
    } catch (ReferenceNotFoundException $e) {

      // The referenced entity is not found. We need to enforce Feeds to try
      // to import the same item again on the next import.
      // Feeds stores a hash of every imported item in order to make the
      // import process more efficient by ignoring items it has already seen.
      // In this case we need to destroy the hash in order to be able to
      // import the reference on a next import.
      $entity
        ->get('feeds_item')->hash = NULL;
      $feed
        ->getState(StateInterface::PROCESS)
        ->setMessage($e
        ->getFormattedMessage(), 'warning', TRUE);
    } catch (EmptyFeedException $e) {

      // Nothing wrong here.
    } catch (TargetValidationException $e) {

      // Validation failed.
      $this
        ->addMessage($e
        ->getFormattedMessage(), 'error');
    }
  }
  if (!empty($values)) {
    $entity_target = $this
      ->getEntityTarget($feed, $entity);
    if ($entity_target) {
      $item_list = $entity_target
        ->get($field_name);

      // Append these values to the existing values.
      $values = array_merge($item_list
        ->getValue(), $values);
      $item_list
        ->setValue($values);
    }
  }
}