I want to do some changes in
vendor/magento/framework/Stdlib/DateTime/Date.php
How can I override the file in app folder?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI want to do some changes in
vendor/magento/framework/Stdlib/DateTime/Date.php
How can I override the file in app folder?
I don't prefer to use <preference></preference>
for this work.
If you want some modification at any public method of any class then I suggest use Plugin instead of preference
.
<preference>
means class override where <plugin>
means method function/method override.
Step 1: Create di.xml at{YourVendorNAME}/{ModuleName}/etc/di.xml.This is the declaration of the plugin.
<?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="Magento\Framework\Stdlib\DateTime\Date">
<plugin name="DateTime_plugin" type="{YourVendorNAME}\{ModuleName}\Plugin\DatePlugin" sortOrder="10" disabled="false" />
</type>
</config>
Step2: Create DatePlugin.php
at {YourVendorNAME}/{ModuleName}/Plugin
,
<?php
namespace {YourVendorNAME}\{ModuleName}\Plugin;
class DatePlugin
{
public function beforeDate(
\Magento\Framework\Stdlib\DateTime\Date $subject,
$format = 'Y-m-d H:i:s',
$input = null
){
return [$format,$input];
}
}
Here, i have created a before plugin on \Magento\Framework\Stdlib\DateTime\Date::date() function .
Add below code to di.xml
<preference for="Magento\Framework\Stdlib\DateTime\Date" type="Vendor\Module\Rewrite\Magento\Framework\Stdlib\DateTime\Date"/>
and create class for Vendor\Module\Rewrite\Magento\Framework\Stdlib\DateTime\Date and extend the \Magento\Framework\Stdlib\DateTime\Date
Hope it will work for you.