diff --git a/Model/Source/AbstractAttributes.php b/Model/Source/AbstractAttributes.php new file mode 100644 index 0000000..bc39e0e --- /dev/null +++ b/Model/Source/AbstractAttributes.php @@ -0,0 +1,123 @@ +entityType = $entityType; + $this->attrColFactory = $attrColFactory; + + $this->entityType->loadByCode( + \Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE + ); + } + + /** + * All price attributes of Product entity + * + * @return array + */ + public function getAllOptions() + { + if ($this->options === null) { + $col = $this->attrColFactory->create(); + + $keyEntityType = \Magento\Eav\Model\Entity\Attribute\Set::KEY_ENTITY_TYPE_ID; + $col->addFieldToFilter($keyEntityType, $this->entityType->getId()); + + //Filter Type is NOT In Array + if (!empty($this->filterTypesNotEqual)) { + $filter = ['nin' => $this->filterTypesNotEqual]; + $col->addFieldToFilter('main_table.frontend_input', $filter); + } + + //Filter Type IS In Array + if (!empty($this->filterTypesEqual)) { + $filter = ['in' => $this->filterTypesEqual]; + $col->addFieldToFilter('main_table.frontend_input', $filter); + } + + if ($this->flatOnly) { + $col->addFieldToFilter('used_in_product_listing', 1); + } + + $col->setOrder('frontend_label', 'ASC'); + $col = $this->additionalFilter($col); + + $attrAll = $col->load()->getItems(); + + $this->options = []; + + if ($this->showEmpty) { + $this->options[] = [ + 'label' => __('No usage'), + 'value' => 0 + ]; + } + + // Loop over all attributes + foreach ($attrAll as $attr) { + $label = $attr->getStoreLabel() ?? $attr->getFrontendLabel(); + if ('' != $label) { + $this->options[] = ['label' => $label, 'value' => $attr->getAttributeCode()]; + } + } + } + + return $this->options; + } + + protected function additionalFilter($collection) + { + return $collection; + } + + public function toOptionArray() + { + return $this->getAllOptions(); + } +} diff --git a/Model/Source/Attributes.php b/Model/Source/Attributes.php index 64086ce..ac44559 100644 --- a/Model/Source/Attributes.php +++ b/Model/Source/Attributes.php @@ -7,55 +7,14 @@ namespace Mygento\Base\Model\Source; -use Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection; - -class Attributes implements \Magento\Framework\Option\ArrayInterface +class Attributes extends AbstractAttributes { - - /** - * Possible payment types - * - * @SuppressWarnings(PHPMD) - * @return array - */ - public function getAllOptions() - { - $obMan = \Magento\Framework\App\ObjectManager::getInstance(); - - $coll = $obMan->create(Collection::class); - - $coll->addFieldToFilter(\Magento\Eav\Model\Entity\Attribute\Set::KEY_ENTITY_TYPE_ID, 4); - - $coll->addFieldToFilter('main_table.frontend_input', ['neq' => 'hidden']); - $coll->addFieldToFilter('main_table.frontend_input', ['neq' => 'multiselect']); - $coll->addFieldToFilter('main_table.frontend_input', ['neq' => 'boolean']); - $coll->addFieldToFilter('main_table.frontend_input', ['neq' => 'date']); - $coll->addFieldToFilter('main_table.frontend_input', ['neq' => 'image']); - $coll->addFieldToFilter('main_table.frontend_input', ['neq' => 'price']); - $coll->addFieldToFilter('used_in_product_listing', '1'); - $coll->setOrder('frontend_label', 'ASC'); - - $attrAll = $coll->load()->getItems(); - - $_options = []; - - $_options[] = [ - 'label' => __('No usage'), - 'value' => 0 - ]; - - // Loop over all attributes - foreach ($attrAll as $attr) { - $label = $attr->getStoreLabel() ? $attr->getStoreLabel() : $attr->getFrontendLabel(); - if ('' != $label) { - $_options[] = ['label' => $label, 'value' => $attr->getAttributeCode()]; - } - } - return $_options; - } - - public function toOptionArray() - { - return $this->getAllOptions(); - } + protected $filterTypesNotEqual = [ + 'hidden', + 'multiselect', + 'boolean', + 'date', + 'image', + 'price' + ]; } diff --git a/composer.json b/composer.json index 2620d61..58c6cdc 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "mygento/base", "type": "magento2-module", - "version": "2.2.5", + "version": "2.2.6", "license": "OSL-3.0", "homepage": "https://github.com/mygento/base", "description": "Mygento Base", diff --git a/etc/module.xml b/etc/module.xml index bd6f5cd..cb5d6f0 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -7,5 +7,5 @@ */ --> - +