<?php
namespace App\EventListener;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\DataObject\Colour;
class ColourListener {
public function onPostAdd (ElementEventInterface $e) {
if (!$e->hasArgument('isAutoSave')) {
if ($e instanceof DataObjectEvent) {
$dataObject = $e->getObject();
if ($dataObject instanceof Colour) {
$conflict = $this->NameConflict($dataObject);
if ($conflict == true) {
throw new \Exception("A colour with that name already exists. Please rename your colour to continue.");
}
}
}
}
}
public function onPreUpdate (ElementEventInterface $e) {
if (!$e->hasArgument('isAutoSave')) {
if ($e instanceof DataObjectEvent) {
$dataObject = $e->getObject();
if ($dataObject instanceof Colour) {
$conflict = $this->NameConflict($dataObject);
if ($conflict == true) {
throw new \Exception("A colour with that name already exists. Please rename your colour to continue.");
}
}
}
}
}
/**
* Checks to see if there are any other colour values with the same name
*
* @param Colour $dataObject
* @return bool
*/
private function NameConflict(Colour $dataObject) {
if ($dataObject->getName() != '') {
$existingColour = Colour::getByName($dataObject->getName());
return false;
if ($existingColour instanceof Colour || $existingColour instanceof Colour\Listing) {
return true;
}
return false;
}
}
}