Skip to content
Snippets Groups Projects
Commit fecad6d3 authored by Maksim Kulichkin's avatar Maksim Kulichkin Committed by Maksim Kulichkin
Browse files

Issue #3313031 by SAVEL, dmitry.korkhau: Add "Remove duplicates" Event

parent c9365f75
No related branches found
No related tags found
1 merge request!17Issue #3313031 by SAVEL, dmitry.korkhau: Add "Remove duplicates" Event
......@@ -86,8 +86,11 @@ class CreditParsersManager extends DefaultPluginManager {
/** @var \Drupal\dosd\Entity\CreditEntityInterface $credit */
$event = new CreditPresaveEvent($credit);
$this->eventDispatcher->dispatch($event, CreditPresaveEvent::PRE_SAVE);
$credit = $event->getCredit();
$credit->save();
if ($event->checkShouldSave()) {
$credit->save();
}
}
}
......
......@@ -87,4 +87,25 @@ class CreditEntity extends ContentEntityBase implements CreditEntityInterface {
return $fields;
}
/**
* Checks if the credit is already in DB.
*
* @return bool
* True if credit is already stored in the DB.
*/
public function isStored() {
$storage = $this->entityTypeManager()->getStorage('credit_entity');
$ids = $storage->getQuery()
->accessCheck(FALSE)
->condition('link', $this->get('link')->getString())
->condition('type', $this->get('type')->getString())
->condition('uid', $this->get('uid')->getString())
->execute();
return (bool) $ids;
}
}
......@@ -8,5 +8,11 @@ use Drupal\Core\Entity\ContentEntityInterface;
* Inteface for custom credit types implementation.
*/
interface CreditEntityInterface extends ContentEntityInterface {
/**
* Checks if the credit is already in DB.
*
* @return bool
* True if credit is already stored in the DB.
*/
public function isStored();
}
......@@ -17,7 +17,15 @@ class CreditPresaveEvent extends Event {
*
* @var \Drupal\dosd\Entity\CreditEntityInterface
*/
public $credit;
protected $credit;
/**
* Indicates if we should save the credit.
*
* @var bool
* TRUE if the credit should be saved.
*/
protected bool $shouldSave = TRUE;
/**
* Constructs the object.
......@@ -49,4 +57,21 @@ class CreditPresaveEvent extends Event {
$this->credit = $credit;
}
/**
* Sets ShouldSave marker to FALSE.
*/
public function doNotSave() {
$this->shouldSave = FALSE;
}
/**
* Returns ShouldSave marker.
*
* @return bool
* ShouldSave value.
*/
public function checkShouldSave() {
return $this->shouldSave;
}
}
......@@ -14,9 +14,12 @@ class CreditPreSaveEventSubscriber implements EventSubscriberInterface {
* Removes Credit if already collected.
*/
public function filterDuplicates(CreditPresaveEvent $event) {
// @todo Add unique check.
// $credit = $event->getCredit();
// $event->setCredit($credit);
$credit = $event->getCredit();
if ($credit->isStored()) {
$event->doNotSave();
}
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment