Forum
Perch Collection Import API does not handle repeaters
When importing with the Perch API into a collection, whenever a repeater field is used in the template, the script fails.
Error:
Uncaught TypeError: Argument 2 passed to PerchFieldType::__construct() must be an instance of PerchXMLTag, instance of PerchRepeater given
Template:
<perch:categories id="category" label="Product Category" set="product-categories" max="1" required="true">
<perch:content type="text" id="model" label="Model Number" title="true" escape="false" />
<perch:content type="text" id="name" label="Name" escape="false" />
<perch:repeater id="features" label="Features" max="8">
<perch:content type="text" id="feature" label="Feature" />
</perch:repeater>
Import Script:
<?php
include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php');
$API = new PerchAPI(1.0, 'product_importer');
$ProductImporter = $API->get('CollectionImporter');
$ProductImporter->set_collection('Products');
$Template = $API->get('Template');
$Template->set('content/data', 'content');
$ProductImporter->set_template($Template);
$products = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'].'/import/json/products.json'));
foreach ($products as $product) {
$features = [];
for ($i=1; $i < 7; $i++) {
if ($product->{'Feature '.$i}) {
array_push($features, ['feature'=>$product->{'Feature '.$i}]);
}
}
try {
$ProductImporter->add_item([
'category' => ['product-categories/cooktops'],
'model' => $product->{'Model'},
'name' => $product->{'Name'},
'features' => $features,
]);
} catch (Exception $e) {
die('Error: '.$e->getMessage());
}
}
echo 'done';
That's right - it doesn't handle repeaters or blocks currently.
Ok, that is probably something you should note on the API Reference.
When is this expected to be implemented?
It's not currently scheduled - we wanted to see if anyone would actually use the API first.
What's the use case you're looking at?
Taking an export of products and a varied number of associated files from a DMP and importing them using a script. Pretty much exactly what an import API is meant for.
I understand that it's something that will get minimal usage so I see the problems of dedicating time to it. A way of creating content programmatically rather than relying on the admin interface is something that Perch would benefit from in the long term.
Another update for the docs, using assets to assign to fields only works if you have both the original field name in addition to the
{$field}_assetID
in the data.For example. this works:
However, this does not:
In addition, if the field is required in the template, the value must be set to a truthy value.
That's not the docs, that's a known bug. We'll get it fixed in a future update.
Yes, validation rules from the template are enforced.