Invalid link with persistent parameter
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.
Thanks for reporting. Can you please add example code how to induce this bug?
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
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.
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.
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.
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.
Since this is not menu-cottrol bug, closing issue.
@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 You're right. Will think about it.