Skip to content

Commit 6d52536

Browse files
author
Ilan Parmentier
committed
This commit introduces several enhancements to the Custom Entity layered navigation module and fixes critical toolbar bugs.
- Add a dedicated "Custom Entity" section in Store Configuration with its own settings - Create a new "Layered Navigation" fieldset with "Display Product Count" toggle option - Implement a ViewModel pattern for layered navigation filter rendering - Update templates to conditionally display product counts in filters - Fix toolbar mode switching not being applied correctly - Resolve parameters mismatch between toolbar model and JavaScript widget - Fix JSON corruption in toolbar widget options - Correct the URL parameter handling for sorting and filtering - Fix inconsistent behavior in the `getCurrentMode()` method - Ensure proper collection sorting when applying toolbar parameters to SQL queries - Refactor SetList helper to use Custom Entity-specific configuration values - Fix JavaScript initialization for entity list toolbar - Standardize parameter naming and handling across components These changes improve the module's independence from Catalog configuration and provide better customization options for the layered navigation experience with Custom Entities.
1 parent 2829328 commit 6d52536

File tree

28 files changed

+556
-181
lines changed

28 files changed

+556
-181
lines changed

Block/Navigation.php

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,24 @@ public function getLayer(): Layer
109109
}
110110

111111
/**
112-
* Check availability display layer block
112+
* Get all layer filters
113113
*
114-
* @return bool
114+
* @return array
115115
*/
116-
public function canShowBlock(): bool
116+
public function getFilters()
117117
{
118-
return $this->getLayer()->getCurrentAttributeSet()
119-
&& $this->visibilityFlag->isEnabled($this->getLayer(), $this->getFilters());
118+
return $this->filterList->getFilters($this->_entityLayer);
120119
}
121120

122121
/**
123-
* Get all layer filters
122+
* Check availability display layer block
124123
*
125-
* @return array
124+
* @return bool
126125
*/
127-
public function getFilters()
126+
public function canShowBlock(): bool
128127
{
129-
return $this->filterList->getFilters($this->_entityLayer);
128+
return $this->getLayer()->getCurrentAttributeSet()
129+
&& $this->visibilityFlag->isEnabled($this->getLayer(), $this->getFilters());
130130
}
131131

132132
/**
@@ -154,15 +154,4 @@ private function configureToolbarBlock(): void
154154
$toolbarBlock->setCollection($collection);
155155
}
156156
}
157-
158-
/**
159-
* Get block identities
160-
* Relies on the Layer model to provide state-dependent cache tags.
161-
*
162-
* @return string[]
163-
*/
164-
public function getIdentities(): array
165-
{
166-
return $this->getLayer()->getStateTags();
167-
}
168157
}

Block/Set/View.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,45 @@
1414
use Smile\CustomEntity\Block\Set\View as SmileCustomEntityView;
1515
use Amadeco\SmileCustomEntityLayeredNavigation\Model\Set\SetList\Toolbar as ToolbarModel;
1616

