Skip to content

Commit

Permalink
NGSTACK-918 adjust ShortcutExtension to work with new link field (nge…
Browse files Browse the repository at this point in the history
…nhancedlink)
  • Loading branch information
hknezevic committed Sep 24, 2024
1 parent 71ee8b3 commit 29ad27d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
48 changes: 27 additions & 21 deletions bundle/Menu/Factory/LocationFactory/ShortcutExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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(),
) {}

Expand All @@ -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

View workflow job for this annotation

GitHub Actions / phpstan

PHPDoc tag @var for variable $link contains unknown class Netgen\IbexaFieldTypeEnhancedLink\FieldType\Value.

if ($location->content->getField('target_blank')->value->bool) {
$this->buildItemFromContent($item, $location->content);
if ($link->isTargetLinkInNewTab()) {

Check failure on line 45 in bundle/Menu/Factory/LocationFactory/ShortcutExtension.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method isTargetLinkInNewTab() on an unknown class Netgen\IbexaFieldTypeEnhancedLink\FieldType\Value.
$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

View workflow job for this annotation

GitHub Actions / phpstan

PHPDoc tag @var for variable $link contains unknown class Netgen\IbexaFieldTypeEnhancedLink\FieldType\Value.

if ($link->isTypeExternal() && is_string($link->reference)) {

Check failure on line 56 in bundle/Menu/Factory/LocationFactory/ShortcutExtension.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to property $reference on an unknown class Netgen\IbexaFieldTypeEnhancedLink\FieldType\Value.

Check failure on line 56 in bundle/Menu/Factory/LocationFactory/ShortcutExtension.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method isTypeExternal() on an unknown class Netgen\IbexaFieldTypeEnhancedLink\FieldType\Value.
$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

View workflow job for this annotation

GitHub Actions / phpstan

Access to property $reference on an unknown class Netgen\IbexaFieldTypeEnhancedLink\FieldType\Value.

Check failure on line 63 in bundle/Menu/Factory/LocationFactory/ShortcutExtension.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to property $reference on an unknown class Netgen\IbexaFieldTypeEnhancedLink\FieldType\Value.

Check failure on line 63 in bundle/Menu/Factory/LocationFactory/ShortcutExtension.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method isTypeInternal() on an unknown class Netgen\IbexaFieldTypeEnhancedLink\FieldType\Value.
$relatedContent = $this->loadService->loadContent($link->reference);

Check failure on line 64 in bundle/Menu/Factory/LocationFactory/ShortcutExtension.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to property $reference on an unknown class Netgen\IbexaFieldTypeEnhancedLink\FieldType\Value.
}

if (!$relatedContent instanceof Content || !$relatedContent->mainLocation instanceof Location) {
Expand All @@ -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

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $link of method Netgen\Bundle\SiteBundle\Menu\Factory\LocationFactory\ShortcutExtension::buildItemFromUrl() has invalid type Netgen\IbexaFieldTypeEnhancedLink\FieldType\Value.
{
$uri = $urlValue->link ?? '';

$uri = $link->reference ?? '';
if (!str_starts_with($uri, 'http')) {
$request = $this->requestStack->getMainRequest();
if (!$request instanceof Request) {
Expand All @@ -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);
}
}
}
1 change: 1 addition & 0 deletions bundle/Resources/config/menu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ services:
arguments:
- "@router"
- "@request_stack"
- "@netgen.ibexa_site_api.load_service"
- "@?logger"
tags:
- { name: ngsite.menu.factory.location.extension, priority: 0 }
Expand Down

0 comments on commit 29ad27d

Please sign in to comment.