<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Enterprise License (PEL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PEL
*/
namespace App\EventListener;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\DataObject\Tag;
use Pimcore\Model\DataObject\TagCategory;
class AdminListener
{
public function onTagAddOrUpdate(ElementEventInterface $e) {
if ($e instanceof DataObjectEvent) {
$tag = $e->getObject();
if (!($tag instanceof Tag)) {
return true;
}
$parentOfTag = $tag->getParent();
if ($parentOfTag instanceof TagCategory) {
$parentsTags = $parentOfTag->getTags();
$parentsTags[] = $tag;
try {
$parentOfTag->setTags($parentsTags);
$parentOfTag->save();
} catch (\Throwable $th) {
// catch and do nothing if tag was already in there
}
}
}
}
}