Hi,
I would like to add entries to collection via API. But before I will add entries, I’d like to check if collection contains entry with the same title to prevent from duplikate entries. Is it possible? How to achieve that?
Thank for you help
Hi,
I would like to add entries to collection via API. But before I will add entries, I’d like to check if collection contains entry with the same title to prevent from duplikate entries. Is it possible? How to achieve that?
Thank for you help
You can hook into the collections.save.before
event in your config/bootstrap.php
and check for duplicates before saving. See code below (not tested):
<?php
$app->on('collections.save.before.collection_name', function($collection, &$entry, $isUpdate) {
if (!isset($entry['title'])) return;
$criteria = [
'title' => $entry['title']
];
$duplicate = $this->module('collections')->findOne($collection, $criteria, null, false, []);
if (!$duplicate) return;
// return error message with 412 status header
else $this->stop(['error' => 'title exists already'], 412);
});
I also wrote that a while ago for the permission rules: