Score:1

How do I use Feeds Tamper to process multiple CSV columns into a single field?

cn flag

I have a CSV file that I import into my site using Feeds Tamper.

The CSV file has columns like

  • URL path
  • Favorite vegetable
  • Favorite fruit
  • Favorite pasta

In Drupal, my content type has a JSON Field, and I want to import "Favorite vegetable", "Favorite fruit", and "Favorite pasta" into the JSON field.

Is there a way to map all three favorites columns in the CSV file to the Drupal JSON field and combine their values in a Tamper plugin? I can't find any way to pull in data from multiple sources (the three CSV columns) in a Tamper plugin.

Here's the structure of a Tamper plugin.

<?php

namespace Drupal\tamper\Plugin\Tamper;

use Drupal\tamper\Annotation\Tamper;
use Drupal\tamper\TamperableItemInterface;
use Drupal\tamper\TamperBase;

/**
 * Plugin implementation for CSV import. Copied from the encode plugin.
 *
 * @Tamper(
 *   id = "json_import",
 *   label = @Translation("JSON Import"),
 *   description = @Translation("Custom import for JSON."),
 *   category = "Text",
 *   handle_multiples = TRUE
 * )
 */
class JsonImport extends TamperBase {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    $config = parent::defaultConfiguration();
    return $config;
  }

  /**
   * {@inheritdoc}
   */
  public function tamper($data, TamperableItemInterface $item = NULL) {
    return $data;
  }

}
Score:3
cn flag

Ok, here's the correct way to do this.

getSource() will return an array with keys for all the CSV columns.

The column names are processed as follows:

  • single words: Capitalization is kept. (yellow, wEiRd

  • multiple words: Spaces are replaced with _, and all lowercase. (Meta Tag becomes meta_tag)

    /**
     * {@inheritdoc}
     */
    public function tamper($data, TamperableItemInterface $item = NULL) {
      $csv_columns = $item->getSource();
    
      $json = [];
      $json['favoriteFruit'] = $csv_columns['favorite_fruit'];
      $json['favoriteVegetable'] = $csv_columns['favorite_vegetable'];
      $json['favoritePasta'] = $csv_columns['favorite_pasta'];
      $encoded_json = json_encode($json);
      return $encoded_json;
    }
    
Score:-1
cn flag

I haven't figured out how to access multiple CSV columns, but one workaround is to use Excel or other spreadsheet software to create a new column and generate the JSON values in Excel. This is not a good approach because you have to do all the escaping in Excel, which is not fun and error prone.

For example, in Excel, you can use a formula like this:

=CONCAT("{"+CHAR(34)+"favoriteFruit"+CHAR(34)+":"+CHAR(34)+J3+CHAR(34)+","+CHAR(34)+"favoriteVegetable"+CHAR(34)+":"++CHAR(34)+K3+CHAR(34)+","+CHAR(34)+"favoritePasta"+CHAR(34)+":"+CHAR(34)+C3+CHAR(34)+"}")

Then, in Feeds Tamper, you need to add the Encode plugin and "Decode JSON" followed by the Encode plugin again with "Encode JSON". If your JSON doesn't have any mistakes, it will be imported into Drupal.

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.