\Magento\Framework\Stdlib\DateTime\TimezoneInterface 上下文对象 magento 2 中已存在

\Magento\Framework\Stdlib\DateTime\TimezoneInterface already exists in context object magento 2

我在 ubuntu 中使用终端编译,但我在 "magento 2" 中发现错误“\Magento\Framework\Stdlib\DateTime\TimezoneInterface 已存在于上下文对象中”。

namespace Perception\Bannerslider\Block;

class SliderItem extends \Magento\Framework\View\Element\Template {

    /**
     * Date conversion model
     * @var \Magento\Framework\Stdlib\DateTime\DateTime
     */
    protected $_stdlibDateTime;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * slider factory
     * @var \Perception\Bannerslider\Model\SliderFactory
     */
    protected $_sliderFactory;

    /**
     * slider model
     * @var \Perception\Bannerslider\Model\Slider
     */
    protected $_slider;

    /**
     * slider id
     * @var int
     */
    protected $_sliderId;

    /**
     * category Factory
     * @var \Magento\Catalog\Model\CategoryFactory
     */
    protected $_categoryFactory;

    /**
     * banner slider helper
     * @var \Perception\Bannerslider\Helper\Data
     */
    protected $_bannersliderHelper;

    /**
     *
     * @var \Perception\Bannerslider\Model\Resource\Banner\CollectionFactory
     */
    protected $_bannerCollectionFactory;

    protected $_scopeConfig;

    public $timezone;

    /**
     * [__construct description]
     * @param \Magento\Framework\View\Element\Template\Context                $context                 [description]
     * @param \Perception\Bannerslider\Model\Resource\Banner\CollectionFactory $bannerCollectionFactory [description]
     * @param \Perception\Bannerslider\Model\SliderFactory                     $sliderFactory           [description]
     * @param \Perception\Bannerslider\Model\Slider                            $slider                  [description]
     * @param \Magento\Framework\Stdlib\DateTime\DateTime                     $stdlibDateTime          [description]
     * @param \Perception\Bannerslider\Helper\Data                             $bannersliderHelper      [description]
     * @param \Magento\Catalog\Model\CategoryFactory                          $categoryFactory         [description]
     * @param \Magento\Framework\ObjectFactory                                $objectFactory           [description]
     * @param \Magento\Store\Model\StoreManagerInterface                      $storeManager            [description]
     * @param array                                                           $data                    [description]
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Perception\Bannerslider\Model\Resource\Banner\CollectionFactory $bannerCollectionFactory,
        \Perception\Bannerslider\Model\SliderFactory $sliderFactory,
        \Perception\Bannerslider\Model\Slider $slider,
        \Magento\Framework\Stdlib\DateTime\DateTime $stdlibDateTime,
        \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
        \Perception\Bannerslider\Helper\Data $bannersliderHelper,
        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
        \Magento\Framework\DataObject $objectFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_sliderFactory = $sliderFactory;
        $this->_slider = $slider;
        $this->_stdlibDateTime = $stdlibDateTime;
        $this->_bannersliderHelper = $bannersliderHelper;
        $this->timezone = $timezone;
        $this->_storeManager = $storeManager;
        $this->_bannerCollectionFactory = $bannerCollectionFactory;
        $this->_categoryFactory = $categoryFactory;
        $this->_scopeConfig = $context->getScopeConfig();
    }

    /**
     * @return
     */
    protected function _toHtml() {
        $store = $this->_storeManager->getStore()->getId();
        $configEnable = $this->_scopeConfig->getValue('bannerslider/general/enable_frontend', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
        if (!$configEnable || $this->_slider->getStatus() === '2' || ($this->_slider->getStyleContent() != 2 && $this->getBannerCollection()->getFirstItem()->getId() == null)) {
            return '';
        }
        return parent::_toHtml();
    }

    /**
     * Add elements in layout
     * @return
     */
    protected function _prepareLayout() {
        return parent::_prepareLayout();
    }

    /**
     * set slider Id and set template
     * @param int $sliderId
     */
    public function setSliderId($sliderId) {
        $this->_sliderId = $sliderId;

        $slider = $this->_sliderFactory->create()->load($this->_sliderId);
        if ($slider->getId()) {
            $this->setSlider($slider);

            if ($slider->getStyleContent() == 2) {
                $this->setTemplate('Perception_Bannerslider::slider/custom.phtml');
            } else {
                switch ($slider->getStyleSlide()) {
                    //Evolution slide
                    case 1:case 2:case 3:case 4:
                        $this->setTemplate('Perception_Bannerslider::slider/evolution.phtml');
                        break;

                    case 5:
                        $this->setTemplate('Perception_Bannerslider::slider/popup.phtml');
                        break;
                    //Note all page
                    case 6:
                        $this->setTemplate('Perception_Bannerslider::slider/special/note.phtml');
                        break;

                    // Flex slide
                    default:
                        $this->setTemplate('Perception_Bannerslider::slider/flexslider.phtml');
                        break;
                }
            }
        }

        return $this;
    }

    public function isShowTitle() {
        return $this->_slider->getShowTitle() == 1 ? true : false;
    }

    /**
     * get banner collection of slider
     * @return \Perception\Bannerslider\Model\Resource\Banner\Collection
     */
    public function getBannerCollection() {
        $storeViewId = $this->_storeManager->getStore()->getId();
        $bannerCollection = $this->_bannerCollectionFactory->create()
                                 ->setStoreViewId($storeViewId)
                                 ->addFieldToFilter('slider_id', $this->_slider->getId())
                                 ->addFieldToFilter('status', 1)
                                 ->setOrder('order_banner', "ASC");
        $bannerCollection->getSelect()->having('start_time < NOW() and end_time > NOW()');
        if ($this->_slider->getSortType() == 1) {
            $bannerCollection->getSelect()->orderRand('main_table.banner_id');
        }
        // $bannerCollection->getSelect()->columns(array($randomise ? 'Rand() as order' : ''));
        return $bannerCollection;
    }

    public function getToday() {
        return $this->_stdlibDateTime->gmtDate();
    }
    // /**
    //  * get slider id
    //  * @return int
    //  */
    // public function getSliderId() {
    //  return $this->_sliderId;
    // }

    public function getPositionNote() {
        switch ($this->_slider->getPositionNote()) {
            case 1:return "top-left";
            case 2:return "middle-top";
            case 3:return "top-right";
            case 4:return "middle-left";
            case 5:return "middle-right";
            case 6:return "bottom-left";
            case 7:return "middle-bottom";
            default:
                return "bottom-right";
        }
    }

    /**
     * set slider model
     * @param \Perception\Bannerslider\Model\Slider $slider [description]
     */
    public function setSlider(\Perception\Bannerslider\Model\Slider $slider) {
        $this->_slider = $slider;
        return $this;
    }

    /**
     * [getSlider description]
     * @return [type] [description]
     */
    public function getSlider() {
        return $this->_slider;
    }

    public function getBannerImageUrl(\Perception\Bannerslider\Model\Banner $banner) {
        return $this->_bannersliderHelper->getBaseUrlMedia($banner->getImage());
    }

    public function getBannerTarget($banner) {
        switch ($banner->getTarget()) {
            case 0:
                return '_self';
            case 1:
                return '_parent';

            default:
                return '_blank';
        }
    }

    public function getStdlibDateTime() {
        return $this->_stdlibDateTime;
    }
}

请给我建议如何解决此错误,如果 "TimezoneInterface already exists in context" 我不知道如何获取 TimezoneInterface 对象以及如何将其用于更多引用和获取更多对象数据。

我在 //~ \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone 行评论过,文件执行不需要对象 $timezone。现在错误消失了。

<?php

namespace Perception\Bannerslider\Block;

class SliderItem extends \Magento\Framework\View\Element\Template {
    /**
     * Date conversion model
     * @var \Magento\Framework\Stdlib\DateTime\DateTime
     */
    protected $_stdlibDateTime;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * slider factory
     * @var \Perception\Bannerslider\Model\SliderFactory
     */
    protected $_sliderFactory;

