-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
…nhancedlink)
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,10 @@ | |
|
||
namespace Netgen\Bundle\SiteBundle\Menu\Factory\LocationFactory; | ||
|
||
use Ibexa\Core\FieldType\Url\Value as UrlValue; | ||
use Ibexa\Core\MVC\Symfony\SiteAccess\URILexer; | ||
use Knp\Menu\ItemInterface; | ||
use Netgen\IbexaFieldTypeEnhancedLink\FieldType\Value as EnhancedLinkValue; | ||
use Netgen\IbexaSiteApi\API\LoadService; | ||
use Netgen\IbexaSiteApi\API\Values\Content; | ||
use Netgen\IbexaSiteApi\API\Values\Location; | ||
use Psr\Log\LoggerInterface; | ||
|
@@ -16,6 +17,8 @@ | |
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
|
||
use function is_int; | ||
use function is_string; | ||
use function sprintf; | ||
use function str_starts_with; | ||
|
||
|
@@ -24,6 +27,7 @@ final class ShortcutExtension implements ExtensionInterface | |
public function __construct( | ||
private UrlGeneratorInterface $urlGenerator, | ||
private RequestStack $requestStack, | ||
private LoadService $loadService, | ||
private LoggerInterface $logger = new NullLogger(), | ||
) {} | ||
|
||
|
@@ -34,27 +38,30 @@ public function matches(Location $location): bool | |
|
||
public function buildItem(ItemInterface $item, Location $location): void | ||
{ | ||
$this->buildItemFromContent($item, $location->content); | ||
/** @var EnhancedLinkValue $link */ | ||
$link = $location->content->getField('link')->value; | ||
Check failure on line 42 in bundle/Menu/Factory/LocationFactory/ShortcutExtension.php GitHub Actions / phpstan
|
||
|
||
if ($location->content->getField('target_blank')->value->bool) { | ||
$this->buildItemFromContent($item, $location->content); | ||
if ($link->isTargetLinkInNewTab()) { | ||
$item->setLinkAttribute('target', '_blank') | ||
->setLinkAttribute('rel', 'nofollow noopener noreferrer'); | ||
} | ||
} | ||
|
||
private function buildItemFromContent(ItemInterface $item, Content $content): void | ||
{ | ||
if (!$content->getField('url')->isEmpty()) { | ||
/** @var \Ibexa\Core\FieldType\Url\Value $urlValue */ | ||
$urlValue = $content->getField('url')->value; | ||
$this->buildItemFromUrl($item, $urlValue, $content); | ||
/** @var EnhancedLinkValue $link */ | ||
$link = $content->getField('link')->value; | ||
Check failure on line 54 in bundle/Menu/Factory/LocationFactory/ShortcutExtension.php GitHub Actions / phpstan
|
||
|
||
if ($link->isTypeExternal() && is_string($link->reference)) { | ||
Check failure on line 56 in bundle/Menu/Factory/LocationFactory/ShortcutExtension.php GitHub Actions / phpstan
|
||
$this->buildItemFromUrl($item, $link, $content); | ||
|
||
return; | ||
} | ||
|
||
$relatedContent = null; | ||
if (!$content->getField('related_object')->isEmpty()) { | ||
$relatedContent = $content->getFieldRelation('related_object'); | ||
if ($link->isTypeInternal() && is_int($link->reference) && $link->reference > 0) { | ||
Check failure on line 63 in bundle/Menu/Factory/LocationFactory/ShortcutExtension.php GitHub Actions / phpstan
Check failure on line 63 in bundle/Menu/Factory/LocationFactory/ShortcutExtension.php GitHub Actions / phpstan
|
||
$relatedContent = $this->loadService->loadContent($link->reference); | ||
} | ||
|
||
if (!$relatedContent instanceof Content || !$relatedContent->mainLocation instanceof Location) { | ||
|
@@ -67,13 +74,12 @@ private function buildItemFromContent(ItemInterface $item, Content $content): vo | |
return; | ||
} | ||
|
||
$this->buildItemFromRelatedContent($item, $content, $relatedContent); | ||
$this->buildItemFromRelatedContent($item, $link, $content, $relatedContent); | ||
} | ||
|
||
private function buildItemFromUrl(ItemInterface $item, UrlValue $urlValue, Content $content): void | ||
private function buildItemFromUrl(ItemInterface $item, EnhancedLinkValue $link, Content $content): void | ||
Check failure on line 80 in bundle/Menu/Factory/LocationFactory/ShortcutExtension.php GitHub Actions / phpstan
|
||
{ | ||
$uri = $urlValue->link ?? ''; | ||
|
||
$uri = $link->reference ?? ''; | ||
if (!str_starts_with($uri, 'http')) { | ||
$request = $this->requestStack->getMainRequest(); | ||
if (!$request instanceof Request) { | ||
|
@@ -88,25 +94,25 @@ private function buildItemFromUrl(ItemInterface $item, UrlValue $urlValue, Conte | |
|
||
$item->setUri($uri); | ||
|
||
if (($urlValue->text ?? '') !== '') { | ||
$item->setLinkAttribute('title', $urlValue->text); | ||
if (($link->label ?? '') !== '') { | ||
$item->setLinkAttribute('title', $link->label); | ||
|
||
if (!$content->getField('use_shortcut_name')->value->bool) { | ||
$item->setLabel($urlValue->text); | ||
} | ||
} | ||
} | ||
|
||
private function buildItemFromRelatedContent(ItemInterface $item, Content $content, Content $relatedContent): void | ||
private function buildItemFromRelatedContent(ItemInterface $item, EnhancedLinkValue $link, Content $content, Content $relatedContent): void | ||
{ | ||
if (!$content->getField('use_shortcut_name')->value->bool) { | ||
$item->setLabel($link->label ?? $relatedContent->name); | ||
} | ||
|
||
$contentUri = $this->urlGenerator->generate('', [RouteObjectInterface::ROUTE_OBJECT => $relatedContent]); | ||
$item->setUri($contentUri . $content->getField('internal_url_suffix')->value->text) | ||
$item->setUri($contentUri . ($link->suffix ?? '')) | ||
->setExtra('ibexa_location', $relatedContent->mainLocation) | ||
->setAttribute('id', 'menu-item-' . $item->getExtra('menu_name') . '-location-id-' . $relatedContent->mainLocationId) | ||
->setLinkAttribute('title', $item->getLabel()); | ||
|
||
if (!$content->getField('use_shortcut_name')->value->bool) { | ||
$item->setLabel($relatedContent->name); | ||
} | ||
} | ||
} |