4
4
5
5
namespace Netgen \Bundle \SiteBundle \Menu \Factory \LocationFactory ;
6
6
7
- use Ibexa \Core \FieldType \Url \Value as UrlValue ;
8
7
use Ibexa \Core \MVC \Symfony \SiteAccess \URILexer ;
9
8
use Knp \Menu \ItemInterface ;
9
+ use Netgen \IbexaFieldTypeEnhancedLink \FieldType \Value as EnhancedLinkValue ;
10
+ use Netgen \IbexaSiteApi \API \LoadService ;
10
11
use Netgen \IbexaSiteApi \API \Values \Content ;
11
12
use Netgen \IbexaSiteApi \API \Values \Location ;
12
13
use Psr \Log \LoggerInterface ;
16
17
use Symfony \Component \HttpFoundation \RequestStack ;
17
18
use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
18
19
20
+ use function is_int ;
21
+ use function is_string ;
19
22
use function sprintf ;
20
23
use function str_starts_with ;
21
24
@@ -24,6 +27,7 @@ final class ShortcutExtension implements ExtensionInterface
24
27
public function __construct (
25
28
private UrlGeneratorInterface $ urlGenerator ,
26
29
private RequestStack $ requestStack ,
30
+ private LoadService $ loadService ,
27
31
private LoggerInterface $ logger = new NullLogger (),
28
32
) {}
29
33
@@ -34,27 +38,30 @@ public function matches(Location $location): bool
34
38
35
39
public function buildItem (ItemInterface $ item , Location $ location ): void
36
40
{
37
- $ this ->buildItemFromContent ($ item , $ location ->content );
41
+ /** @var EnhancedLinkValue $link */
42
+ $ link = $ location ->content ->getField ('link ' )->value ;
38
43
39
- if ($ location ->content ->getField ('target_blank ' )->value ->bool ) {
44
+ $ this ->buildItemFromContent ($ item , $ location ->content );
45
+ if ($ link ->isTargetLinkInNewTab ()) {
40
46
$ item ->setLinkAttribute ('target ' , '_blank ' )
41
47
->setLinkAttribute ('rel ' , 'nofollow noopener noreferrer ' );
42
48
}
43
49
}
44
50
45
51
private function buildItemFromContent (ItemInterface $ item , Content $ content ): void
46
52
{
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 );
51
58
52
59
return ;
53
60
}
54
61
55
62
$ 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 );
58
65
}
59
66
60
67
if (!$ relatedContent instanceof Content || !$ relatedContent ->mainLocation instanceof Location) {
@@ -67,13 +74,12 @@ private function buildItemFromContent(ItemInterface $item, Content $content): vo
67
74
return ;
68
75
}
69
76
70
- $ this ->buildItemFromRelatedContent ($ item , $ content , $ relatedContent );
77
+ $ this ->buildItemFromRelatedContent ($ item , $ link , $ content , $ relatedContent );
71
78
}
72
79
73
- private function buildItemFromUrl (ItemInterface $ item , UrlValue $ urlValue , Content $ content ): void
80
+ private function buildItemFromUrl (ItemInterface $ item , EnhancedLinkValue $ link , Content $ content ): void
74
81
{
75
- $ uri = $ urlValue ->link ?? '' ;
76
-
82
+ $ uri = $ link ->reference ?? '' ;
77
83
if (!str_starts_with ($ uri , 'http ' )) {
78
84
$ request = $ this ->requestStack ->getMainRequest ();
79
85
if (!$ request instanceof Request) {
@@ -88,25 +94,25 @@ private function buildItemFromUrl(ItemInterface $item, UrlValue $urlValue, Conte
88
94
89
95
$ item ->setUri ($ uri );
90
96
91
- if (($ urlValue -> text ?? '' ) !== '' ) {
92
- $ item ->setLinkAttribute ('title ' , $ urlValue -> text );
97
+ if (($ link -> label ?? '' ) !== '' ) {
98
+ $ item ->setLinkAttribute ('title ' , $ link -> label );
93
99
94
100
if (!$ content ->getField ('use_shortcut_name ' )->value ->bool ) {
95
- $ item ->setLabel ($ urlValue -> text );
101
+ $ item ->setLabel ($ link -> label );
96
102
}
97
103
}
98
104
}
99
105
100
- private function buildItemFromRelatedContent (ItemInterface $ item , Content $ content , Content $ relatedContent ): void
106
+ private function buildItemFromRelatedContent (ItemInterface $ item , EnhancedLinkValue $ link , Content $ content , Content $ relatedContent ): void
101
107
{
108
+ if (!$ content ->getField ('use_shortcut_name ' )->value ->bool ) {
109
+ $ item ->setLabel ($ link ->label ?? $ relatedContent ->name );
110
+ }
111
+
102
112
$ 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 ?? '' ) )
104
114
->setExtra ('ibexa_location ' , $ relatedContent ->mainLocation )
105
115
->setAttribute ('id ' , 'menu-item- ' . $ item ->getExtra ('menu_name ' ) . '-location-id- ' . $ relatedContent ->mainLocationId )
106
116
->setLinkAttribute ('title ' , $ item ->getLabel ());
107
-
108
- if (!$ content ->getField ('use_shortcut_name ' )->value ->bool ) {
109
- $ item ->setLabel ($ relatedContent ->name );
110
- }
111
117
}
112
118
}
0 commit comments