    /**
     * slider model
     * @var \Perception\Bannerslider\Model\Slider
     */
    protected $_slider;

    /**
     * slider id
     * @var int
     */
    protected $_sliderId;

    /**
     * category Factory
     * @var \Magento\Catalog\Model\CategoryFactory
     */
    protected $_categoryFactory;

    /**
     * banner slider helper
     * @var \Perception\Bannerslider\Helper\Data
     */
    protected $_bannersliderHelper;

    /**
     *
     * @var \Perception\Bannerslider\Model\Resource\Banner\CollectionFactory
     */
    protected $_bannerCollectionFactory;

    protected $_scopeConfig;

    public $timezone;

    /**
     * [__construct description]
     * @param \Magento\Framework\View\Element\Template\Context                $context                 [description]
     * @param \Perception\Bannerslider\Model\Resource\Banner\CollectionFactory $bannerCollectionFactory [description]
     * @param \Perception\Bannerslider\Model\SliderFactory                     $sliderFactory           [description]
     * @param \Perception\Bannerslider\Model\Slider                            $slider                  [description]
     * @param \Magento\Framework\Stdlib\DateTime\DateTime                     $stdlibDateTime          [description]
     * @param \Perception\Bannerslider\Helper\Data                             $bannersliderHelper      [description]
     * @param \Magento\Catalog\Model\CategoryFactory                          $categoryFactory         [description]
     * @param \Magento\Framework\ObjectFactory                                $objectFactory           [description]
     * @param \Magento\Store\Model\StoreManagerInterface                      $storeManager            [description]
     * @param array                                                           $data                    [description]
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Perception\Bannerslider\Model\Resource\Banner\CollectionFactory $bannerCollectionFactory,
        \Perception\Bannerslider\Model\SliderFactory $sliderFactory,
        \Perception\Bannerslider\Model\Slider $slider,
        \Magento\Framework\Stdlib\DateTime\DateTime $stdlibDateTime,
        //~ \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
        \Perception\Bannerslider\Helper\Data $bannersliderHelper,
        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
        \Magento\Framework\DataObject $objectFactory,
         //~ \Magento\Store\Model\StoreManagerInterface $storeManager,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_sliderFactory = $sliderFactory;
        $this->_slider = $slider;
        $this->_stdlibDateTime = $stdlibDateTime;
        $this->_bannersliderHelper = $bannersliderHelper;
        //~ $this->timezone = $timezone;
        //~ $this->_storeManager = $storeManager;
        $this->_bannerCollectionFactory = $bannerCollectionFactory;
        $this->_categoryFactory = $categoryFactory;
        $this->_scopeConfig = $context->getScopeConfig();

    }

    /**
     * @return
     */
    protected function _toHtml() {

        $store = $this->_storeManager->getStore()->getId();
        $configEnable = $this->_scopeConfig->getValue('bannerslider/general/enable_frontend', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
        if (!$configEnable || $this->_slider->getStatus() === '2' || ($this->_slider->getStyleContent() != 2 && $this->getBannerCollection()->getFirstItem()->getId() == null)) {
            return '';
        }
        return parent::_toHtml();
    }

    /**
     * Add elements in layout
     * @return
     */
    protected function _prepareLayout() {
        return parent::_prepareLayout();
    }

    /**
     * set slider Id and set template
     * @param int $sliderId
     */
    public function setSliderId($sliderId) {
        $this->_sliderId = $sliderId;

        $slider = $this->_sliderFactory->create()->load($this->_sliderId);
        if ($slider->getId()) {
            $this->setSlider($slider);

            if ($slider->getStyleContent() == 2) {
                $this->setTemplate('Perception_Bannerslider::slider/custom.phtml');
            } else {
                switch ($slider->getStyleSlide()) {
                    //Evolution slide
                    case 1:case 2:case 3:case 4:
                        $this->setTemplate('Perception_Bannerslider::slider/evolution.phtml');
                        break;

                    case 5:
                        $this->setTemplate('Perception_Bannerslider::slider/popup.phtml');
                        break;
                    //Note all page
                    case 6:
                        $this->setTemplate('Perception_Bannerslider::slider/special/note.phtml');
                        break;

                    // Flex slide
                    default:
                        $this->setTemplate('Perception_Bannerslider::slider/flexslider.phtml');
                        break;
                }
            }
        }

        return $this;
    }

    public function isShowTitle() {
        return $this->_slider->getShowTitle() == 1 ? true : false;
    }

    /**
     * get banner collection of slider
     * @return \Perception\Bannerslider\Model\Resource\Banner\Collection
     */
    public function getBannerCollection() {
        $storeViewId = $this->_storeManager->getStore()->getId();
        $bannerCollection = $this->_bannerCollectionFactory->create()
                                 ->setStoreViewId($storeViewId)
                                 ->addFieldToFilter('slider_id', $this->_slider->getId())
                                 ->addFieldToFilter('status', 1)
                                 ->setOrder('order_banner', "ASC");
        $bannerCollection->getSelect()->having('start_time < NOW() and end_time > NOW()');
        if ($this->_slider->getSortType() == 1) {
            $bannerCollection->getSelect()->orderRand('main_table.banner_id');
        }
        // $bannerCollection->getSelect()->columns(array($randomise ? 'Rand() as order' : ''));
        return $bannerCollection;
    }

    public function getToday() {
        return $this->_stdlibDateTime->gmtDate();
    }
    // /**
    //  * get slider id
    //  * @return int
    //  */
    // public function getSliderId() {
    //  return $this->_sliderId;
    // }

    public function getPositionNote() {
        switch ($this->_slider->getPositionNote()) {
            case 1:return "top-left";
            case 2:return "middle-top";
            case 3:return "top-right";
            case 4:return "middle-left";
            case 5:return "middle-right";
            case 6:return "bottom-left";
            case 7:return "middle-bottom";
            default:
                return "bottom-right";
        }
    }

    /**
     * set slider model
     * @param \Perception\Bannerslider\Model\Slider $slider [description]
     */
    public function setSlider(\Perception\Bannerslider\Model\Slider $slider) {
        $this->_slider = $slider;
        return $this;
    }

    /**
     * [getSlider description]
     * @return [type] [description]
     */
    public function getSlider() {
        return $this->_slider;
    }

    public function getBannerImageUrl(\Perception\Bannerslider\Model\Banner $banner) {
        return $this->_bannersliderHelper->getBaseUrlMedia($banner->getImage());
    }

    public function getBannerTarget($banner) {
        switch ($banner->getTarget()) {
            case 0:
                return '_self';
            case 1:
                return '_parent';

            default:
                return '_blank';
        }
    }

    public function getStdlibDateTime() {
        return $this->_stdlibDateTime;
    }
}