2

I've customized one of the footer link blocks within Magento 2's Luma theme in the default.xml file (htdocs/app/design/frontend/Ouho/outwardhound/Magento_Theme/layout/default.xml) with an external link:

<block class="Magento\Framework\View\Element\Html\Link\Current" name="external-link">
  <arguments>
    <argument name="label" xsi:type="string">External Link Name</argument>
    <argument name="path" xsi:type="string">http://example.com</argument>
  </arguments>
</block>

My question is, how can I get this link to open within a new tab using the xml/block/argument structure? I've seen mention of using <aParams>target="_blank"</aParams> elsewhere but it doesn't seem to function no matter where i place it within the code here.

2

2 Answers 2

1

The following will allow you to open a link in a new window (tested in Magento 2.2.3). Note specifically the attributes argument:

    <referenceBlock name="footer_links">
        <block class="Magento\Framework\View\Element\Html\Link\Current" name="about-link" after="contact-us-link">
            <arguments>
                <argument name="label" xsi:type="string" translate="true">About Us</argument>
                <argument name="path" xsi:type="string">about</argument>
                <argument name="attributes" xsi:type="array">
                    <item name="target" xsi:type="string">_blank</item>
                </argument>
            </arguments>
        </block>
    </referenceBlock>

If, for example, we look at the _toHTML and getAttributesHTML() methods of the Magento\Framework\View\Element\Html\Link\Current class, we see how the attributes are appended to the link. Specifically:

private function getAttributesHtml()
{
    $attributesHtml = '';
    $attributes = $this->getAttributes();
    if ($attributes) {
        foreach ($attributes as $attribute => $value) {
            $attributesHtml .= ' ' . $attribute . '="' . $this->escapeHtml($value) . '"';
        }
    }

    return $attributesHtml;
}

So, the item name attribute and value turns into the <a> tag’s attribute and value, respectively.

0

Strictly speaking, not an answer to the original question, but after searching around I ended up here looking for a similar answer so I thought I'd drop this in case it's helpful for someone else.

This worked for me in Magento 2.4, in the header.panel:

    <arguments>
        <argument name="label" xsi:type="string" translate="true">External Link</argument>
        <argument name="path" xsi:type="string" translate="true">https://external.link</argument>
        <argument name="title" xsi:type="string" translate="true">Visit external.link!</argument>
        <argument name="target" xsi:type="string">_blank</argument>
    </arguments>

Output:

<a href="https://external.link" title="Visit external.link!" target="_blank" id="idtgk9TxM0">External Link</a>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.