Skip to content

Commit 6231667

Browse files
author
Ilan Parmentier
committed
Extract Initial Release
0 parents  commit 6231667

File tree

23 files changed

+2501
-0
lines changed

23 files changed

+2501
-0
lines changed

Block/Adminhtml/Set/Chooser.php

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
<?php
2+
/**
3+
* Artbambou SmileCustomEntityWidget Module
4+
*
5+
* @category Artbambou
6+
* @package Artbambou_ElasticsuiteStock
7+
* @author Ilan Parmentier
8+
*/
9+
declare(strict_types=1);
10+
11+
namespace Artbambou\SmileCustomEntityWidget\Block\Adminhtml\Set;
12+
13+
use Magento\Backend\Block\Template\Context;
14+
use Magento\Backend\Helper\Data;
15+
use Magento\Backend\Block\Widget\Grid\Extended;
16+
use Magento\Eav\Model\Config;
17+
use Smile\CustomEntity\Api\Data\CustomEntityAttributeInterface;
18+
19+
/**
20+
* Attribute Set Chooser
21+
*/
22+
class Chooser extends Extended
23+
{
24+
/**
25+
* @var \Magento\Eav\Model\Config
26+
*/
27+
private $eavConfig;
28+
29+
/**
30+
* @var \Magento\Eav\Api\AttributeSetRepositoryInterface
31+
*/
32+
protected $attributeSetRepository;
33+
34+
/**
35+
* @param Context $context
36+
* @param Data $backendHelper
37+
* @param Magento\Eav\Model\Config $eavConfig
38+
* @param Magento\Eav\Api\AttributeSetRepositoryInterface $attributeSetRepository
39+
* @param array $data
40+
*/
41+
public function __construct(
42+
Context $context,
43+
Data $backendHelper,
44+
\Magento\Eav\Model\Config $eavConfig,
45+
\Magento\Eav\Api\AttributeSetRepositoryInterface $attributeSetRepository,
46+
array $data = []
47+
) {
48+
$this->eavConfig = $eavConfig;
49+
$this->attributeSetRepository = $attributeSetRepository;
50+
51+
parent::__construct($context, $backendHelper, $data);
52+
}
53+
54+
/**
55+
* Block construction, prepare grid params
56+
*
57+
* @return void
58+
*/
59+
protected function _construct()
60+
{
61+
parent::_construct();
62+
$this->setDefaultSort('attribute_set_id');
63+
$this->setDefaultDir('DESC');
64+
$this->setUseAjax(true);
65+
$this->setDefaultFilter(['chooser_is_active' => '1']);
66+
}
67+
68+
/**
69+
* Prepare chooser element HTML
70+
*
71+
* @param \Magento\Framework\Data\Form\Element\AbstractElement $element Form Element
72+
* @return \Magento\Framework\Data\Form\Element\AbstractElement
73+
*/
74+
public function prepareElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
75+
{
76+
$uniqId = $this->mathRandom->getUniqueHash($element->getId());
77+
$sourceUrl = $this->getUrl('custom_entity_widget/set/chooser', ['uniq_id' => $uniqId]);
78+
79+
$chooser = $this->getLayout()->createBlock(
80+
\Magento\Widget\Block\Adminhtml\Widget\Chooser::class
81+
)->setElement(
82+
$element
83+
)->setConfig(
84+
$this->getConfig()
85+
)->setFieldsetId(
86+
$this->getFieldsetId()
87+
)->setSourceUrl(
88+
$sourceUrl
89+
)->setUniqId(
90+
$uniqId
91+
);
92+
93+
if ($element->getValue()) {
94+
$attributeSet = $this->attributeSetRepository->get((int) $element->getValue());
95+
if ($attributeSet->getId()) {
96+
$chooser->setLabel($this->escapeHtml($attributeSet->getAttributeSetName()));
97+
}
98+
}
99+
100+
$element->setData('after_element_html', $chooser->toHtml());
101+
return $element;
102+
}
103+
104+
/**
105+
* Grid Row JS Callback
106+
*
107+
* @return string
108+
*/
109+
public function getRowClickCallback()
110+
{
111+
$chooserJsObject = $this->getId();
112+
113+
$js = '
114+
function (grid, event) {
115+
116+
var trElement = Event.findElement(event, "tr");
117+
var customEntitySetTitle = trElement.down("td").next().innerHTML;
118+
var customEntitySetId = trElement.down("td").innerHTML.replace(/^\s+|\s+$/g,"");
119+
120+
' . $chooserJsObject . '.setElementValue(customEntitySetId);
121+
' . $chooserJsObject . '.setElementLabel(customEntitySetTitle);
122+
' . $chooserJsObject . '.close();
123+
}
124+
';
125+
return $js;
126+
}
127+
128+
/**
129+
* Prepare pages collection
130+
*
131+
* @return \Magento\Backend\Block\Widget\Grid\Extended
132+
*/
133+
protected function _prepareCollection()
134+
{
135+
$entityTypeId = CustomEntityAttributeInterface::ENTITY_TYPE_CODE;
136+
$entityType = $this->eavConfig->getEntityType($entityTypeId);
137+
$this->setCollection($entityType->getAttributeSetCollection());
138+
return parent::_prepareCollection();
139+
}
140+
141+
/**
142+
* Prepare columns for slider grid
143+
*
144+
* @return $this
145+
*/
146+
protected function _prepareColumns()
147+
{
148+
$this->addColumn(
149+
'chooser_id',
150+
[
151+
'header' => __('ID'),
152+
'index' => 'attribute_set_id',
153+
'header_css_class' => 'col-id',
154+
'column_css_class' => 'col-id'
155+
]
156+
);
157+
158+
$this->addColumn(
159+
'chooser_title',
160+
[
161+
'header' => __('Title'),
162+
'index' => 'attribute_set_name',
163+
'header_css_class' => 'col-title',
164+
'column_css_class' => 'col-title'
165+
]
166+
);
167+
168+
/**
169+
* Check is single store mode
170+
*/
171+
if (!$this->_storeManager->isSingleStoreMode()) {
172+
$this->addColumn(
173+
'store_id',
174+
[
175+
'header' => __('Store View'),
176+
'index' => 'store_id',
177+
'type' => 'store',
178+
'store_all' => true,
179+
'store_view' => true,
180+
'sortable' => false,
181+
'filter_condition_callback' => [$this, '_filterStoreCondition']
182+
]
183+
);
184+
}
185+
186+
return parent::_prepareColumns();
187+
}
188+
189+
/**
190+
* Filter store condition
191+
*
192+
* @param \Magento\Framework\Data\Collection $collection
193+
* @param \Magento\Framework\DataObject $column
194+
* @return void
195+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
196+
*/
197+
protected function _filterStoreCondition($collection, \Magento\Framework\DataObject $column)
198+
{
199+
if (!($value = $column->getFilter()->getValue())) {
200+
return;
201+
}
202+
203+
$this->getCollection()->addStoreFilter($value);
204+
}
205+
206+
/**
207+
* Get grid url
208+
*
209+
* @return string
210+
*/
211+
public function getGridUrl()
212+
{
213+
return $this->getUrl(
214+
'custom_entity_widget/set/chooser',
215+
[
216+
'_current' => true,
217+
'uniq_id' => $this->getId()
218+
]
219+
);
220+
}
221+
}

