2

I want to do some changes in

vendor/magento/framework/Stdlib/DateTime/Date.php

How can I override the file in app folder?

5
  • What is the purpose of your rewrite?
    – Amit Bera
    Aug 28, 2019 at 9:31
  • @AmitBera changed the script in that as per my requirements
    – Nafsss
    Aug 28, 2019 at 9:35
  • @AmitBera Can u say how to do that?
    – Nafsss
    Aug 28, 2019 at 10:09
  • You can try the plugin for it.
    – kunj
    Aug 29, 2019 at 5:27
  • @Nafisa May I know on which 2.3.* version you trying?
    – kunj
    Aug 29, 2019 at 5:35

2 Answers 2

2

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 .

0

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.

1
  • Can you tell me where is the validation for the datepicker is done. Where can i get the code?
    – Nafsss
    Aug 29, 2019 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.