Score:-1

How to add a new field in a feed AfterParseBase?

km flag

Parsing a CSV file, and the goal is to take an input column and turn it into two Drupal fields. The last 3 characters of the BondId field becomes a new field called TrancheId.

The BondID field looks like

enter image description here

The CSV source field that is not in the input data is mapped

enter image description here

Code to add a field

class CbiFeedAlterSubscriber extends AfterParseBase {

 /**
  * {@inheritdoc}
  */
 public function applies(ParseEvent $event) {
    return $event->getFeed()->getType()->id() === 'bond';
 }

 /**
  * {@inheritdoc}
  */
 protected function alterItem(ItemInterface $item, ParseEvent $event) {
   $old_tranche_id = $item->get('TrancheId');
   if ($old_tranche_id) {
     return;
   }

   // Tranche ID is a new data item derived from BondID
   $bond_id = $item->get('bondid');
   $tranche_id = substr($bond_id, strlen($bond_id) - 3, 3);

   $item->set('TrancheId', $tranche_id);
 }
}

The service is defined

services:
  cbi_feed_alter.feed_alter_event_subscriber:
    class: Drupal\cbi_feed_alter\EventSubscriber\CbiFeedAlterSubscriber
    tags:
      - { name: event_subscriber }
Score:-1
km flag

Lower case field names and it works

$item->set('trancheid', $tranche_id);

Similarly for BondID

$item->set('bondid', $tranche_id);

sets the value, mixed case doesn't work.

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.