How to override vendor/magento/framework/View/Element/Html/Date.php?
Please help me out with this.
Solved! Go to Solution.
Okay, you can override by following approach :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Vendor\Module\Model;
//namespace Magento\Framework\View\Element\Html;
/**
* Date element block
*/
class Date extends \Magento\Framework\View\Element\Html\Date
{
/**
* Render block HTML
*
* @return string
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function _toHtml()
{
$html = '<input type="text" name="' . $this->getName() . '" id="' . $this->getId() . '" ';
$html .= 'value="' . $this->escapeHtml($this->getValue()) . '" ';
$html .= 'class="' . $this->getClass() . '" ' . $this->getExtraParams() . '/> ';
$calendarYearsRange = $this->getYearsRange();
$changeMonth = $this->getChangeMonth();
$changeYear = $this->getChangeYear();
$maxDate = $this->getMaxDate();
$showOn = $this->getShowOn();
$firstDay = $this->getFirstDay();
$html .= '<span style="display:none;">123</span><script type="text/javascript">
require(["jquery", "mage/calendar"], function($){
$("#' .
$this->getId() .
'").calendar({
showsTime: ' .
($this->getTimeFormat() ? 'true' : 'false') .
',
' .
($this->getTimeFormat() ? 'timeFormat: "' .
$this->getTimeFormat() .
'",' : '') .
'
dateFormat: "' .
$this->getDateFormat() .
'",
buttonImage: "' .
$this->getImage() .
'",
' .
($calendarYearsRange ? 'yearRange: "' .
$calendarYearsRange .
'",' : '') .
'
buttonText: "' .
(string)new \Magento\Framework\Phrase(
'Select Date'
) .
'"' . ($maxDate ? ', maxDate: "' . $maxDate . '"' : '') .
($changeMonth === null ? '' : ', changeMonth: ' . $changeMonth) .
($changeYear === null ? '' : ', changeYear: ' . $changeYear) .
($showOn ? ', showOn: "' . $showOn . '"' : '') .
($firstDay ? ', firstDay: ' . $firstDay : '') .
'})
});
</script>';
return $html;
}
/**
* Convert special characters to HTML entities
*
* @return string
*/
public function getEscapedValue()
{
if ($this->getFormat() && $this->getValue()) {
return strftime($this->getFormat(), strtotime($this->getValue()));
}
return $this->escapeHtml($this->getValue());
}
/**
* Produce and return block's html output
*
* {@inheritdoc}
*
* @return string
*/
public function getHtml()
{
return $this->toHtml();
}
}
?>
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Framework\View\Element\Html\Date" type="Vendor\Module\Model\Date"/>
</config>
Copy code from your core class and paste in above class file.Hope it helps !
Yes, you should create a new module, please find a reference below :
https://devdocs.magento.com/videos/fundamentals/create-a-new-module/
Hello @waqassadafa462
You can take this reference :
https://magento.stackexchange.com/questions/99845/disable-previous-days-in-default-magento2-calendar
But this will overrride all the calendar in Magento scope, suppose you want to change the catalog sale from past sale or make a product available from past time, you will not longer able to change it after adding once.
better you can change as per required page.
Hope it helps !
Hallo, there is a calendar function in date.php. i have written minDate: new Date() over there then i am able to disable past dates. But when magento will update then edited past will be no longer. So, i have to override that date.php file. I have a main javascript file when i write that function there but it doesn't work so i have no other option. Please help me out with this
Okay, you can override by following approach :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Vendor\Module\Model;
//namespace Magento\Framework\View\Element\Html;
/**
* Date element block
*/
class Date extends \Magento\Framework\View\Element\Html\Date
{
/**
* Render block HTML
*
* @return string
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function _toHtml()
{
$html = '<input type="text" name="' . $this->getName() . '" id="' . $this->getId() . '" ';
$html .= 'value="' . $this->escapeHtml($this->getValue()) . '" ';
$html .= 'class="' . $this->getClass() . '" ' . $this->getExtraParams() . '/> ';
$calendarYearsRange = $this->getYearsRange();
$changeMonth = $this->getChangeMonth();
$changeYear = $this->getChangeYear();
$maxDate = $this->getMaxDate();
$showOn = $this->getShowOn();
$firstDay = $this->getFirstDay();
$html .= '<span style="display:none;">123</span><script type="text/javascript">
require(["jquery", "mage/calendar"], function($){
$("#' .
$this->getId() .
'").calendar({
showsTime: ' .
($this->getTimeFormat() ? 'true' : 'false') .
',
' .
($this->getTimeFormat() ? 'timeFormat: "' .
$this->getTimeFormat() .
'",' : '') .
'
dateFormat: "' .
$this->getDateFormat() .
'",
buttonImage: "' .
$this->getImage() .
'",
' .
($calendarYearsRange ? 'yearRange: "' .
$calendarYearsRange .
'",' : '') .
'
buttonText: "' .
(string)new \Magento\Framework\Phrase(
'Select Date'
) .
'"' . ($maxDate ? ', maxDate: "' . $maxDate . '"' : '') .
($changeMonth === null ? '' : ', changeMonth: ' . $changeMonth) .
($changeYear === null ? '' : ', changeYear: ' . $changeYear) .
($showOn ? ', showOn: "' . $showOn . '"' : '') .
($firstDay ? ', firstDay: ' . $firstDay : '') .
'})
});
</script>';
return $html;
}
/**
* Convert special characters to HTML entities
*
* @return string
*/
public function getEscapedValue()
{
if ($this->getFormat() && $this->getValue()) {
return strftime($this->getFormat(), strtotime($this->getValue()));
}
return $this->escapeHtml($this->getValue());
}
/**
* Produce and return block's html output
*
* {@inheritdoc}
*
* @return string
*/
public function getHtml()
{
return $this->toHtml();
}
}
?>
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Framework\View\Element\Html\Date" type="Vendor\Module\Model\Date"/>
</config>
Copy code from your core class and paste in above class file.Hope it helps !
Run below commands after that :
php bin/magento setup:di:compile php bin/magento c:f
Should i create a module folder and then new folder modal and create date.php there ? I have a folder in app with the name of cars
Yes, you should create a new module, please find a reference below :
https://devdocs.magento.com/videos/fundamentals/create-a-new-module/