Magento 2.2.2 Custom theme Xml update external script with query string & or &
-
08-01-2021 - |
Question
Adding external link in custom theme in any xml e.g: contact_index_index.xml doe not parse properly if it has & or &
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" src_type="url" />
Notice the &
gives:
{"0":"Warning: simplexml_load_string(): Entity: line 6371: parser error : EntityRef: expecting ';' in \/var\/www\/vhosts\/magento2 \/htdocs\/vendor\/magento\/framework\/View\/Model\/Layout\/Merge.php
Does anyone experiencing this too and how to fix it?
Thanks.
Solution
The only way that I found it works is by using head.aditional:
e.g
<referenceBlock name="head.additional">
<block class="Magento\Framework\View\Element\Text" name="my_script">
<action method="setText">
<argument translate="true" name="text" xsi:type="string">
<![CDATA[<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" src_type="url" />]]>
</argument>
</action>
</block>
</referenceBlock>
OTHER TIPS
Try this one :
<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" src_type="url"/>
</head>
</page>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange