у меня есть компиляция с использованием терминала в Ubuntu, но я обнаружил ошибку «\ Magento \ Framework \ Stdlib \ DateTime \ TimezoneInterface уже существует в объекте контекста» в «magento 2».
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 уже существует в контексте», чем я не знаю, как получить объект TimezoneInterface и как использовать его для большего количества ссылок и получить больше данных объекта.
Я прокомментировал строку // ~ \Magento\Framework\Stdlib\DateTime\TimezoneInterface $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;
}
}
Других решений пока нет …