Score:-1

How do I combine terms in Drupal feeds?

km flag

For example if I had 2 rows with people in them

uid | favourite colour
----------------------
 3  | Blue
 3  | Yellow

And I wanted the result to have both Blue and Yellow selected at the end. Is there a combine tamper plugin? I am already doing

class MyAlterSubscriber extends AfterParseBase

Do I need to lookup existing values here and combine them first?

Score:0
km flag

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)));
   }

}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.