1

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

}

2 Answers 2

3

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.