17+
use Magento\Eav\Api\Data\AttributeSetInterface;
18+
use Magento\Framework\Api\SearchCriteriaBuilder;
19+
use Magento\Framework\Api\SearchCriteriaBuilderFactory;
20+
use Magento\Framework\DataObject\IdentityInterface;
21+
use Magento\Framework\Exception\LocalizedException;
22+
use Magento\Framework\Exception\NoSuchEntityException;
23+
use Magento\Framework\Registry;
24+
use Magento\Framework\View\Element\RendererList;
25+
use Magento\Framework\View\Element\Template;
26+
use Smile\CustomEntity\Api\CustomEntityRepositoryInterface;
27+
use Smile\CustomEntity\Api\Data\CustomEntityInterface;
28+
use Smile\CustomEntity\Block\Html\Pager;
29+
1730
/**
1831
* Attribute set view block.
1932
* Inherits from Smile_CustomEntity view block and adds specific logic.
2033
* Will potentially interact with the layered navigation components later.
2134
*/
2235
class View extends SmileCustomEntityView
2336
{
37+
/**
38+
* View constructor.
39+
*
40+
* @param Template\Context $context Context.
41+
* @param Registry $registry Registry.
42+
* @param CustomEntityRepositoryInterface $customEntityRepository Custom entity repository.
43+
* @param SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory Search criteria builder factory.
44+
* @param array $data Block data.
45+
*/
46+
public function __construct(
47+
Template\Context $context,
48+
Registry $registry,
49+
CustomEntityRepositoryInterface $customEntityRepository,
50+
SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory,
51+
array $data = []
52+
) {
53+
parent::__construct($context, $registry, $customEntityRepository, $searchCriteriaBuilderFactory, $data);
54+
}
55+
2456
/**
2557
* Return entity list html
2658
*
@@ -38,7 +70,7 @@ public function getEntityListHtml()
3870
*/
3971
public function getAdditionalHtml()
4072
{
41-
return $this->getChildHtml('additional');
73+
return $this->getChildHtml('set_additional');
4274
}
4375

4476
/**
@@ -59,7 +91,10 @@ public function isFilteredEntity(): bool
5991
$requestParams = array_keys($this->_request->getParams());
6092
$found = array_intersect($params, $requestParams);
6193

62-
return count($found) > 0;
94+
if (count($found) > 0) {
95+
return true;
96+
}
97+
return false;
6398
}
6499

65100
/**
@@ -70,11 +105,9 @@ public function isFilteredEntity(): bool
70105
public function getIdentities(): array
71106
{
72107
$identities = [];
73-
74108
if ($attributeSet = $this->getAttributeSet()) {
75109
$identities[] = CustomEntity::CACHE_CUSTOM_ENTITY_SET_TAG . '_' . $attributeSet->getAttributeSetId();
76110
}
77-
78111
return $identities;
79112
}
80113
}

Block/SetList.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
namespace Amadeco\SmileCustomEntityLayeredNavigation\Block;
1818

19-
use Magento\Framework\Registry;
2019
use Magento\Framework\DataObject\IdentityInterface;
20+
use Magento\Framework\Data\Helper\PostHelper;
2121
use Magento\Framework\View\Element\Template;
2222
use Magento\Framework\View\Element\Template\Context;
2323
use Magento\Eav\Api\Data\AttributeSetInterface;
@@ -52,14 +52,14 @@ class SetList extends Template implements IdentityInterface
5252

5353
/**
5454
* @param Template\Context $context
55-
* @param Registry $registry
55+
* @param PostHelper $postDataHelper
5656
* @param CustomEntityCollectionFactory $customEntityCollectionFactory
5757
* @param LayerResolver $layerResolver
5858
* @param array $data Block data.
5959
*/
6060
public function __construct(
6161
protected Context $context,
62-
private Registry $registry,
62+
protected PostHelper $postDataHelper,
6363
private CustomEntityCollectionFactory $customEntityCollectionFactory,
6464
private LayerResolver $layerResolver,
6565
array $data = []
@@ -141,8 +141,8 @@ public function getMode()
141141
}
142142

143143
/**
144-
* Get listing mode for products if toolbar is removed from layout.
145-
* Use the general configuration for product list mode from config path catalog/frontend/list_mode as default value
144+
* Get listing mode for entities if toolbar is removed from layout.
145+
* Use the general configuration for entity list mode from config path catalog/custom_entity/list_mode as default value
146146
* or mode data from block declaration from layout.
147147
*
148148
* @return string
@@ -352,7 +352,14 @@ private function initializeEntityCollection()
352352
{
353353
$layer = $this->getLayer();
354354
$collection = $layer->getEntityCollection();
355+
355356
$this->prepareSortableFieldsBySet($layer->getCurrentAttributeSet());
357+
358+
$this->_eventManager->dispatch(
359+
'amadeco_block_set_list_collection',
360+
['collection' => $collection]
361+
);
362+
356363
return $collection;
357364
}
358365

Block/SetList/Toolbar.php

Lines changed: 90 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ class Toolbar extends Template
5757
*/
5858
protected $_availableOrder = null;
5959

60+
/**
61+
* List of available view types
62+
*
63+
* @var array
64+
*/
65+
protected $_availableMode = [];
66+
6067
/**
6168
* Is enable View switcher
6269
*
@@ -211,7 +218,14 @@ public function getCurrentDirection()
211218
return $dir;
212219
}
213220

214-
$dir = $this->_direction;
221+
$directions = ['asc', 'desc'];
222+
$dir = is_string($this->toolbarModel->getDirection()) ?
223+
strtolower($this->toolbarModel->getDirection()) : '';
224+
225+
if (!$dir || !in_array($dir, $directions)) {
226+
$dir = $this->_direction;
227+
}
228+
215229
$this->setData('_current_grid_direction', $dir);
216230
return $dir;
217231
}
@@ -316,13 +330,11 @@ public function isOrderCurrent($order)
316330
*/
317331
public function getPagerUrl($params = [])
318332
{
319-
$urlParams = [
320-
'_current' => true,
321-
'_escape' => false,
322-
'_use_rewrite' => true,
323-
'_query' => array_merge($params, ['entity_id' => null])
324-
];
325-
333+
$urlParams = [];
334+
$urlParams['_current'] = true;
335+
$urlParams['_escape'] = false;
336+
$urlParams['_use_rewrite'] = true;
337+
$urlParams['_query'] = $params;
326338
return $this->getUrl('*/*/*', $urlParams);
327339
}
328340

@@ -337,6 +349,67 @@ public function getPagerEncodedUrl($params = [])
337349
return $this->urlEncoder->encode($this->getPagerUrl($params));
338350
}
339351

