<?php
/**
* This file is part of the Pimcore X Installation by
* ercas GmbH & CO. KG <https://www.ercasdieagentur.de>
*
* @license GPLv3
*/
namespace App\EventListener;
use Pimcore\Event\Model\DocumentEvent;
use Pimcore\Model\Document\Page;
class DocumentSaveListener
{
public function updateKey(DocumentEvent $event)
{
$document = $event->getDocument();
if ($document instanceof Page && !strlen($document->getTitle()) && $document->getTitle()) {
$document->setTitle($document->getTitle());
}
$document->setKey($this->sanitizeKey($document->getKey()));
}
public function sanitizeKey(string $str): string
{
$str = trim(strtolower($str));
return str_replace(['----', '---', '--'], ['-', '-', '-'], $str);
}
}