Skip to content

Magento 2 – after Plugin for MagentoCspModelPolicyFetchPolicy not working

Why is my plugin not getting called?

app/code/Company/Configurator/etc/module.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Company_Configurator" setup_version="0.1.0">
        <sequence>
            <module name="Magento_Quote"/>
            <module name="Magento_Csp"/>
        </sequence>
    </module>
</config>

app/code/Company/Configurator/etc/di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="MagentoCspModelPolicyFetchPolicy">
        <plugin name="company_configurator_after_fetch_policy"
                type="CompanyConfiguratorPluginFetchPolicyPlugin" />
    </type>
</config>

app/code/Company/Configurator/Plugin/FetchPolicyPlugin.php:

<?php
namespace CompanyConfiguratorPlugin;

class FetchPolicyPlugin
{
    /**
     * @param MagentoCspModelPolicyFetchPolicy $subject
     * @param string[] $result
     * @return string[]
     */
    public function afterGetHostSources(MagentoCspModelPolicyFetchPolicy $subject, array $result): array
    {
        // Modify the $result array here, if needed
        // For example, add an additional host source
        $result[] = 'example.com';  // I have set a xdebug breakpoint here, but it is never hit.

        return $result;
    }
}

The original method is getting called, but not my plugin.