AfterParseBase does not give access to the full set of data.
Therefore, use the FeedsEvents::AFTER event instead.
I found that if the last item had no taxonomy items selected then all items would be lost. To create a union of taxonomy items:
<?php
namespace Drupal\cbi_feed_alter\EventSubscriber;
use Drupal\feeds\Event\FeedsEvents;
use Drupal\feeds\Event\ParseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class IssuerFeedEventSubscriber implements EventSubscriberInterface {
const JOIN_CHAR = ',';
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[FeedsEvents::PARSE][] = ['afterParse', FeedsEvents::AFTER];
return $events;
}
public function afterParse(ParseEvent $event) {
/** @var \Drupal\feeds\FeedInterface */
$feed = $event->getFeed();
/** @var \Drupal\feeds\Result\ParserResultInterface */
$parser_result = $event->getParserResult();
$feed_type = $feed->getType();
if ($feed_type->id() != 'issuer') {
return;
}
for ($parser_result->rewind(); $parser_result->valid(); $parser_result->next()) {
$this->findStates($parser_result->current(), $parser_result);
}
}
protected function findStates($item, $parser_result) {
$issuer_id = $item->get('issuerid');
$states = [];
$parser_current_key = $parser_result->key();
for ($parser_result->rewind(); $parser_result->valid(); $parser_result->next()) {
if ($parser_result->current()->get('issuerid') == $issuer_id) {
$states = array_filter(array_merge($states, explode(self::JOIN_CHAR, $parser_result->current()->get('state'))));
}
}
// Iterate back to the key
$parser_result->rewind();
while ($parser_result->valid() && $parser_result->key() != $parser_current_key) {
$parser_result->next();
}
$parser_result->offsetSet($parser_current_key);
$item->set('state', implode(self::JOIN_CHAR, array_unique($states, SORT_STRING)));
}
}