Skip to content

Commit ff3d23d

Browse files
authored
Merge pull request #55 from netgen/NGSTACK-918-menu-shortcut-enhanced-link
NGSTACK-918 adjust ShortcutExtension to work with new link field (ngenhancedlink)
2 parents 71ee8b3 + 30170d7 commit ff3d23d

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
lines changed

bundle/Menu/Factory/LocationFactory/ShortcutExtension.php

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
namespace Netgen\Bundle\SiteBundle\Menu\Factory\LocationFactory;
66

7-
use Ibexa\Core\FieldType\Url\Value as UrlValue;
87
use Ibexa\Core\MVC\Symfony\SiteAccess\URILexer;
98
use Knp\Menu\ItemInterface;
9+
use Netgen\IbexaFieldTypeEnhancedLink\FieldType\Value as EnhancedLinkValue;
10+
use Netgen\IbexaSiteApi\API\LoadService;
1011
use Netgen\IbexaSiteApi\API\Values\Content;
1112
use Netgen\IbexaSiteApi\API\Values\Location;
1213
use Psr\Log\LoggerInterface;
@@ -16,6 +17,8 @@
1617
use Symfony\Component\HttpFoundation\RequestStack;
1718
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1819

20+
use function is_int;
21+
use function is_string;
1922
use function sprintf;
2023
use function str_starts_with;
2124

@@ -24,6 +27,7 @@ final class ShortcutExtension implements ExtensionInterface
2427
public function __construct(
2528
private UrlGeneratorInterface $urlGenerator,
2629
private RequestStack $requestStack,
30+
private LoadService $loadService,
2731
private LoggerInterface $logger = new NullLogger(),
2832
) {}
2933

@@ -34,27 +38,30 @@ public function matches(Location $location): bool
3438

3539
public function buildItem(ItemInterface $item, Location $location): void
3640
{
37-
$this->buildItemFromContent($item, $location->content);
41+
/** @var EnhancedLinkValue $link */
42+
$link = $location->content->getField('link')->value;
3843

39-
if ($location->content->getField('target_blank')->value->bool) {
44+
$this->buildItemFromContent($item, $location->content);
45+
if ($link->isTargetLinkInNewTab()) {
4046
$item->setLinkAttribute('target', '_blank')
4147
->setLinkAttribute('rel', 'nofollow noopener noreferrer');
4248
}
4349
}
4450

4551
private function buildItemFromContent(ItemInterface $item, Content $content): void
4652
{
47-
if (!$content->getField('url')->isEmpty()) {
48-
/** @var \Ibexa\Core\FieldType\Url\Value $urlValue */
49-
$urlValue = $content->getField('url')->value;
50-
$this->buildItemFromUrl($item, $urlValue, $content);
53+
/** @var EnhancedLinkValue $link */
54+
$link = $content->getField('link')->value;
55+
56+
if ($link->isTypeExternal() && is_string($link->reference)) {
57+
$this->buildItemFromUrl($item, $link, $content);
5158

5259
return;
5360
}
5461

5562
$relatedContent = null;
56-
if (!$content->getField('related_object')->isEmpty()) {
57-
$relatedContent = $content->getFieldRelation('related_object');
63+
if ($link->isTypeInternal() && is_int($link->reference) && $link->reference > 0) {
64+
$relatedContent = $this->loadService->loadContent($link->reference);
5865
}
5966

6067
if (!$relatedContent instanceof Content || !$relatedContent->mainLocation instanceof Location) {
@@ -67,13 +74,12 @@ private function buildItemFromContent(ItemInterface $item, Content $content): vo
6774
return;
6875
}
6976

70-
$this->buildItemFromRelatedContent($item, $content, $relatedContent);
77+
$this->buildItemFromRelatedContent($item, $link, $content, $relatedContent);
7178
}
7279

73-
private function buildItemFromUrl(ItemInterface $item, UrlValue $urlValue, Content $content): void
80+
private function buildItemFromUrl(ItemInterface $item, EnhancedLinkValue $link, Content $content): void
7481
{
75-
$uri = $urlValue->link ?? '';
76-
82+
$uri = $link->reference ?? '';
7783
if (!str_starts_with($uri, 'http')) {
7884
$request = $this->requestStack->getMainRequest();
7985
if (!$request instanceof Request) {
@@ -88,25 +94,25 @@ private function buildItemFromUrl(ItemInterface $item, UrlValue $urlValue, Conte
8894

8995
$item->setUri($uri);
9096

91-
if (($urlValue->text ?? '') !== '') {
92-
$item->setLinkAttribute('title', $urlValue->text);
97+
if (($link->label ?? '') !== '') {
98+
$item->setLinkAttribute('title', $link->label);
9399

94100
if (!$content->getField('use_shortcut_name')->value->bool) {
95-
$item->setLabel($urlValue->text);
101+
$item->setLabel($link->label);
96102
}
97103
}
98104
}
99105

100-
private function buildItemFromRelatedContent(ItemInterface $item, Content $content, Content $relatedContent): void
106+
private function buildItemFromRelatedContent(ItemInterface $item, EnhancedLinkValue $link, Content $content, Content $relatedContent): void
101107
{
108+
if (!$content->getField('use_shortcut_name')->value->bool) {
109+
$item->setLabel($link->label ?? $relatedContent->name);
110+
}
111+
102112
$contentUri = $this->urlGenerator->generate('', [RouteObjectInterface::ROUTE_OBJECT => $relatedContent]);
103-
$item->setUri($contentUri . $content->getField('internal_url_suffix')->value->text)
113+
$item->setUri($contentUri . ($link->suffix ?? ''))
104114
->setExtra('ibexa_location', $relatedContent->mainLocation)
105115
->setAttribute('id', 'menu-item-' . $item->getExtra('menu_name') . '-location-id-' . $relatedContent->mainLocationId)
106116
->setLinkAttribute('title', $item->getLabel());
107-
108-
if (!$content->getField('use_shortcut_name')->value->bool) {
109-
$item->setLabel($relatedContent->name);
110-
}
111117
}
112118
}

bundle/Resources/config/menu.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ services:
3333
arguments:
3434
- "@router"
3535
- "@request_stack"
36+
- "@netgen.ibexa_site_api.load_service"
3637
- "@?logger"
3738
tags:
3839
- { name: ngsite.menu.factory.location.extension, priority: 0 }

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"symfony/mailer": "^5.4",
2323
"netgen/ibexa-forms-bundle": "^4.0",
2424
"netgen/ibexa-site-api": "^6.1",
25+
"netgen/ibexa-fieldtype-enhanced-link": "^1.1",
2526
"netgen/information-collection-bundle": "^3.0",
2627
"netgen/siteaccess-routes-bundle": "^3.0",
2728
"netgen/content-type-list-bundle": "^3.0",

0 commit comments

Comments
 (0)