2

Attaching a link to an image is not an uncommon action to take, and I'm trying to accomplish this within the default.xml of my theme. We have a static image of a google maps location, and we want it to link to Google's map app.

XML

<block class="Magento\Framework\View\Element\Html\Link" name="location.link">
  <arguments>
    <argument name="label" xsi:type="string"><![CDATA[<img src="/pub/media/logo/default/location-map.png" />]]></argument>
    <argument name="href" xsi:type="string">https://goo.gl/maps/XSKNf</argument>
  </arguments>
</block>

The xml above correctly renders a clickable link that goes to the proper location, but the img tag itself is rendered as text, not as html. Removing CDATA results in the argument not being parsed.

It's obvious I'm coming at this wrong, and given M2's relative newness, I've not been able to find an example that shows a way.

Is what I am asking possible?

NOTE ** I am aware that I could just create a template file to accomplish this, but I'm asking if there is another method that would work. I would like to avoid creating unnecessary templates, if possible, just to render a single line of static html.

1 Answer 1

1

If you read the code of Magento\Framework\View\Element\Html\Link carefully you will notice the label is escaped :

protected function _toHtml()
{
   if (false != $this->getTemplate()) {
      return parent::_toHtml();
   }

   return '
  • getLinkAttributes() . ' >' . $this->escapeHtml($this->getLabel()) . '
  • '; }

    But you can also notice that you can set a template that should solve the problem. The phtml should be something like :

    <li><a <?php $block->getLinkAttributes(); ?>> <?php echo $block->getLabel(); ?></a></li>

    The label is not escaped here.

    2
    • Right, I know I can set a template. I'm updating my question to clarify. I'm asking if there is a way to accomplish this without creating a template file for what amounts to a single line of html. Commented Jul 5, 2016 at 15:09
    • As the escapeHtml call is not conditional, I would say no Commented Jul 5, 2016 at 15:22

    Your Answer

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

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