menu-control icon indicating copy to clipboard operation
menu-control copied to clipboard

Invalid link with persistent parameter

Open olexap opened this issue 5 years ago • 9 comments

Found a bug.

Trying to use route with language parameter which is persistent parameter in presenter. All links generated in project are okay but links in menu-control don't respect actual value of the parameter. Instead there are just default parameter value for route.

olexap avatar Feb 14 '20 08:02 olexap

Thanks for reporting. Can you please add example code how to induce this bug?

foxycode avatar Feb 14 '20 12:02 foxycode

web-project.tar.gz

Adding clean nette web project with implemented navigation with showing generated link and plink in template (they are ok) and menu (not ok)

Try to change language string in url and you see that links are changes but language in menu keep using default from router, not actual value of the variable.

Tested on PHP7.4

olexap avatar Feb 14 '20 15:02 olexap

I trying to write my own menu component and I end at the same problem when I trying to generate url myself (not using latte). It seems like problem is LinkGenerator in nette. It ignores parameters defined in route and use defaults instead. I have no idea how to make it work.

olexap avatar Feb 16 '20 03:02 olexap

Yes, this will be probably the cause, because menu-control uses LinkGenerator which don't known about current state of presenter persistent parameters and thus uses default values.

Solution would be create your own implementation of ILinkGenerator and use Presenter for link generation, but we get rid of Presenter earlier, because we had some problems I don't remember.

foxycode avatar Feb 16 '20 09:02 foxycode

Yes. Nette LinkGenerator is wrong. I wandering that maybe will be possible to use RouteList and Request to make own LinkGenerator but I don't know how to access parameters from router :( Dead end at the moment.

olexap avatar Feb 16 '20 10:02 olexap

For somebody, who stumble upon this as I did, here is a hacky workaround:

use Carrooi\Menu\IMenuItem;
use Carrooi\Menu\LinkGenerator\ILinkGenerator;
use Nette\Application\Application;
use Nette\Application\UI\Presenter;

final class NetteLinkGenerator implements ILinkGenerator
{

    private Presenter $presenter;

    public function __construct(Application $application)
	{
		$this->presenter = $application->getPresenter();
	}


	public function link(IMenuItem $item): string
	{
        if(($action = $item->getAction()) !== null) {
            return $this->presenter->link($action, $item->getActionParameters());
        }

        if (($link = $item->getLink()) !== null) {
            return $link;
        }

        return '#';
	}

}

The caveat is that now it recognizes relative vs absolute links, so in your config file, all your links now have to start with a colon.

I can give you no warranty, I just tried this out and persistent parameters works, but I have no idea if something else isn't broken.

patrickkusebauch avatar Mar 25 '20 06:03 patrickkusebauch

Since this is not menu-cottrol bug, closing issue.

foxycode avatar Mar 30 '20 16:03 foxycode

@foxycode I guess this really depends on the point of view. Since it is a presented as Nette component, but it does not work fully with Nette. It should be at least mentioned somewhere in the documentation, WDYT?

patrickkusebauch avatar Mar 30 '20 16:03 patrickkusebauch

@patrickkusebauch You're right. Will think about it.

foxycode avatar Mar 30 '20 16:03 foxycode