Skip to content

Commit 34d3643

Browse files
authored
Merge pull request #1 from ivanhrytsaim/Dont-Use-Lable-On-Some-Page
Dont Use Lable On Some Page
2 parents 8d80d3b + 83a4861 commit 34d3643

File tree

5 files changed

+153
-3
lines changed

5 files changed

+153
-3
lines changed

Model/Config.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Config
3333

3434
const XML_PATH_GENERAL_LIST_PAGE_CONTAINER_SELECTOR = 'mfproductlabel/general/pl_selector';
3535

36+
const XML_PATH_EXCLUDE_PAGE_TYPES = 'mfproductlabel/general/exclude_page_types';
3637
/**
3738
* Config constructor.
3839
* @param ScopeConfigInterface $scopeConfig
@@ -87,4 +88,11 @@ public function getConfig($path, $storeId = null)
8788
$storeId
8889
);
8990
}
91+
92+
public function getExcludePageTypes(): array
93+
{
94+
$pageTypes = $this->scopeConfig->getValue(self::XML_PATH_EXCLUDE_PAGE_TYPES);
95+
return explode(',', $pageTypes);
96+
}
97+
9098
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan (support@magefan.com). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magefan\ProductLabel\Model\Config\Backend;
10+
11+
use Magento\Framework\App\Config\Value as ConfigValue;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
use Magento\Framework\Model\Context;
14+
use Magento\Framework\Registry;
15+
use Magento\Framework\App\Cache\TypeListInterface;
16+
use Magento\Framework\Model\ResourceModel\AbstractResource;
17+
use Magento\Framework\Data\Collection\AbstractDb;
18+
use Magento\Framework\View\Layout\PageType\Config;
19+
20+
class ExcludePageType extends ConfigValue
21+
{
22+
/**
23+
* @var Config
24+
*/
25+
private $pageConfig;
26+
27+
/**
28+
* @param Context $context
29+
* @param Registry $registry
30+
* @param Config $pageConfig
31+
* @param ScopeConfigInterface $config
32+
* @param TypeListInterface $cacheTypeList
33+
* @param AbstractResource|null $resource
34+
* @param AbstractDb|null $resourceCollection
35+
* @param array $data
36+
*/
37+
public function __construct(
38+
Context $context,
39+
Registry $registry,
40+
Config $pageConfig,
41+
ScopeConfigInterface $config,
42+
TypeListInterface $cacheTypeList,
43+
AbstractResource $resource = null,
44+
AbstractDb $resourceCollection = null,
45+
array $data = []
46+
)
47+
{
48+
$this->pageConfig = $pageConfig;
49+
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
50+
}
51+
52+
/**
53+
* @return void
54+
*/
55+
protected function _afterLoad()
56+
{
57+
parent::_afterLoad();
58+
59+
if (!$this->getValue()) {
60+
$pageTypes = $this->pageConfig->getPageTypes();
61+
$excludePageType = ['cms_index_index', 'cms_page_view', 'contact_index_index', 'catalog_product_view', 'catalog_category_view'];
62+
63+
foreach ($excludePageType as $type) {
64+
unset($pageTypes[$type]);
65+
}
66+
$pageTypes = implode(',', array_keys($pageTypes));
67+
68+
$this->setValue($pageTypes);
69+
}
70+
}
71+
}

Model/Config/Source/PageType.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan (support@magefan.com). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magefan\ProductLabel\Model\Config\Source;
10+
11+
use Magento\Framework\Data\OptionSourceInterface;
12+
use Magento\Framework\View\Layout\PageType\Config;
13+
14+
class PageType implements OptionSourceInterface
15+
{
16+
/**
17+
* @var Config
18+
*/
19+
private $pageConfig;
20+
21+
/**
22+
* @var array
23+
*/
24+
private $options;
25+
26+
/**
27+
* @param Config $pageConfig
28+
*/
29+
public function __construct(
30+
Config $pageConfig
31+
) {
32+
$this->pageConfig = $pageConfig;
33+
}
34+
35+
/**
36+
* @return array
37+
*/
38+
public function toOptionArray(): array
39+
{
40+
if (!$this->options) {
41+
$pageTypes = array_keys($this->pageConfig->getPageTypes());
42+
sort($pageTypes);
43+
44+
foreach ($pageTypes as $action) {
45+
$label = explode('_', $action);
46+
$label = array_map('ucfirst', $label);
47+
48+
$this->options[] = [
49+
'value' => $action,
50+
'label' => implode(' · ', $label),
51+
];
52+
}
53+
}
54+
55+
return $this->options;
56+
}
57+
}

Plugin/Frontend/Magento/Framework/Controller/ResultInterface.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,25 @@ class ResultInterface
2424
*/
2525
protected $config;
2626

27+
/**
28+
* @var \Magento\Framework\App\RequestInterface
29+
*/
30+
private $request;
31+
2732
/**
2833
* @param Html $htmlParser
2934
* @param Config $config
3035
*/
3136
public function __construct(
3237
Html $htmlParser,
33-
Config $config
38+
Config $config,
39+
\Magento\Framework\App\RequestInterface $request,
3440
)
3541
{
3642
$this->htmlParser = $htmlParser;
3743
$this->config = $config;
44+
$this->request = $request;
45+
3846
}
3947

4048
/**
@@ -49,8 +57,9 @@ public function afterRenderResult(
4957
ResponseInterface $response
5058
) {
5159
$html = $response->getBody();
52-
53-
if ($this->config->isEnabled() && (false !== strpos($html, Html::COMMENT_PREFIX))) {
60+
if ($this->config->isEnabled() && (false !== strpos($html, Html::COMMENT_PREFIX))
61+
&& !in_array($this->request->getFullActionName(),$this->config->getExcludePageTypes()))
62+
{
5463
$response->setBody($this->htmlParser->execute($html));
5564
}
5665

etc/adminhtml/system.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
<label>Product List Label Container</label>
3636
<comment>Only one class that located above the product image!</comment>
3737
</field>
38+
<field id="exclude_page_types" translate="label" type="multiselect" sortOrder="20" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
39+
<label>Dont Use On Page</label>
40+
<backend_model>Magefan\ProductLabel\Model\Config\Backend\ExcludePageType</backend_model>
41+
<source_model>Magefan\ProductLabel\Model\Config\Source\PageType</source_model>
42+
</field>
3843
</group>
3944
</section>
4045
</system>

0 commit comments

Comments
 (0)