I am using the Creating a custom Feeds workflow guide to retrieve data from a JSON file on a server and import it as Drupal nodes. My fetcher looks something like this:
namespace Drupal\mymodule\Feeds\Fetcher;
use Drupal\feeds\FeedInterface;
use Drupal\feeds\Plugin\Type\Fetcher\FetcherInterface;
use Drupal\feeds\Plugin\Type\PluginBase;
use Drupal\feeds\Result\RawFetcherResult;
use Drupal\feeds\StateInterface;
/**
* Retrieves test data.
*
* @FeedsFetcher(
* id = "test_fetcher",
* title = @Translation("Test fetcher"),
* description = @Translation("Test fetcher"),
* )
*/
class TestFetcher extends PluginBase implements FetcherInterface {
/**
* {@inheritdoc}
*/
public function fetch(FeedInterface $feed, StateInterface $state) {
$result = $this->getTestData();
if ($result !== false) {
return new RawFetcherResult($result);
}
else {
return new RawFetcherResult('');
}
}
/**
* Make the HTTP request to the test server and return the response.
*
* @return string
* A JSON string of the test data, or false on failure.
*/
public function getTestData() {
$ch = curl_init('https://test-data-server/data.json');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
if (!is_string($response) || !strlen($response)) {
return false;
}
return $response;
}
}
The fetch method successfully retrieves a string of JSON and returns it as a RawFetcherResult, but the request runs into a 500 error before the data ever reaches the parser, with this message:
LogicException: Settings can not be serialized. This probably means you are serializing an object that has an indirect reference to the Settings object. Adjust your code so that is not necessary.
The settings are getting attached to the payload in Feeds' RawFetcherResult __construct method. The Drupal FileSystem class adds the site settings in its own __construct method.
So far, the least hacky way I have found to work around this and allow the data to actually import is to create my own subclass of the Drupal FileSystem with an empty __construct method, and pass that in as the second argument to RawFetcherResult, but it's still a pretty bad hack.
Have I just badly misinterpreted the custom Feeds workflow guide, or is this an issue with the module?
Versions:
drupal 8.7.3
feeds dev-3.x at commit bf3f6e4e8479875cc33eae03a6818aa97d2cb118
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | 3063789-add-DependencySerializationTrait-to-RawFetcherResult-3.patch | 551 bytes | daniel korte |
Comments
Comment #2
megachrizIt sounds like an issue with Feeds. The file system thing was added a few months ago to RawFetcherResult, I believe it was to fix deprecation warnings. I think this issue might be fixed by using the trait DependencySerializationTrait in the RawFetcherResult class.
Comment #3
daniel korte@MegaChriz That worked for me. Thanks!
Comment #5
megachrizThanks for the patch! Committed #3.