Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/MC-19777' into 2.3-develop-pr32
Browse files Browse the repository at this point in the history
  • Loading branch information
serhii-balko committed Sep 16, 2019
2 parents 795aa94 + 1e6389c commit a6e5450
Showing 1 changed file with 56 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
* See COPYING.txt for license details.
*/

/**
* Attribute add/edit form options tab
*
* @author Magento Core Team <core@magentocommerce.com>
*/
namespace Magento\Eav\Block\Adminhtml\Attribute\Edit\Options;

use Magento\Store\Model\ResourceModel\Store\Collection;
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;

/**
* Attribute add/edit form options tab
*
* @api
* @since 100.0.2
*/
Expand Down Expand Up @@ -61,6 +59,7 @@ public function __construct(

/**
* Is true only for system attributes which use source model
*
* Option labels and position for such attributes are kept in source model and thus cannot be overridden
*
* @return bool
Expand Down Expand Up @@ -96,12 +95,16 @@ public function getStoresSortedBySortOrder()
{
$stores = $this->getStores();
if (is_array($stores)) {
usort($stores, function ($storeA, $storeB) {
if ($storeA->getSortOrder() == $storeB->getSortOrder()) {
return $storeA->getId() < $storeB->getId() ? -1 : 1;
usort(
$stores,
function ($storeA, $storeB) {
if ($storeA->getSortOrder() == $storeB->getSortOrder()) {
return $storeA->getId() < $storeB->getId() ? -1 : 1;
}

return ($storeA->getSortOrder() < $storeB->getSortOrder()) ? -1 : 1;
}
return ($storeA->getSortOrder() < $storeB->getSortOrder()) ? -1 : 1;
});
);
}
return $stores;
}
Expand Down Expand Up @@ -130,12 +133,14 @@ public function getOptionValues()
}

/**
* @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
* Preparing values of attribute options
*
* @param AbstractAttribute $attribute
* @param array|\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection $optionCollection
* @return array
*/
protected function _prepareOptionValues(
\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute,
AbstractAttribute $attribute,
$optionCollection
) {
$type = $attribute->getFrontendInput();
Expand All @@ -149,6 +154,41 @@ protected function _prepareOptionValues(

$values = [];
$isSystemAttribute = is_array($optionCollection);
if ($isSystemAttribute) {
$values = $this->getPreparedValues($optionCollection, $isSystemAttribute, $inputType, $defaultValues);
} else {
$optionCollection->setPageSize(200);
$pageCount = $optionCollection->getLastPageNumber();
$currentPage = 1;
while ($currentPage <= $pageCount) {
$optionCollection->clear();
$optionCollection->setCurPage($currentPage);
$values = array_merge(
$values,
$this->getPreparedValues($optionCollection, $isSystemAttribute, $inputType, $defaultValues)
);
$currentPage++;
}
}

return $values;
}

/**
* Return prepared values of system or user defined attribute options
*
* @param array|\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection $optionCollection
* @param bool $isSystemAttribute
* @param string $inputType
* @param array $defaultValues
*/
private function getPreparedValues(
$optionCollection,
bool $isSystemAttribute,
string $inputType,
array $defaultValues
) {
$values = [];
foreach ($optionCollection as $option) {
$bunch = $isSystemAttribute ? $this->_prepareSystemAttributeOptionValues(
$option,
Expand All @@ -169,12 +209,13 @@ protected function _prepareOptionValues(

/**
* Retrieve option values collection
*
* It is represented by an array in case of system attribute
*
* @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
* @param AbstractAttribute $attribute
* @return array|\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection
*/
protected function _getOptionValuesCollection(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute)
protected function _getOptionValuesCollection(AbstractAttribute $attribute)
{
if ($this->canManageOptionDefaultOnly()) {
$options = $this->_universalFactory->create(
Expand Down Expand Up @@ -226,7 +267,7 @@ protected function _prepareSystemAttributeOptionValues($option, $inputType, $def
foreach ($this->getStores() as $store) {
$storeId = $store->getId();
$value['store' . $storeId] = $storeId ==
\Magento\Store\Model\Store::DEFAULT_STORE_ID ? $valuePrefix . $this->escapeHtml($option['label']) : '';
\Magento\Store\Model\Store::DEFAULT_STORE_ID ? $valuePrefix . $this->escapeHtml($option['label']) : '';
}

return [$value];
Expand Down

0 comments on commit a6e5450

Please sign in to comment.