Git Product home page Git Product logo

Comments (4)

alexander-schranz avatar alexander-schranz commented on July 26, 2024 1

You should give the full Uri into the navIsActive and use the full uri of the currentRequest. Else you could have false active elements when having multiple webspaces and links to other webspaces in your application navigation.

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());

from sulu-docs.

alexander-schranz avatar alexander-schranz commented on July 26, 2024

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 request.resourceLocator could not be set:

Mostly a starts with is an easier and more bulletproof solution here like its done in the demo:

https://github.com/sulu/sulu-demo/blob/bd4c742e6d5e7fb5fdff050bd829e90023431c18/templates/includes/navbar.html.twig#L17

So not sure if we should maybe deprecate it 🤔

from sulu-docs.

sanderquirynen avatar sanderquirynen commented on July 26, 2024

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

public function navIsActive($itemPath): bool
{
    $path = $this->requestStack->getCurrentRequest()->getPathInfo();

    return str_starts_with($path, $itemPath);
}

I'm not sure if this kind of function is a Sulu-responsibility though.

from sulu-docs.

rogamoore avatar rogamoore commented on July 26, 2024

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 str_starts_with:

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)];
    }
}

from sulu-docs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.