Skip to content

Commit

Permalink
Refactored _getMultiSelectAttributeWithSourceModels to use repo getLi…
Browse files Browse the repository at this point in the history
…st as opposed to direct query
  • Loading branch information
rain2o committed Nov 19, 2018
1 parent f506bf3 commit 028a15c
Showing 1 changed file with 33 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Magento\Catalog\Model\Product\Attribute\Source\Status as ProductStatus;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\Data\ProductAttributeInterface;

/**
* Catalog Product Eav Select and Multiply Select Attributes Indexer resource model
Expand All @@ -29,6 +30,16 @@ class Source extends AbstractEav
*/
protected $_attributeRepository;

/**
* @var \Magento\Framework\Api\SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;

/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;

/**
* Construct
*
Expand All @@ -38,6 +49,8 @@ class Source extends AbstractEav
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper
* @param \Magento\Eav\Api\AttributeRepositoryInterface $attributeRepository
* @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
* @param \Psr\Log\LoggerInterface $logger
* @param null|string $connectionName
*/
public function __construct(
Expand All @@ -47,6 +60,8 @@ public function __construct(
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Catalog\Model\ResourceModel\Helper $resourceHelper,
\Magento\Eav\Api\AttributeRepositoryInterface $attributeRepository,
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
\Psr\Log\LoggerInterface $logger,
$connectionName = null
) {
parent::__construct(
Expand All @@ -58,6 +73,8 @@ public function __construct(
);
$this->_resourceHelper = $resourceHelper;
$this->_attributeRepository = $attributeRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->logger = $logger;
}

/**
Expand Down Expand Up @@ -310,39 +327,31 @@ protected function _prepareMultiselectIndex($entityIds = null, $attributeId = nu

/**
* Get options for multiselect attributes using custom source models
* @maderlock's fix from: https://github.com/magento/magento2/issues/417#issuecomment-265146285
* Based on @maderlock's fix from:
* https://github.com/magento/magento2/issues/417#issuecomment-265146285
*
* @param array $attrIds
* @param array $options
*
* @return array
* @throws \Zend_Db_Statement_Exception
*/
protected function _getMultiSelectAttributeWithSourceModels($attrIds, $options)
{
// Add options from custom source models
$select = $this->getConnection()->select()
->from(
['ea' => $this->getTable('eav_attribute')],
['attribute_id','entity_type_id', 'attribute_code']
)
->where('attribute_id IN(?)', $attrIds)
->where('source_model is not null');
$query = $select->query();

while ($row = $query->fetch()) {
try {
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */
$attribute = $this->_attributeRepository->get($row['entity_type_id'], $row['attribute_code']);
$sourceModelOptions = $attribute->getOptions();
// Add options to list used below
foreach ($sourceModelOptions as $o) {
$options[$row['attribute_id']][$o->getValue()] = true;
}
} catch (\BadMethodCallException $e) {
// Skip
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
// skip
$this->searchCriteriaBuilder
->addFilter('attribute_id', $attrIds, 'in')
->addFilter('source_model', true, 'notnull');
$criteria = $this->searchCriteriaBuilder->create();
$attributes = $this->_attributeRepository->getList(
ProductAttributeInterface::ENTITY_TYPE_CODE,
$criteria
)->getItems();

foreach ($attributes as $attribute) {
$sourceModelOptions = $attribute->getOptions();
// Add options to list used below
foreach ($sourceModelOptions as $o) {
$options[$attribute->getAttributeId()][$o->getValue()] = true;
}
}

Expand Down

0 comments on commit 028a15c

Please sign in to comment.