Skip to content

Commit fa34b8f

Browse files
Merge pull request #1 from magebitcom/feature/cms-attributes
CMS attribute data provider/parser
2 parents 3d16114 + b347bab commit fa34b8f

File tree

10 files changed

+532
-5
lines changed

10 files changed

+532
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of the Magebit_StaticContentProcessor package
4+
*
5+
* DISCLAIMER
6+
*
7+
* Do not edit or add to this file if you wish to upgrade Magebit_StaticContentProcessor
8+
* to newer versions in the future.
9+
*
10+
* @copyright Copyright (c) 2021 Magebit (https://magebit.com/)
11+
* @author Kristofers Ozolins <[email protected]>
12+
* @license GNU General Public License ("GPL") v3.0
13+
*
14+
* For the full copyright and license information, please view the LICENSE
15+
* file that was distributed with this source code.
16+
*/
17+
namespace Magebit\StaticContentProcessor\Model\Config\Source\Category;
18+
19+
use Magento\Catalog\Model\ResourceModel\Category\Attribute\Collection;
20+
use Magento\Catalog\Model\ResourceModel\Category\Attribute\CollectionFactory;
21+
use Magento\Eav\Model\Entity\Attribute;
22+
use Magento\Framework\Option\ArrayInterface;
23+
24+
/**
25+
* Class AbstractAttributeSource
26+
*/
27+
abstract class AbstractAttributeSource implements ArrayInterface
28+
{
29+
/**
30+
* @var array|null
31+
*/
32+
private $options;
33+
34+
/**
35+
* @var CollectionFactory
36+
*/
37+
private $collectionFactory;
38+
39+
/**
40+
* Attributes constructor.
41+
*
42+
* @param CollectionFactory $collectionFactory
43+
*/
44+
public function __construct(CollectionFactory $collectionFactory)
45+
{
46+
$this->collectionFactory = $collectionFactory;
47+
}
48+
49+
/**
50+
* @inheritDoc
51+
*
52+
* @return array
53+
*/
54+
public function toOptionArray()
55+
{
56+
if (null === $this->options) {
57+
$this->options = [];
58+
59+
/** @var Collection $collection */
60+
$collection = $this->collectionFactory->create();
61+
$attributes = $collection->getItems();
62+
63+
/** @var Attribute $attribute */
64+
foreach ($attributes as $attribute) {
65+
if ($this->canAddAttribute($attribute)) {
66+
$label = sprintf(
67+
'%s (%s)',
68+
$attribute->getDefaultFrontendLabel(),
69+
$attribute->getAttributeCode()
70+
);
71+
$this->options[] = [
72+
'label' => $label,
73+
'value' => $attribute->getAttributeCode(),
74+
];
75+
}
76+
}
77+
}
78+
79+
return $this->options;
80+
}
81+
82+
/**
83+
* Validate if attribute can be shown
84+
*
85+
* @param Attribute $attribute
86+
*
87+
* @return bool
88+
*/
89+
abstract public function canAddAttribute(Attribute $attribute): bool;
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of the Magebit_StaticContentProcessor package
4+
*
5+
* DISCLAIMER
6+
*
7+
* Do not edit or add to this file if you wish to upgrade Magebit_StaticContentProcessor
8+
* to newer versions in the future.
9+
*
10+
* @copyright Copyright (c) 2021 Magebit (https://magebit.com/)
11+
* @author Kristofers Ozolins <[email protected]>
12+
* @license GNU General Public License ("GPL") v3.0
13+
*
14+
* For the full copyright and license information, please view the LICENSE
15+
* file that was distributed with this source code.
16+
*/
17+
namespace Magebit\StaticContentProcessor\Model\Config\Source\Category;
18+
19+
use Magento\Eav\Model\Entity\Attribute;
20+
21+
/**
22+
* Class Attributes
23+
*/
24+
class Attributes extends AbstractAttributeSource
25+
{
26+
/**
27+
*
28+
*/
29+
const RESTRICTED_ATTRIBUTES = [
30+
'all_children',
31+
'children',
32+
'children_count',
33+
'url_path',
34+
'url_key',
35+
'name',
36+
'is_active',
37+
'level',
38+
'path_in_store',
39+
'path',
40+
'position',
41+
];
42+
43+
/**
44+
* @inheritDoc
45+
*
46+
* @param Attribute $attribute
47+
*
48+
* @return bool
49+
*/
50+
public function canAddAttribute(Attribute $attribute): bool
51+
{
52+
return !in_array($attribute->getAttributeCode(), self::RESTRICTED_ATTRIBUTES);
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of the Magebit_StaticContentProcessor package
4+
*
5+
* DISCLAIMER
6+
*
7+
* Do not edit or add to this file if you wish to upgrade Magebit_StaticContentProcessor
8+
* to newer versions in the future.
9+
*
10+
* @copyright Copyright (c) 2021 Magebit (https://magebit.com/)
11+
* @author Kristofers Ozolins <[email protected]>
12+
* @license GNU General Public License ("GPL") v3.0
13+
*
14+
* For the full copyright and license information, please view the LICENSE
15+
* file that was distributed with this source code.
16+
*/
17+
namespace Magebit\StaticContentProcessor\Model\Config\Source\Product;
18+
19+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
20+
use Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection;
21+
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory;
22+
use Magento\Framework\Option\ArrayInterface;
23+
24+
/**
25+
* Class AbstractAttributeSource
26+
*/
27+
abstract class AbstractAttributeSource implements ArrayInterface
28+
{
29+
/**
30+
* @var array|null
31+
*/
32+
private $options;
33+
34+
/**
35+
* @var CollectionFactory
36+
*/
37+
private $collectionFactory;
38+
39+
/**
40+
* Attributes constructor.
41+
*
42+
* @param CollectionFactory $collectionFactory
43+
*/
44+
public function __construct(CollectionFactory $collectionFactory)
45+
{
46+
$this->collectionFactory = $collectionFactory;
47+
}
48+
49+
/**
50+
* @inheritDoc
51+
*
52+
* @return array
53+
*/
54+
public function toOptionArray()
55+
{
56+
if (null === $this->options) {
57+
$this->options = [];
58+
/** @var Collection $collection */
59+
$collection = $this->collectionFactory->create();
60+
$collection->addVisibleFilter();
61+
$attributes = $collection->getItems();
62+
63+
/** @var ProductAttributeInterface $attribute */
64+
foreach ($attributes as $attribute) {
65+
if ($this->canAddAttribute($attribute)) {
66+
$label = sprintf(
67+
'%s (%s)',
68+
$attribute->getDefaultFrontendLabel(),
69+
$attribute->getAttributeCode()
70+
);
71+
72+
$this->options[] = [
73+
'label' => $label,
74+
'value' => $attribute->getAttributeCode(),
75+
];
76+
}
77+
}
78+
}
79+
80+
return $this->options;
81+
}
82+
83+
/**
84+
* Validate if attribute can be shown
85+
*
86+
* @param ProductAttributeInterface $attribute
87+
*
88+
* @return bool
89+
*/
90+
abstract public function canAddAttribute(ProductAttributeInterface $attribute): bool;
91+
}
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of the Magebit_StaticContentProcessor package
4+
*
5+
* DISCLAIMER
6+
*
7+
* Do not edit or add to this file if you wish to upgrade Magebit_StaticContentProcessor
8+
* to newer versions in the future.
9+
*
10+
* @copyright Copyright (c) 2021 Magebit (https://magebit.com/)
11+
* @author Kristofers Ozolins <[email protected]>
12+
* @license GNU General Public License ("GPL") v3.0
13+
*
14+
* For the full copyright and license information, please view the LICENSE
15+
* file that was distributed with this source code.
16+
*/
17+
namespace Magebit\StaticContentProcessor\Model\Config\Source\Product;
18+
19+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
20+
21+
/**
22+
* Class Attributes
23+
*/
24+
class Attributes extends AbstractAttributeSource
25+
{
26+
/**
27+
*
28+
*/
29+
const GENERAL_RESTRICTED_ATTRIBUTES = [
30+
'sku',
31+
'url_path',
32+
'url_key',
33+
'name',
34+
'visibility',
35+
'status',
36+
'tier_price',
37+
'price',
38+
'price_type',
39+
'gallery',
40+
'status',
41+
'category_ids',
42+
'swatch_image',
43+
'quantity_and_stock_status',
44+
'options_container',
45+
];
46+
47+
/**
48+
* @inheritDoc
49+
*
50+
* @param ProductAttributeInterface $attribute
51+
*
52+
* @return bool
53+
*/
54+
public function canAddAttribute(ProductAttributeInterface $attribute): bool
55+
{
56+
return !in_array($attribute->getAttributeCode(), self::GENERAL_RESTRICTED_ATTRIBUTES);
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of the Magebit_StaticContentProcessor package
4+
*
5+
* DISCLAIMER
6+
*
7+
* Do not edit or add to this file if you wish to upgrade Magebit_StaticContentProcessor
8+
* to newer versions in the future.
9+
*
10+
* @copyright Copyright (c) 2021 Magebit (https://magebit.com/)
11+
* @author Kristofers Ozolins <[email protected]>
12+
* @license GNU General Public License ("GPL") v3.0
13+
*
14+
* For the full copyright and license information, please view the LICENSE
15+
* file that was distributed with this source code.
16+
*/
17+
18+
namespace Magebit\StaticContentProcessor\Model\Indexer\DataProvider\Category;
19+
20+
use Divante\VsbridgeIndexerCatalog\Model\Category\GetAttributeCodesByIds;
21+
use Magento\Store\Model\ScopeInterface;
22+
use Magento\Framework\App\Config\ScopeConfigInterface;
23+
use Divante\VsbridgeIndexerCms\Model\Indexer\DataProvider\CmsContentFilter;
24+
use Divante\VsbridgeIndexerCore\Api\DataProviderInterface;
25+
26+
/**
27+
* Class WysiwygBlockData
28+
*/
29+
class WysiwygBlockData implements DataProviderInterface
30+
{
31+
/**
32+
* @var CmsContentFilter
33+
*/
34+
private $cmsContentFilter;
35+
36+
/**
37+
* @var ScopeConfigInterface
38+
*/
39+
private $scopeConfig;
40+
41+
/**
42+
* ContentData constructor.
43+
*
44+
* @param CmsContentFilter $cmsContentFilter
45+
*/
46+
public function __construct(
47+
CmsContentFilter $cmsContentFilter,
48+
ScopeConfigInterface $scopeConfig,
49+
GetAttributeCodesByIds $getAttributeCodesByIds
50+
) {
51+
$this->cmsContentFilter = $cmsContentFilter;
52+
$this->scopeConfig = $scopeConfig;
53+
$this->getAttributeCodesByIds = $getAttributeCodesByIds;
54+
}
55+
56+
/**
57+
* @inheritdoc
58+
*/
59+
public function addData(array $indexData, $storeId)
60+
{
61+
$processable = [];
62+
63+
$cmsContentAttributes = $this->scopeConfig->getValue(
64+
'vsbridge_indexer_settings/url_rewrites/category_content_attributes',
65+
ScopeInterface::SCOPE_STORE
66+
);
67+
68+
if (is_string($cmsContentAttributes)) {
69+
$parseBlockIds = explode(',', $cmsContentAttributes);
70+
71+
foreach ($indexData as $categoryId => $categoryData) {
72+
foreach ($categoryData as $attributeCode => $attribute) {
73+
if (in_array($attributeCode, $parseBlockIds) && is_string($attribute)) {
74+
$processable[] = [
75+
'categoryId' => $categoryId,
76+
'attributeId' => $attributeCode,
77+
'content' => $attribute
78+
];
79+
}
80+
}
81+
}
82+
83+
$filteredIndexData = $this->cmsContentFilter->filter($processable, $storeId, 'block');
84+
85+
foreach ($filteredIndexData as $data) {
86+
$indexData[$data['categoryId']][$data['attributeId']] = $data['content'];
87+
}
88+
}
89+
90+
return $indexData;
91+
}
92+
}

0 commit comments

Comments
 (0)