0

I have a controller which I want to use to load scripts via ajax however whenever I add scripts to the block within the layout page type all script tags seem to be stripped out.

Is there a way I can load scripts into this controller like this?

Controller

<?php
namespace Harrigo\GDPR\Controller\Index;

use Magento\Framework\Controller\ResultFactory;

class Tags extends \Magento\Framework\App\Action\Action
{
    protected $resultPageFactory;

    /**
     * Constructor
     * 
     * @param \Magento\Framework\App\Action\Context  $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    )
    {
        $this->resultPageFactory = $resultPageFactory;
        $this->_resultFactory = $context->getResultFactory();
        parent::__construct($context);
    }

    /**
     * Execute view action
     * 
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        $resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
        return $resultLayout;   
    }
}

xml

<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
    <container name="root" label="Root">
        <block class="Magento\Framework\View\Element\Template" name="tagblock" template="Harrigo_GDPR::scriptblock.phtml" />
    </container>
</layout>

tagblock.phtml

<?php echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('beforebodyend')->toHtml();?>
TEST
<script type="text/javascript">
alert("hello world");
</script>
0

So I couldn't figure this out but would like to as have had difficulties with certain blocks that I add into layouts include both HTML and JS and so do not function as would expect and this could potentially be the cause.

I found however that using a JSON controller and passing the scripts via json to be more appropriate and am posting this as a workaround for if anyone needs.

JSON Controller:

<?php
namespace Harrigo\GDPR\Controller\Index;

class Tags extends \Magento\Framework\App\Action\Action
{
    protected $resultJsonFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
        \Magento\Cms\Model\Template\FilterProvider $filterProvider,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Cms\Model\BlockFactory $blockFactory,
        array $data = []
    )
    {
        $this->resultJsonFactory = $resultJsonFactory;
        $this->_filterProvider = $filterProvider;
        $this->_storeManager = $storeManager;
        $this->_blockFactory = $blockFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $blockId = 'beforebodyend';
        $storeId = $this->_storeManager->getStore()->getId();
        $block = $this->_blockFactory->create();
        $block->setStoreId($storeId)->load($blockId);
        $html = $this->_filterProvider->getBlockFilter()->setStoreId($storeId)->filter($block->getContent());
        $result = $this->resultJsonFactory->create();
        $result->setData(['tags' => $html]);
        return $result;  
    }
}

Calling the Controller:

<div class="non-essential-tag-wrapper">
</div>
<script>
require(['jquery', 'jquery/jquery.cookie'], function($){ 
    if ($.cookie('user_allowed_save_cookie')) {
        $.getJSON("/tags/index/tags", function(result){
            $(".non-essential-tag-wrapper").html(result["tags"]);
        });
    }
});
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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