0

I am trying to import data to Drupal 8 website using Feeds and Feeds_ex module and here is my scenario:

  • I need to make a call to the first JSON API and extract the IDs from it, here is a sample of the API

    {
    "rm": [{
        "mn": "Friday",
        "id": 3003,
        "ra": "JEB  ",
        "md": "2019-11-01 00:00:00.0"
    }, {
        "mn": "Saturday",
        "id": 3014,
        "ra": "SHJ  ",
        "md": "2019-11-02 00:00:00.0"
    }]
    

    }

  • Then I want to use the extracted IDs to construct custom URLs to get the actual data that I want to import.

    http:/somewebsite/api/index.php?id=1234

I looked to Feeds documentation to create a custom fetcher with no luck.Any advice?

4
  • A custom fetcher seems like an appropriate approach to me. Can you share your code and/or give more detail about what wasn't working? Were you getting an error message? Also which feeds documentation were you looking at? The Creating a Custom Feeds Workflow - Fetcher guide on d.o? – sonfd Dec 14 '19 at 17:00
  • Thank for your interest. Actually I managed to resolve the issue. – Hodba Khalaf Dec 15 '19 at 11:24
  • Excellent! You should post your solution as an answer for anyone else who comes along with the same question. (You don't need to include all details if you don't want to.) – sonfd Dec 15 '19 at 14:42
  • Sure, I am working on creating a custom module and when it's ready I will post it here. – Hodba Khalaf Dec 17 '19 at 6:52
0

The trick is to create a new parser, not a new fetcher. I created a new module and a new parser at the following: New files at MY_MODULE/src/Feeds/Parser/MymoduleJsonPathParser.php

I wanted to build on top of the feeds_ex module so I used their interfaces as the following:

use RuntimeException;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\feeds\FeedInterface;
use Drupal\feeds\Result\FetcherResultInterface;
use Drupal\feeds\Result\ParserResultInterface;
use Drupal\feeds\StateInterface;
use Peekmo\JsonPath\JsonStore;
use Drupal\feeds_ex\feeds\Parser\JsonParserBase;
use Drupal\feeds_ex\feeds\Parser\ParserBase;
use Drupal\Core\Form\FormStateInterface;

If you would like to add custom options that appear at the feeds UI add your custom form at the following:

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);

$form['FIELD'] = [ .....];
return $form;

}

Now you can build on your custom parser:

    protected function executeContext(FeedInterface $feed, FetcherResultInterface $fetcher_result, StateInterface $state) {
    $raw = $this->prepareRaw($fetcher_result);
    $parsed = $this->utility->decodeJsonArray($raw);
    // Your code gose here.
  }

I am writing a custom module and I will share it here when it's finished.

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.