Skip to content

Magento 2.4 How to override vendormagentoframeworkViewElementTemplateFile using a preference or a plugin in a custom module.?

How to override vendormagentoframeworkViewElementTemplateFileValidator.php using a preference or a plugin in a custom module.

Workout:

appcodeVendorEditCore1di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="magentoframeworkViewElementTemplateFileValidator" type="VendorEditCore1frameworkViewElementTemplateFileValidator" />
</config>

And my validator.php file path is,

Vendor Path : vendormagentoframeworkViewElementTemplateFileValidator.php

Custom Module Path: appcodeVendorEditCore1frameworkViewElementTemplateFileValidator.php

I am updating following code,

 /**protected function isPathInDirectories($path, $directories)
    {
        if (!is_array($directories)) {
            $directories = (array)$directories;
        }
        $realPath = $this->fileDriver->getRealPath($path);
        foreach ($directories as $directory) {
            if ($directory !== null && 0 === strpos($realPath, $directory)) {
                return true;
            }
        }
        return false;
    }*/
    protected function isPathInDirectories($path, $directories)
    {
        if (!is_array($directories)) {
            $directories = (array)$directories;
        }
        $realPath = str_replace('\', '/', $this->fileDriver->getRealPath($path)); //This Line Especially
        foreach ($directories as $directory) {
            if (0 === strpos($realPath, $directory)) {
                return true;
            }
        }
        return false;
    }

I am getting following error,

1 exception(s):
Exception #0 (MagentoFrameworkExceptionValidatorException): Invalid template file: 'C:/xampp/htdocs/magento/vendor/magento/module-theme/view/frontend/templates/page/js/require_js.phtml' in module: '' block's name: 'require.js'

Exception #0 (MagentoFrameworkExceptionValidatorException): Invalid template file: 'C:/xampp/htdocs/magento/vendor/magento/module-theme/view/frontend/templates/page/js/require_js.phtml' in module: '' block's name: 'require.js'
<pre>#1 MagentoFrameworkViewElementTemplate->_toHtml() called at [vendormagentoframeworkViewElementAbstractBlock.php:1128]
#2 MagentoFrameworkViewElementAbstractBlock->MagentoFrameworkViewElement{closure}() called at [vendormagentoframeworkViewElementAbstractBlock.php:1132]
#3 MagentoFrameworkViewElementAbstractBlock->_loadCache() called at [vendormagentoframeworkViewElementAbstractBlock.php:676]
#4 MagentoFrameworkViewElementAbstractBlock->toHtml() called at [vendormagentoframeworkViewResultPage.php:251]
#5 MagentoFrameworkViewResultPage->render() called at [vendormagentoframeworkViewResultLayout.php:171]
#6 MagentoFrameworkViewResultLayout->renderResult() called at [vendormagentoframeworkInterceptionInterceptor.php:58]
#7 MagentoFrameworkViewResultPageInterceptor->___callParent() called at [vendormagentoframeworkInterceptionInterceptor.php:138]
#8 MagentoFrameworkViewResultPageInterceptor->MagentoFrameworkInterception{closure}() called at [vendormagentoframeworkInterceptionInterceptor.php:153]
#9 MagentoFrameworkViewResultPageInterceptor->___callPlugins() called at [generatedcodeMagentoFrameworkViewResultPageInterceptor.php:95]
#10 MagentoFrameworkViewResultPageInterceptor->renderResult() called at [vendormagentoframeworkAppHttp.php:120]
#11 MagentoFrameworkAppHttp->launch() called at [vendormagentoframeworkInterceptionInterceptor.php:58]
#12 MagentoFrameworkAppHttpInterceptor->___callParent() called at [vendormagentoframeworkInterceptionInterceptor.php:138]
#13 MagentoFrameworkAppHttpInterceptor->MagentoFrameworkInterception{closure}() called at [vendormagentomodule-application-performance-monitorPluginApplicationPerformanceMonitor.php:38]
#14 MagentoApplicationPerformanceMonitorPluginApplicationPerformanceMonitor->aroundLaunch() called at [vendormagentoframeworkInterceptionInterceptor.php:135]
#15 MagentoFrameworkAppHttpInterceptor->MagentoFrameworkInterception{closure}() called at [vendormagentoframeworkInterceptionInterceptor.php:153]
#16 MagentoFrameworkAppHttpInterceptor->___callPlugins() called at [generatedcodeMagentoFrameworkAppHttpInterceptor.php:23]
#17 MagentoFrameworkAppHttpInterceptor->launch() called at [vendormagentoframeworkAppBootstrap.php:264]
#18 MagentoFrameworkAppBootstrap->run() called at [index.php:31]
</pre>