You are here

protected function FeedsSubscriber::alterItem in Feeds Tamper 8.2

Alters a single item.

Parameters

\Drupal\feeds\Feeds\Item\ItemInterface $item: The item to make modifications on.

\Drupal\feeds\Event\ParseEvent $event: The parse event.

\Drupal\tamper\TamperInterface[][] $tampers_by_source: A list of tampers to apply, grouped by source.

1 call to FeedsSubscriber::alterItem()
FeedsSubscriber::afterParse in src/EventSubscriber/FeedsSubscriber.php
Acts on parser result.

File

src/EventSubscriber/FeedsSubscriber.php, line 97

Class

FeedsSubscriber
Subscriber to Feeds events.

Namespace

Drupal\feeds_tamper\EventSubscriber

Code

protected function alterItem(ItemInterface $item, ParseEvent $event, array $tampers_by_source) {
  $tamperable_item = new TamperableFeedItemAdapter($item);
  foreach ($tampers_by_source as $source => $tampers) {
    try {

      // Get the value for a source.
      $item_value = $item
        ->get($source);
      $multiple = is_array($item_value) && !empty($item_value);

      /** @var \Drupal\tamper\TamperInterface $tamper */
      foreach ($tampers as $tamper) {
        $definition = $tamper
          ->getPluginDefinition();

        // Many plugins expect a scalar value but the current value of the
        // pipeline might be multiple scalars and in this case the current
        // value needs to be iterated and each scalar separately transformed.
        if ($multiple && !$definition['handle_multiples']) {
          $new_value = [];

          // @todo throw exception if $item_value is not an array.
          foreach ($item_value as $scalar_value) {
            $new_value[] = $tamper
              ->tamper($scalar_value, $tamperable_item);
          }
          $item_value = $new_value;
        }
        else {
          $item_value = $tamper
            ->tamper($item_value, $tamperable_item);
          $multiple = $tamper
            ->multiple();
        }
      }

      // Write the changed value.
      $item
        ->set($source, $item_value);
    } catch (SkipTamperDataException $e) {

      // @todo We would rather unset the source, but that isn't possible yet
      // with ItemInterface.
      $item
        ->set($source, NULL);
    }
  }
}