Skip to content

Instantly share code, notes, and snippets.

@med-amin
Created October 2, 2019 09:39
Show Gist options
  • Save med-amin/83cc1aab1053ded1bff02896db0f7e90 to your computer and use it in GitHub Desktop.
Save med-amin/83cc1aab1053ded1bff02896db0f7e90 to your computer and use it in GitHub Desktop.
<?php
namespace TestModule\TestDate\Model\Config\Source;
use Magento\Framework\App\ObjectManager;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
class TestDate extends AbstractSource
{
/**
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory
*/
protected $_attrOptionCollectionFactory;
/**
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory
*/
protected $_attrOptionFactory;
/**
* Default values for option cache
*
* @var array
*/
protected $_optionsDefault = [];
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory
* @codeCoverageIgnore
*/
public function __construct(
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory,
\Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory
) {
$this->_attrOptionCollectionFactory = $attrOptionCollectionFactory;
$this->_attrOptionFactory = $attrOptionFactory;
}
/**
* Retrieve Full Option values array
*
* @param bool $withEmpty Add empty option to array
* @param bool $defaultValues
* @return array
*/
public function getAllOptions($withEmpty = true, $defaultValues = false)
{
$storeId = $this->getAttribute()->getStoreId();
if ($storeId === null) {
$storeId = $this->getStoreManager()->getStore()->getId();
}
if (!is_array($this->_options)) {
$this->_options = [];
}
if (!is_array($this->_optionsDefault)) {
$this->_optionsDefault = [];
}
$attributeId = $this->getAttribute()->getId();
if (!isset($this->_options[$storeId][$attributeId])) {
$date = date('Y-m-d') . ' 00:00:00';
$collection = $this->_attrOptionCollectionFactory->create()->setPositionOrder(
'asc'
)->setAttributeFilter(
$attributeId
)->setStoreFilter(
$storeId
)->addFieldToFilter(
'tdv.value', ['gt' => $date]
)->load();
$this->_options[$storeId][$attributeId] = $collection->toOptionArray();
$this->_optionsDefault[$storeId][$attributeId] = $collection->toOptionArray('default_value');
}
$options = $defaultValues
? $this->_optionsDefault[$storeId][$attributeId]
: $this->_options[$storeId][$attributeId];
return $options;
}
/**
* Get StoreManager dependency
*
* @return StoreManagerInterface
* @deprecated 100.1.6
*/
private function getStoreManager()
{
if ($this->storeManager === null) {
$this->storeManager = ObjectManager::getInstance()->get(StoreManagerInterface::class);
}
return $this->storeManager;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment