0

i want change the third party module helper class.

thrid party module helper class

<?php

namespace Amasty\Blog\Helper;

/**
 * Class
 */
class Date extends \Magento\Framework\App\Helper\AbstractHelper
{
    const DATE_TIME_PASSED = 'passed';

    const DATE_TIME_DIRECT = 'direct';

    /**
     * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
     */
    private $timezoneInterface;

    /**
     * @var \Magento\Framework\Locale\ResolverInterface
     */
    private $resolverInterface;

    /**
     * @var Settings
     */
    private $helperSettings;

    /**
     * @var \Magento\Framework\Stdlib\DateTime\DateTime
     */
    private $date;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\Locale\ResolverInterface $resolverInterface,
        \Amasty\Blog\Helper\Settings $helperSettings,
        \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezoneInterface,
        \Magento\Framework\Stdlib\DateTime\DateTime $date
    ) {
        parent::__construct($context);
        $this->timezoneInterface = $timezoneInterface;
        $this->resolverInterface = $resolverInterface;
        $this->helperSettings = $helperSettings;
        $this->date = $date;
    }

    /**
     * @param $datetime
     * @return string
     */
    public function renderTime($datetime)
    {
        $date = $this->timezoneInterface->formatDateTime(
            $datetime,
            \IntlDateFormatter::NONE,
            \IntlDateFormatter::SHORT,
            $this->resolverInterface->getLocale()
        );

        return $date;
    }

    /**
     * @param $date
     * @return bool
     */
    private function isToday($date)
    {
        $today = $nowDate = $this->date->gmtDate('Ymd');
        $day = $this->timezoneInterface->convertConfigTimeToUtc($date, 'Ymd');

        return $today == $day;
    }

    /**
     * @param $date
     * @return bool
     */
    private function isYesterday($date)
    {
        $today = $nowDate = $this->date->gmtDate('Ymd');
        $day = $this->timezoneInterface->convertConfigTimeToUtc($date, 'Ymd');

        return ($today - 1) == $day;
    }

    /**
     * @param $date
     * @return \Magento\Framework\Phrase
     */
    public function getHumanizedDate($date)
    {
        $nowDate = $this->date->gmtDate();
        $timestamp = $this->date->gmtTimestamp($nowDate) - $this->date->gmtTimestamp($date);

        if ($this->isToday($date) || ($timestamp <= 0)) {
            return __("Today");
        } elseif ($this->isYesterday($date)) {
            return __("Yesterday");
        } else {
            # Nice correction
            $days = round($timestamp / (3600 * 24));
            $months = round($timestamp / (3600 * 24 * 30));
            $years = round($timestamp / (3600 * 24 * 30 * 12));

            if ($days < 30) {
                if ($days == 1) {
                    return __("%1 day ago", $days);
                } else {
                    return __("%1 days ago", $days);
                }
            } elseif ($months < 12) {
                if ($months == 1) {
                    return __("%1 month ago", $months);
                } else {
                    return __("%1 months ago", $months);
                }
            } else {
                if ($years == 1) {
                    return __("%1 year ago", $years);
                } else {
                    return __("%1 years ago", $years);
                }
            }
        }
    }

    /**
     * @param $datetime
     * @param bool $forceDirect
     * @param bool $dateFormat
     * @return bool|\Magento\Framework\Phrase|string
     */
    public function renderDate($datetime, $forceDirect = false, $dateFormat = false)
    {
        $date = $this->timezoneInterface->formatDateTime(
            $datetime,
            \IntlDateFormatter::LONG,
            \IntlDateFormatter::NONE,
            $this->resolverInterface->getLocale()
        );

        if (!$dateFormat) {
            $dateFormat = $this->helperSettings->getDateFormat();
        }

        if ($forceDirect || ($dateFormat == self::DATE_TIME_DIRECT)) {
            return $date;
        } else {
            return $this->getHumanizedDate($datetime);
        }
    }
}

my module helper class ..

<?php

namespace Magemastery\Blog\Helper;

/**
 * Class
 */
class Date extends \Amasty\Blog\Helper\Date
{
    const DATE_TIME_PASSED = 'passed';

    const DATE_TIME_DIRECT = 'direct';

    /**
     * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
     */
    private $timezoneInterface;

    /**
     * @var \Magento\Framework\Locale\ResolverInterface
     */
    private $resolverInterface;

    /**
     * @var Settings
     */
    private $helperSettings;

    /**
     * @var \Magento\Framework\Stdlib\DateTime\DateTime
     */
    private $date;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\Locale\ResolverInterface $resolverInterface,
        \Amasty\Blog\Helper\Settings $helperSettings,
        \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezoneInterface,
        \Magento\Framework\Stdlib\DateTime\DateTime $date
    ) {
        parent::__construct($context,$timezoneInterface,$resolverInterface,$helperSettings,$date);
        
    }

    /**
     * @param $datetime
     * @return string
     */
    public function renderTime($datetime)
    {
        $date = $this->timezoneInterface->formatDateTime(
            $datetime,
            \IntlDateFormatter::NONE,
            \IntlDateFormatter::SHORT,
            $this->resolverInterface->getLocale()
        );

        return $date;
    }

    /**
     * @param $date
     * @return bool
     */
    private function isToday($date)
    {
        $today = $nowDate = $this->date->gmtDate('Ymd');
        $day = $this->timezoneInterface->convertConfigTimeToUtc($date, 'Ymd');

        return $today == $day;
    }

    /**
     * @param $date
     * @return bool
     */
    private function isYesterday($date)
    {
        $today = $nowDate = $this->date->gmtDate('Ymd');
        $day = $this->timezoneInterface->convertConfigTimeToUtc($date, 'Ymd');

        return ($today - 1) == $day;
    }

    /**
     * @param $date
     * @return \Magento\Framework\Phrase
     */
    public function getHumanizedDate($date)
    {
        $nowDate = $this->date->gmtDate();
        $timestamp = $this->date->gmtTimestamp($nowDate) - $this->date->gmtTimestamp($date);

        if ($this->isToday($date) || ($timestamp <= 0)) {
            return __("Today");
        } elseif ($this->isYesterday($date)) {
            return __("Yesterday");
        } else {
            # Nice correction
            $days = round($timestamp / (3600 * 24));
            $months = round($timestamp / (3600 * 24 * 30));
            $years = round($timestamp / (3600 * 24 * 30 * 12));

            if ($days < 30) {
                if ($days == 1) {
                    return __("%1 day ago", $days);
                } else {
                    return __("%1 days ago", $days);
                }
            } elseif ($months < 12) {
                if ($months == 1) {
                    return __("%1 month ago", $months);
                } else {
                    return __("%1 months ago", $months);
                }
            } else {
                if ($years == 1) {
                    return __("%1 year ago", $years);
                } else {
                    return __("%1 years ago", $years);
                }
            }
        }
    }

    /**
     * @param $datetime
     * @param bool $forceDirect
     * @param bool $dateFormat
     * @return bool|\Magento\Framework\Phrase|string
     */
    public function renderDate($datetime, $forceDirect = false, $dateFormat = false)
    {
        $date = $this->timezoneInterface->formatDateTime(
            $datetime,
            \IntlDateFormatter::LONG,
            \IntlDateFormatter::NONE,
            $this->resolverInterface->getLocale()
        );

        if (!$dateFormat) {
            $dateFormat = $this->helperSettings->getDateFormat();
        }

        if ($forceDirect || ($dateFormat == self::DATE_TIME_DIRECT)) {
            return $date;
        } else {
            return $this->getHumanizedDate($datetime);
        }
    }
}

my di.xml file

<?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="Amasty\Blog\Helper\Date" type="Magemastery\Blog\Helper\Date" />
</config>

when did above changes in my module helper class i getting this error.

Errors during compilation:Magemastery\Blog\Helper\Date Incompatible argument type: Required type: \Magento\Framework\Locale\ResolverInterface. Actual type: \Magento\Framework\Stdlib\DateTime\TimezoneInterface; File: 
/var/www/html/apetito-ltd/app/code/Magemastery/Blog/Helper/Date.php


Total Errors Count: 1

Can any one explain why this error is coming and how we can overcome it. How can i override the thrid party helper class in my module.

Thanks.

2 Answers 2

0

Change extends \Amasty\Blog\Helper\Data into extends \Amasty\Blog\Helper\Date

And did you already write your preference too in di.xml?

EDIT : You actually just took the parameter in the wrong order when you call the parent. Your second parameter is the timezone when it should be the resolver.

2
  • HI @Claims, i wrote di.xml file, i changed Data to Date. Agian facing some error, could you please look once. i updated my question.
    – Mani
    Aug 24, 2022 at 8:02
  • @Mani Check edit :) Btw if you are using ide like php storm constructor should be written automatically being auto generated so this kind of error should not happen.
    – Claims
    Aug 24, 2022 at 8:48
0

In Magemastery\Blog\Helper\Date try changing

parent::__construct($context,$timezoneInterface,$resolverInterface,$helperSettings,$date);

to

parent::__construct($context,$resolverInterface,$helperSettings,$timezoneInterface,$date);

Basically your order of arguments in child is not same as in parent class.

2
  • HI @Anshu Mishra, i changed the order, but getting this erros; Error: Call to a member function gmtDate() on null in /var/www/html/apetito-ltd/app/code/Magemastery/Blog/Helper/Date.php:91
    – Mani
    Aug 24, 2022 at 9:22
  • @Mani For your second issue $date is private which means when you try to access $this->date you probably get something undefined that's what i guess. May be you should reset in the construct the date as in the parent contruct or use a getter for that parameter if provided by the class but i doubt there is one.
    – Claims
    Aug 24, 2022 at 9:59

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.