verbb / navigation

A Craft CMS plugin to create navigation menus for your site.
Other
90 stars 22 forks source link

When using paginate tag getActiveNode does not return active node #379

Closed stanislavprokopov closed 7 months ago

stanislavprokopov commented 7 months ago

Describe the bug

When using paginate tag, the urls that it generate look smth like https://domain/{slug}/page/2, when you navigate to this page, and then try to use craft.navigation.getActiveNode it will return you null, as Node._getActive does not handle page/x in any way so it fails to find active node.

Steps to reproduce

  1. In config/general.php add ->pageTrigger('page/'), see manual
  2. Create new Navigation, handle test, with one Site Url with URL test
  3. Add test.twig to templates with following content
    <html>
    <body>
    {% set activeNode = craft.navigation.getActiveNode({ handle: 'test' }) %}
    {{ dump(activeNode) }}
    </body>
    </html>
  4. Open https://localhost/test, you should see corresponding verbb\navigation\elements\Node active node
  5. Now open https://localhost/test/page/2, this time you will see null

Note: This is a simple example that achieves the same result without actually setting up collection and using paginate tag to query elements

Craft CMS version

4.5.14

Plugin version

2.0.24

Multi-site?

Yes

Additional context

No response

stanislavprokopov commented 7 months ago

This is how craft is handling page detection, so added this to Node._getActive, seems like its working.

// Is this a paginated request?
$pageTrigger = Craft::$app->getConfig()->getGeneral()->getPageTrigger();

// If this is not query string-based pagination, then need to cleanup currentUrl
if (!str_starts_with($pageTrigger, '?')) {
    // Match against the entire path string as opposed to just the last segment so that we can support
    // "/page/2"-style pagination URLs
    $pageTrigger = preg_quote($pageTrigger, '/');

    if (preg_match("/^(?:(.*)\/)?$pageTrigger(\d+)$/", $currentUrl, $match)) {
        $currentUrl = $match[1];
    }
}
engram-design commented 7 months ago

Good point, hadn't actually considered using non-query string pagination. Updated for the next release. To get this early, run composer require verbb/navigation:"dev-craft-4 as 2.0.24".

engram-design commented 7 months ago

Fixed in 2.0.25