Block/Set/Widget/Conditions.php

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<?php
2+
/**
3+
* Artbambou SmileCustomEntityWidget Module
4+
*
5+
* @category Artbambou
6+
* @package Artbambou_SmileCustomEntityWidget
7+
* @author Ilan Parmentier
8+
*/
9+
declare(strict_types=1);
10+
11+
namespace Artbambou\SmileCustomEntityWidget\Block\Set\Widget;
12+
13+
use Magento\Backend\Block\Template;
14+
use Magento\Backend\Block\Template\Context;
15+
use Magento\Framework\Registry;
16+
use Magento\Framework\Data\Form\Element\AbstractElement;
17+
use Magento\Framework\Data\Form\Element\Factory as ElementFactory;
18+
use Magento\Framework\Data\Form\Element\Renderer\RendererInterface;
19+
use Magento\Rule\Block\Conditions as RuleConditions; // Alias for clarity
20+
use Artbambou\SmileCustomEntityWidget\Model\Rule;
21+
22+
/**
23+
* Entity Chooser for Smile Custom Entity
24+
*/
25+
class Conditions extends Template implements RendererInterface
26+
{
27+
/**
28+
* Use the standard Magento conditions template
29+
*
30+
* @var string
31+
*/
32+
protected $_template = 'Artbambou_SmileCustomEntityWidget::widget/conditions.phtml';
33+
34+
/**
35+
* @var RuleConditions
36+
*/
37+
protected $conditions;
38+
39+
/**
40+
* @var Rule
41+
*/
42+
protected $rule;
43+
44+
/**
45+
* @var ElementFactory
46+
*/
47+
protected $elementFactory;
48+
49+
/**
50+
* @var Registry
51+
*/
52+
protected $registry;
53+
54+
/**
55+
* The element being rendered by this block
56+
*
57+
* @var AbstractElement
58+
*/
59+
protected $element;
60+
61+
/**
62+
* Internal element used to render conditions input
63+
*
64+
* @var \Magento\Framework\Data\Form\Element\Text
65+
*/
66+
protected $input;
67+
68+
/**
69+
* @param Context $context
70+
* @param ElementFactory $elementFactory
71+
* @param RuleConditions $conditions // Use alias
72+
* @param Rule $rule
73+
* @param Registry $registry
74+
* @param array $data
75+
*/
76+
public function __construct(
77+
Context $context,
78+
ElementFactory $elementFactory,
79+
RuleConditions $conditions,
80+
Rule $rule,
81+
Registry $registry,
82+
array $data = []
83+
) {
84+
$this->elementFactory = $elementFactory;
85+
$this->conditions = $conditions;
86+
$this->rule = $rule;
87+
$this->registry = $registry;
88+
parent::__construct($context, $data);
89+
}
90+
91+
/**
92+
* {@inheritdoc}
93+
*/
94+
protected function _construct()
95+
{
96+
$widgetParameters = [];
97+
$widget = $this->registry->registry('current_widget_instance');
98+
if ($widget) {
99+
$widgetParameters = $widget->getWidgetParameters();
100+
} elseif ($widgetOptions = $this->getLayout()->getBlock('wysiwyg_widget.options')) {
101+
$widgetParameters = $widgetOptions->getWidgetValues();
102+
}
103+
104+
if (isset($widgetParameters['conditions'])) {
105+
$this->rule->loadPost($widgetParameters);
106+
}
107+
}
108+
109+
/**
110+
* {@inheritdoc}
111+
*/
112+
public function render(AbstractElement $element)
113+
{
114+
$this->element = $element;
115+
$this->rule->getConditions()->setJsFormObject($this->getHtmlId());
116+
return $this->toHtml();
117+
}
118+
119+
/**
120+
* @return string
121+
*/
122+
public function getNewChildUrl()
123+
{
124+
return $this->getUrl(
125+
'custom_entity_widget/set_widget/conditions',
126+
['form' => $this->getHtmlId()]
127+
);
128+
}
129+
130+
/**
131+
* Get the element this renderer is attached to.
132+
*
133+
* @return AbstractElement
134+
*/
135+
public function getElement()
136+
{
137+
return $this->element;
138+
}
139+
140+
/**
141+
* Get the HTML ID for the conditions container.
142+
*
143+
* @return string
144+
*/
145+
public function getHtmlId()
146+
{
147+
return $this->getElement()->getContainer()->getHtmlId();
148+
}
149+
150+
/**
151+
* Generate the HTML for the conditions input element.
152+
*
153+
* @return string
154+
*/
155+
public function getInputHtml()
156+
{
157+
if (!$this->input) {
158+
$this->input = $this->elementFactory->create('text');
159+
$this->input->setRule($this->rule)
160+
->setRenderer($this->conditions);
161+
162+
if ($this->getElement() && $this->getElement()->getForm()) {
163+
$this->input->setForm($this->getElement()->getForm());
164+
}
165+
}
166+
return $this->input->toHtml();
167+
}
168+
}

0 commit comments

Comments
 (0)