Skip to content

Commit

Permalink
Update NewsLinkGenerator.php
Browse files Browse the repository at this point in the history
Add error message that user need to configure 'news_default_document' before StandatdTeaser usage.
  • Loading branch information
kaurov authored Dec 19, 2023
1 parent 8b1d8c0 commit bf584b0
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/Website/LinkGenerator/NewsLinkGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* 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
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PEL
*/

namespace App\Website\LinkGenerator;
Expand All @@ -25,8 +25,14 @@
use Pimcore\Twig\Extension\Templating\PimcoreUrl;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Generates URI links for News Data-Objects
* @todo stop using direct ID. You can use slug field or at least UUID
*/
class NewsLinkGenerator implements LinkGeneratorInterface
{
const DEFAULT_DOCUMENT = 'news_default_document';

/**
* @var DocumentResolver
*/
Expand Down Expand Up @@ -84,18 +90,21 @@ public function generate(object $object, array $params = []): string
}

if ($document && !$fullPath) {
$fullPath = $document->getProperty('news_default_document') ? substr($document->getProperty('news_default_document')->getFullPath(), strlen($localeUrlPart)) : '';
$fullPath = $document->getProperty(static::DEFAULT_DOCUMENT) ? substr($document->getProperty(static::DEFAULT_DOCUMENT)->getFullPath(), strlen($localeUrlPart)) : '';
}
if (!$fullPath) {
throw new \InvalidArgumentException('Set parameter '.static::DEFAULT_DOCUMENT.' for the root or current document.');
}

$locale = $params['locale'] ?? null;

return $this->pimcoreUrl->__invoke(
[
'newstitle' => Text::toUrl($object->getTitle($locale) ? $object->getTitle($locale) : 'news'),
'news' => $object->getId(),
'path' => $fullPath,
'_locale' => $locale,
],
'newstitle' => Text::toUrl($object->getTitle($locale) ? $object->getTitle($locale) : 'news'),
'news' => $object->getId(),
'path' => $fullPath,
'_locale' => $locale,
],
'news-detail',
true
);
Expand Down

0 comments on commit bf584b0

Please sign in to comment.