-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add documentation for sulu_navigation_is_active Twig function #702
Comments
Must say I did not know about that one but if I look at the pull request of it there are some edge cases which maybe don't work with it, example when working together with static routes or none sulu page routes. As in that case Mostly a So not sure if we should maybe deprecate it 🤔 |
Thats what we use most of the time as well. I came across the above function while looking for a solution to shorten all my if-statements in Twig (since I noticed I was writing "app.request.uri starts with" a lot). I ended up using something like this
I'm not sure if this kind of function is a Sulu-responsibility though. |
You should give the E.g.: {% set itemUrl = sulu_content_path(item.url, item.webspace) %}
{% if sulu_navigation_is_active(itemUrl) %} return str_starts_with($itemUrl, $this->requestStack->getCurrentRequest()->getUri()); |
This is our current implementation, which allows either exact match (but ignoring query params) or sub path matching (e.g. when on https://www.example.com/somepath/subpath, https://www.example.com/somepath is considered active). It also takes care of an edge case that can happen when using just a simple
final readonly class NavigationHelperTwigRuntime implements RuntimeExtensionInterface
{
public function __construct(
private RequestStack $requestStack,
) {}
public function isActiveLink(string $targetUrl, bool $allowSubPath = true): bool
{
$currentRequest = $this->requestStack->getCurrentRequest();
if (null === $currentRequest) {
return false;
}
$requestUrl =
$currentRequest->getSchemeAndHttpHost().$currentRequest->getBaseUrl().$currentRequest->getPathInfo();
return $requestUrl === $targetUrl || ($allowSubPath && $this->isSubPath(
$requestUrl,
$targetUrl,
));
}
private function isSubPath(string $requestUrl, string $targetUrl): bool
{
return str_starts_with($requestUrl, $targetUrl) && '/' === $requestUrl[strlen($targetUrl)];
}
} |
There seems to be a very handy Twig function
sulu_navigation_is_active
, but it's not documented anywhere.It's also not used in the demo project (as far as I can tell), so I assume a lot of people will miss it.
I'd be happy to create a pull-request, but I'm a bit clueless on how to do this.
The text was updated successfully, but these errors were encountered: