KnpMenuBundle icon indicating copy to clipboard operation
KnpMenuBundle copied to clipboard

Current item is not marked as current

Open ciajulius opened this issue 9 years ago • 3 comments

Hello,

I have an issue with this bundle. I've set up my menu, it works fine, but current item is not set. But it is set if I add same route in routes array in extras, item is being set as current.

Heres my code:

$menu = $this->factory->createItem('root')->setChildrenAttributes(['class' => 'sidebar-menu']);

$menu
    ->addChild('dashboard', [
         'route' => 'app_dashboard_index',
          'label' => 'Dashboard',
    ])
    ->setExtras([
        'icon' => 'fa fa-dashboard',
    ]);

With this configuration, current item is not being set. But with this configuration

...
    ->setExtras([
        'icon' => 'fa fa-dashboard',
        'routes' => [
            'app_dashboard_index',
        ]
    ])

current item is being set.

Doesn't this look suspicious in RouteVoter?:

public function matchItem(ItemInterface $item)
    {
        if (null === $this->request) {
            return null;
        }

        $route = $this->request->attributes->get('_route');
        if (null === $route) {
            return null;
        }

        $routes = (array) $item->getExtra('routes', array());

        foreach ($routes as $testedRoute) {
            if (is_string($testedRoute)) {
                $testedRoute = array('route' => $testedRoute);
            }

            if (!is_array($testedRoute)) {
                throw new \InvalidArgumentException('Routes extra items must be strings or arrays.');
            }

            if ($this->isMatchingRoute($testedRoute)) {
                return true;
            }
        }

        return null;
    }

Suspicious, because this method is being called from Matcher::isCurrent() method, and it takes _route from request only to check if it's there, but it does not compare it with item's route .

BTW: I'm using symfony 3.3.5

ciajulius avatar Aug 08 '17 12:08 ciajulius

Well, setExtras is replacing all extras. It does not merge with the current extras.

The top-level route option has 2 effects:

  • it configures the uri option using the router to generate it (if you also configure uri explicitly at the same time, the route option will overwrite it btw)
  • it adds the route in the routes extra for matching purposes.

The fact that you used setExtras to replace all extras instead of providing extras directly in the addChild call means that your own extras are overwriting the second part of the list above (and so the route is not matched). This code will work fine:

$menu
    ->addChild('dashboard', [
         'route' => 'app_dashboard_index',
          'label' => 'Dashboard',
          'extras' => [
              'icon' => 'fa fa-dashboard',
          ],
    ]);

Items don't have a route. They only have extras and uri. The route in addChild is only a factory-level extension.

stof avatar Aug 08 '17 13:08 stof

@stof Got it. It's working perfectly now, and looks cleaner. Thanks!

ciajulius avatar Aug 08 '17 14:08 ciajulius

To be honest, I never use setExtras in my own code. I'm always putting them directly in addChild

stof avatar Aug 08 '17 14:08 stof