<?php
/**
* Dieser Quellcode ist geistiges Eigentum der Weblizards GmbH
* und darf ohne vorheriges schriftliches Einverständnis nicht
* vervielfältigt werden.
*
* @copyright Copyright (c) Weblizards GmbH <https://www.weblizards.de>
* @license https://www.gnu.org/licenses/gpl-3.0 GPLv3
*/
namespace App\PortalEngine\EventListener;
use App\Model\DataObject\NiceNameTrait;
use Pimcore\Bundle\PortalEngineBundle\Event\DataObject\ExtractNameEvent;
use Pimcore\Model\DataObject;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ExtractNameSubscriber implements EventSubscriberInterface
{
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [
ExtractNameEvent::class => 'onExtractName',
];
}
public function onExtractName(ExtractNameEvent $event)
{
$object = $event->getObject();
if (array_key_exists(NiceNameTrait::class, class_uses($object)) && method_exists($object, 'getNiceName')) {
$language = $event->getLocale();
$prevGetFallbackValues = DataObject\Localizedfield::getGetFallbackValues();
try {
DataObject\Localizedfield::setGetFallbackValues(true);
/** @var NiceNameTrait $object */
$name = $object->getNiceName($language); // @phpstan-ignore-line
$event->setName($name);
} finally {
DataObject\Localizedfield::setGetFallbackValues($prevGetFallbackValues);
}
}
}
}