_blank attribute doesn't get set I'm trying to target on the Menu (Bootstrap) a _blank attribute.

This is my code. It works on my local machine.

if ($variables['url']-&gt;isExternal()) {
  $variables['options']['attributes'] = ['target' =&gt; '_blank'];
}


The HTML target.

&lt;ul class="dropdown-menu"&gt;
  &lt;li&gt;
    &lt;a href="https://example.com" target="_blank"&gt;MySite&lt;/a&gt;
  &lt;/li&gt;
&lt;ul&gt;


BUT,

In here is not working because of the "class="first". 

&lt;ul class="dropdown-menu"&gt;
  &lt;li class="first"&gt;
    &lt;a href="https://example.com/1"&gt;Example link 1&lt;/a&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;a href="https://example.com/2"&gt;Example link 2&lt;/a&gt;
  &lt;/li&gt;
  &lt;li class="last"&gt;
    &lt;a href="https://example.com/3"&gt;Example link 3&lt;/a&gt;
  &lt;/li&gt;
&lt;/ul&gt;


I'm missing something, any help? I don't want to install a module for this.
When you write $variables['options']['attributes'] = ['target' =&gt; '_blank'] you are replacing all attributes, never ever do that! 

Only set specific keys:
$variables['options']['attributes']['target'] = '_blank'.

If that key is an array, append your value:
$variables['options']['attributes']['class'][] = 'appended_class'