3

I'm attempting to create a new window when a user clicks a link in the navigation menu. What I'm attempting to do at present is just add a target="_blank" item to the url, thereby creating an entirely new page, and from there I'm planning to learn how to change this for my various other needs. The problem is, I'm unable to get the target to go to the associated link.

I've attempted:

$dropdown->addChild('Text', array('route' => 
     'routeName', 'routeParameters' => array('parmName' => 'parameter'), 
     'attr' => array('target' => '_blank'));

But the above results in it not adding the _blank to anything.

$dropdown->addChild('Text', array('route' => 'routeName',
     'routeParameters' => array('paramName' => 'parameter')))
             ->setAttribute('target', '_blank');

Results in the target being set to the li, not to the link itself, as seen below.

<li target="_blank" class="first">        <a href="routeLink">Text</a>

Is there a means to directly set the attribute to the link, so that it will open in a new window when clicked?

Any time and help you can provide is much appreciated.

user2672165
  • 2,986
  • 19
  • 27
Gyhth
  • 1,155
  • 9
  • 21

2 Answers2

17

For what's worth, if you want to do that at the addChild method:

$menu->addChild('Homepage', [
    'route' => 'homepage',
    'linkAttributes' => ['target' => '_blank'],
]);
lsouza
  • 2,448
  • 4
  • 26
  • 39
12

If you are using KnpMenuBundle (it sure looks like it) you can do it like this:

$dropdown->setLinkAttributes(array('target' => '_blank'));
Jay Claiton
  • 437
  • 5
  • 17