0

I am trying to override a date calendar class in Magento. But when I try doing it the date input from the product page disappears. Basic I want to override the date calendar script to add custom settings to it. I added a hidden span to see if my changes get out.

c3m\custommodule\Model\Date

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace c3m\custommodule\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();
    }
}
?>

c3m\custommodule\etc\di

<?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="c3m\custommodule\Model\Date"/>
</config>

You can see at the picture I added that the calendar input is gone. enter image description here

I have set developer mode on and some errors came when i reloaded the page:

2 exception(s):
Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid block type: Magento\Framework\View\Element\Html\Date
Exception #1 (ReflectionException): Class c3m\custommodule\Model\Date does not exist

Tried making a new plugin just with first letter uppercase instead of small like C3m\Custommodule but its still saying class does not exist.

0

Your custom class must extend,

Magento\Framework\View\Element\Html\Date

Instead of

\Magento\Framework\View\Element\Template

| improve this answer | |
  • Hello @aravind tried your changes, the calendar input is still missing for some reason. you can see my changes at first post. – Lasse Bang Mar 14 at 16:01
  • Did you get any errors in logs? or browser console? – aravind Mar 16 at 4:31
  • i cleaned the logs and no errors was found when i reloaded the page, but a bit later there was two errors: main.CRITICAL: Class c3m\custommodule\Model\Date does not exist [] [] and main.CRITICAL: Invalid block type: Magento\Framework\View\Element\Html\Date [] [] – Lasse Bang Mar 16 at 14:06

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.