352+
/**
353+
* Retrieve current View mode
354+
*
355+
* @return string
356+
*/
357+
public function getCurrentMode()
358+
{
359+
360+
$mode = $this->_getData('_current_grid_mode');
361+
if ($mode) {
362+
return $mode;
363+
}
364+
$defaultMode = $this->_setListHelper->getDefaultViewMode($this->getModes());
365+
$mode = $this->toolbarModel->getMode();
366+
if (!$mode || !isset($this->_availableMode[$mode])) {
367+
$mode = $defaultMode;
368+
}
369+
370+
$this->setData('_current_grid_mode', $mode);
371+
return $mode;
372+
}
373+
374+
/**
375+
* Compare defined view mode with current active mode
376+
*
377+
* @param string $mode
378+
* @return bool
379+
*/
380+
public function isModeActive($mode)
381+
{
382+
return $this->getCurrentMode() == $mode;
383+
}
384+
385+
/**
386+
* Retrieve available view modes
387+
*
388+
* @return array
389+
*/
390+
public function getModes()
391+
{
392+
if ($this->_availableMode === []) {
393+
$this->_availableMode = $this->_setListHelper->getAvailableViewMode();
394+
}
395+
return $this->_availableMode;
396+
}
397+
398+
/**
399+
* Set available view modes list
400+
*
401+
* @param array $modes
402+
* @return $this
403+
*/
404+
public function setModes($modes)
405+
{
406+
$this->getModes();
407+
if (!isset($this->_availableMode)) {
408+
$this->_availableMode = $modes;
409+
}
410+
return $this;
411+
}
412+
340413
/**
341414
* Disable view switcher
342415
*
@@ -408,10 +481,12 @@ public function isExpanded()
408481
*/
409482
public function getDefaultPerPageValue()
410483
{
411-
if ($default = $this->getDefaultGridPerPage()) {
484+
if ($this->getCurrentMode() == 'list' && ($default = $this->getDefaultListPerPage())) {
485+
return $default;
486+
} elseif ($this->getCurrentMode() == 'grid' && ($default = $this->getDefaultGridPerPage())) {
412487
return $default;
413488
}
414-
return 28;
489+
return $this->_setListHelper->getDefaultLimitPerPageValue($this->getCurrentMode());
415490
}
416491

417492
/**
@@ -455,11 +530,10 @@ public function getLimit()
455530
$defaultLimit = $this->getDefaultPerPageValue();
456531
if (!$defaultLimit || !isset($limits[$defaultLimit])) {
457532
$keys = array_keys($limits);
458-
$defaultLimit = $keys[0] ?? 10;
533+
$defaultLimit = $keys[0];
459534
}
460535

461536
$limit = $this->toolbarModel->getLimit();
462-
463537
if (!$limit || !isset($limits[$limit])) {
464538
$limit = $defaultLimit;
465539
}
@@ -582,20 +656,21 @@ public function getPagerHtml()
582656
*/
583657
public function getWidgetOptionsJson(array $customOptions = [])
584658
{
659+
$defaultMode = $this->_setListHelper->getDefaultViewMode($this->getModes());
585660
$options = [
586661
'mode' => ToolbarModel::MODE_PARAM_NAME,
587662
'direction' => ToolbarModel::DIRECTION_PARAM_NAME,
588663
'order' => ToolbarModel::ORDER_PARAM_NAME,
589664
'limit' => ToolbarModel::LIMIT_PARAM_NAME,
590-
'modeDefault' => 'grid',
665+
'modeDefault' => $defaultMode,
591666
'directionDefault' => $this->_direction,
592667
'orderDefault' => $this->getOrderField(),
593-
'limitDefault' => $this->getDefaultPerPageValue(),
668+
'limitDefault' => $this->_setListHelper->getDefaultLimitPerPageValue($defaultMode),
594669
'url' => $this->getPagerUrl(),
595670
'formKey' => $this->formKey->getFormKey()
596671
];
597672
$options = array_replace_recursive($options, $customOptions);
598-
return json_encode(['productListToolbarForm' => $options]);
673+
return json_encode(['entityListToolbarForm' => $options]);
599674
}
600675

601676
/**

0 commit comments

Comments
 (0)