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!

  • you can directly not change this file you can just overwritten in your custom create extension – Rakesh Donga Jan 30 at 6:29
1

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": "info@test.com"
        }
    ],
    "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.

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.