Skip to content

Commit

Permalink
New abstract attribute source model (#7)
Browse files Browse the repository at this point in the history
* new abstract attribute source model

* bump 2.2.6
  • Loading branch information
luckyraul committed Dec 29, 2017
1 parent 7f6c52d commit a0f6010
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 52 deletions.
123 changes: 123 additions & 0 deletions Model/Source/AbstractAttributes.php
@@ -0,0 +1,123 @@
<?php
/**
* @author Mygento
* @copyright See COPYING.txt for license details.
* @package Mygento_Base
*/

namespace Mygento\Base\Model\Source;

abstract class AbstractAttributes implements \Magento\Framework\Option\ArrayInterface
{
/**
* @var array
*/
protected $options;

/**
* @var bool
*/
protected $showEmpty = true;

/**
* @var bool
*/
protected $flatOnly = true;

/**
* @var array
*/
protected $filterTypesNotEqual = [];

/**
* @var array
*/
protected $filterTypesEqual = [];

/**
* @var \Magento\Eav\Model\Entity\Type
*/
protected $entityType;

/**
* @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory
*/
protected $attrCollFactory;

public function __construct(
\Magento\Eav\Model\Entity\Type $entityType,
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attrColFactory
) {
$this->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();
}
}
59 changes: 9 additions & 50 deletions Model/Source/Attributes.php
Expand Up @@ -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'
];
}
2 changes: 1 addition & 1 deletion 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",
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Expand Up @@ -7,5 +7,5 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Mygento_Base" setup_version="2.2.5"></module>
<module name="Mygento_Base" setup_version="2.2.6"></module>
</config>

0 comments on commit a0f6010

Please sign in to comment.