0

I want do do changes in the following file:

\vendor\magento\module-cookie\Block\Html\Notices.php

How can I stop that file from being overwritten when doing upgrades in the future? Can I override it in some way, e.g. move it to the theme structure?

Thanks!

1
  • you can directly not change this file you can just overwritten in your custom create extension Commented Jan 30, 2019 at 6:29

1 Answer 1

2

Create your custom module and add rewrite code in your custom module, please try following way.

Create custom module Vendor/Module

/app/code/Vendor/Module

|_ composer.json

{
    "name": "vendor/module-module",
    "description": "",
    "type": "magento2-module",
    "license": "proprietary",
    "authors": [
        {
            "name": "TestModule",
            "email": "[email protected]"
        }
    ],
    "minimum-stability": "dev",
    "require": {},
    "autoload": {
        "psr-4": {
            "Vendor\\Module\\": ""
        },
        "files": [
            "registration.php"
        ]
    }
}

|_registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

|_ 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="Vendor_Module" setup_version="1.0.0"/>
</config>

|_ etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="Vendor\Module\Block\Html\Notices" type="Magento\Cookie\Block\Html\Notices" />
</config>

|_ Block/Html/Notices.php (Please add your overrited code in this file )

<?php

/**
 * Frontend form key content block
 */
namespace Vendor\Module\Block\Html;

class Notices extends \Magento\Cookie\Block\Html\Notices
{

    public function getPrivacyPolicyLink()
    {
        return $this->_urlBuilder->getUrl('privacy-policy-cookie-restriction-mode');
    }
}

Please try this way and let me know if any issue.

1
  • I did exactly like this, just changed the Vendor\Module to my namespace, and changed "privacy-policy-cookie-restriction-mode" to "privacy-policy" in the Notices.php file but it still shows the link to "privacy-policy-cookie-restriction-mode" Commented Nov 20, 2019 at 6:32

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.