Skip to content

Commit

Permalink
[Tasl(#560)
Browse files Browse the repository at this point in the history
* Update NewsLinkGenerator.php

Add error message that user need to configure 'news_default_document' before StandatdTeaser usage.

* extract IF

---------

Co-authored-by: JiaJia Ji <[email protected]>
  • Loading branch information
kaurov and kingjia90 authored Jan 8, 2024
1 parent fad4319 commit b566a6d
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 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 @@ -79,23 +85,28 @@ public function generate(object $object, array $params = []): string
}

$localeUrlPart = '/' . $this->localeService->getLocale() . '/';
if ($document && $localeUrlPart !== $document->getFullPath()) {
$fullPath = substr($document->getFullPath(), strlen($localeUrlPart));
}

if ($document && !$fullPath) {
$fullPath = $document->getProperty('news_default_document') ? substr($document->getProperty('news_default_document')->getFullPath(), strlen($localeUrlPart)) : '';
if ($document) {
if ($localeUrlPart !== $document->getFullPath()) {
$fullPath = substr($document->getFullPath(), strlen($localeUrlPart));
}
if (!$fullPath) {
$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

1 comment on commit b566a6d

@kingjia90
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😞 sorry for the wrong commit title, misclicked when editing it, and shouldn't force push to amend

Add error message that user need to configure 'news_default_document' before StandatdTeaser usage.

Please sign in to comment.