Skip to content

Commit 977786f

Browse files
committed
Add event list to config
1 parent ef0e6b0 commit 977786f

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). 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\GoogleTagManager\Block\Adminhtml\System\Config\Form;
10+
11+
use Magento\Backend\Block\Template\Context;
12+
use Magento\Config\Block\System\Config\Form\Field;
13+
use Magento\Framework\Data\Form\Element\AbstractElement;
14+
use Magento\Framework\Module\Manager;
15+
use Magento\Framework\View\Helper\SecureHtmlRenderer;
16+
17+
class EventList extends Field
18+
{
19+
public const EVENT_LIST_TEMPLATE = 'system/config/event/list.phtml';
20+
21+
/**
22+
* @var Manager
23+
*/
24+
private $moduleManager;
25+
26+
/**
27+
* EventList constructor.
28+
*
29+
* @param Context $context
30+
* @param Manager $moduleManager
31+
* @param array $data
32+
* @param SecureHtmlRenderer|null $secureRenderer
33+
*/
34+
public function __construct(
35+
Context $context,
36+
Manager $moduleManager,
37+
array $data = [],
38+
?SecureHtmlRenderer $secureRenderer = null
39+
) {
40+
$this->moduleManager = $moduleManager;
41+
parent::__construct($context, $data, $secureRenderer);
42+
}
43+
44+
/**
45+
* Set template to itself
46+
*
47+
* @return $this
48+
*/
49+
protected function _prepareLayout(): EventList
50+
{
51+
parent::_prepareLayout();
52+
if (!$this->getTemplate()) {
53+
$this->setTemplate(static::EVENT_LIST_TEMPLATE);
54+
}
55+
return $this;
56+
}
57+
58+
/**
59+
* Render event list
60+
*
61+
* @param AbstractElement $element
62+
* @return string
63+
*/
64+
public function render(AbstractElement $element): string
65+
{
66+
// Remove scope label
67+
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
68+
return parent::render($element);
69+
}
70+
71+
/**
72+
* Get the event list and scripts contents
73+
*
74+
* @param AbstractElement $element
75+
* @return string
76+
*/
77+
protected function _getElementHtml(AbstractElement $element): string
78+
{
79+
return $this->_toHtml();
80+
}
81+
82+
/**
83+
* Retrieve true if GTM Plus is enabled
84+
*
85+
* @return bool
86+
*/
87+
public function isPlusEnabled()
88+
{
89+
return (bool)$this->moduleManager->isEnabled('Magefan_GoogleTagManagerPlus');
90+
}
91+
}

etc/adminhtml/system.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@
6464
<field id="enable">1</field>
6565
</depends>
6666
</field>
67+
<field id="events" translate="label comment" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
68+
<label>Events</label>
69+
<frontend_model>Magefan\GoogleTagManager\Block\Adminhtml\System\Config\Form\EventList</frontend_model>
70+
</field>
6771
</group>
6872
<group id="attributes" translate="label" type="text" sortOrder="60" showInDefault="1" showInWebsite="1" showInStore="1">
6973
<label>Product Attributes Mapping</label>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
/** @var \Magefan\GoogleTagManager\Block\Adminhtml\System\Config\Form\EventList $block */
7+
/** @var \Magento\Framework\Escaper $escaper */
8+
?>
9+
<?php
10+
$events = [
11+
'View Product (view_item)',
12+
'View Shopping Cart (view_cart)',
13+
'View Checkout (begin_checkout)',
14+
'Checkout Success Page (purchase)'
15+
];
16+
$eventsPlus = [
17+
'Add To Cart (add_to_cart)',
18+
'Remove From Cart (remove_from_cart)',
19+
'Add To Wishlist (add_to_wishlist)',
20+
'View Product List on category page, related/upsale/crossale products (view_item_list)',
21+
'Shipping Step on Checkout Page (add_shipping_info)',
22+
'Payments Step on Checkout Page (add_payment_info)'
23+
];
24+
?>
25+
<br/>
26+
<div>
27+
<ul>
28+
<li>
29+
<?= $escaper->escapeHtml(implode('</li><li>', $events), ['li']) ?>
30+
</li>
31+
</ul>
32+
<p <?php if (!$block->isPlusEnabled()) { ?>style="color: red"<?php } ?>>
33+
<strong>
34+
<?= $escaper->escapeHtml(__(!$block->isPlusEnabled() ? 'Available in GTM Plus Only' : 'GTM Plus')) ?>
35+
<?php if (!$block->isPlusEnabled()) { ?>
36+
(<a href="https://magefan.com/magento-2-google-tag-manager/pricing" target="_blank">Read More</a>):
37+
<?php } ?>
38+
</strong>
39+
</p>
40+
<div <?php if (!$block->isPlusEnabled()) { ?>style="opacity: 0.7; color: red"<?php } ?>>
41+
<ul>
42+
<li>
43+
<?= $escaper->escapeHtml(implode('</li><li>', $eventsPlus), ['li']) ?>
44+
</li>
45+
</ul>
46+
</div>
47+
</div>

0 commit comments

Comments
 (0)