0

Working in Drupal8.

I have configured Feeds module in my build. I have a content type with "Country" field ["Country" contributed module https://www.drupal.org/project/country]. Have mapped the "Country" field with the source and tried importing, but the values from csv file is not storing to the DB.

Below is my Country.php file which I placed inside feeds module:

<?php

namespace Drupal\feeds\Feeds\Target;

use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\feeds\FieldTargetDefinition;
use Drupal\feeds\Plugin\Type\Target\FieldTargetBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\feeds\FeedInterface;

/**
 * Defines a Country field mapper.
 *
 * @FeedsTarget(
 *   id = "country",
 *   field_types = {
 *     "country"
 *   }
 * )
 */

class Country extends StringTarget {
   /*
    * {@inheritdoc}
   */
   protected static function prepareTarget(FieldDefinitionInterface $field_definition) {
      //$type = $field_definition->getSetting('field_sac_admin_data');
      return FieldTargetDefinition::createFromFieldDefinition($field_definition)
         ->addProperty('country')
         ->markPropertyUnique('country');

   }

  /**
   * {@inheritdoc}
   */
  public function setTarget(FeedInterface $feed, EntityInterface $entity, $field_name, array $values) {
    /*if ($values = $this->prepareValues($values)) { //print_r($values);die;
      $entity_target = $this->getEntityTarget($feed, $entity);
      $item_list = $entity_target->get($field_name);

      // Append these values to the existing values.
      $values = array_merge($item_list->getValue()[0], $values[0]);
      //$values = array_merge(array("India"), array("IN"));

      $item_list->setValue($values);
    }*/
    
    if (!empty($values)) {
      $entity_target = $this->getEntityTarget($feed, $entity);
      if ($entity_target) {
        $item_list = $entity_target->get($field_name);

        // Append these values to the existing values.
        $values = array_merge($item_list->getValue(), $values);
        $item_list->setValue($values);
      }
    }
  }
  /**
   * {@inheritdoc}
  */
   protected function prepareValue($delta, array &$values) {
     if (isset($values['country']) && empty($values['country'])) {
       // If country is empty set it to null
       $values['country'] = NULL;
     }
     //$values['country'] = "IN";
   }
}

Have tried hard-coding $values['country'], but no luck. Header is same as like in csv header.

Manually selected a Country and saved the node. After that, when I import the csv file, its removing the country field value from DB for that node. Other field values are updating.

Can you please point out my mistake and help solving this issue to import the values from csv file?

10
  • What type of field is the field "Country"? And which module provides that field type?
    – MegaChriz
    Mar 30 at 13:43
  • Note that Feeds doesn't provide targets yet for fields defined by contrib modules - I have tried to make a generic field target, but I ran into the issue that it replaced all existing targets. See drupal.org/project/feeds/issues/3173024
    – MegaChriz
    Mar 30 at 13:47
  • Thanks @MegaChriz , Today got to know about this. Is any where documented where I can see reference for adding custom code for my requirement where I need to add the field which is a contributed module field?
    – JaSiMi
    Mar 30 at 15:14
  • Hi @MegaChriz , Have updated my question, Can you please guide me to import the Country field values from csv file?
    – JaSiMi
    Apr 12 at 13:32
  • I see that there's no item on creating FeedsTarget plugins in the documentation (drupal.org/docs/contributed-modules/feeds) yet, but I see that I did try to explain it before: drupal.org/project/feeds/issues/2997019
    – MegaChriz
    Apr 12 at 14:18

Your Answer

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

Browse other questions tagged or ask your own question.