Skip to content

Commit 60674b2

Browse files
committed
implement visibility
1 parent 23cbe14 commit 60674b2

File tree

7 files changed

+97
-67
lines changed

7 files changed

+97
-67
lines changed

Block/Widget/Document/Link.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private function resolveDocument(): ?DocumentInterface
9393
{
9494
if (!$this->hasData('document')) {
9595
try {
96-
$this->setData('document', $this->documentRepository->getById((int)$this->getData('document_id')));
96+
$this->setData('document', $this->documentRepository->getById((int) $this->getData('document_id')));
9797
} catch (NoSuchEntityException $e) {
9898
$this->_logger->error($e->getLogMessage(), $e->getTrace());
9999
$this->setData('document');

Block/Widget/Document/ListByType.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Opengento\DocumentWidget\Block\Widget\Document;
99

10+
use Magento\Framework\Data\CollectionModifierInterface;
1011
use Magento\Framework\DataObject\IdentityInterface;
1112
use Magento\Framework\Exception\NoSuchEntityException;
1213
use Magento\Framework\View\Element\Template;
@@ -42,18 +43,25 @@ class ListByType extends Template implements BlockInterface, IdentityInterface
4243
*/
4344
private $imageViewModel;
4445

46+
/**
47+
* @var CollectionModifierInterface
48+
*/
49+
private $collectionModifier;
50+
4551
public function __construct(
4652
Context $context,
4753
DocumentTypeRepositoryInterface $docTypeRepository,
4854
CollectionFactory $collectionFactory,
4955
UrlViewModel $urlViewModel,
5056
ImageViewModel $imageViewModel,
57+
CollectionModifierInterface $collectionModifier,
5158
array $data = []
5259
) {
5360
$this->docTypeRepository = $docTypeRepository;
5461
$this->collectionFactory = $collectionFactory;
5562
$this->urlViewModel = $urlViewModel;
5663
$this->imageViewModel = $imageViewModel;
64+
$this->collectionModifier = $collectionModifier;
5765
parent::__construct($context, $data);
5866
}
5967

@@ -102,7 +110,7 @@ private function createCollection(): Collection
102110
/** @var Collection $collection */
103111
$collection = $this->collectionFactory->create();
104112
$collection->addFieldToFilter('type_id', ['eq' => $this->getData('type_id')]);
105-
$collection->addDefaultImage();
113+
$this->collectionModifier->apply($collection);
106114

107115
return $collection;
108116
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\DocumentWidget\Model\Document\Collection;
9+
10+
use Magento\Framework\Data\Collection\AbstractDb;
11+
use Magento\Framework\Data\CollectionModifierInterface;
12+
13+
/**
14+
* @api
15+
*/
16+
final class CollectionModifier implements CollectionModifierInterface
17+
{
18+
/**
19+
* @var CollectionModifierInterface[]
20+
*/
21+
private $modifiers;
22+
23+
public function __construct(
24+
array $modifiers = []
25+
) {
26+
$this->modifiers = $modifiers;
27+
}
28+
29+
public function apply(AbstractDb $collection): void
30+
{
31+
foreach ($this->modifiers as $modifier) {
32+
$modifier->apply($collection);
33+
}
34+
}
35+
}

Model/Document/Collection/SorterModifier.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,26 @@ final class SorterModifier implements CollectionModifierInterface
2020
*/
2121
private $sorter;
2222

23+
/**
24+
* @var string[]
25+
*/
26+
private $mapper;
27+
2328
public function __construct(
24-
Sorter $sorter
29+
Sorter $sorter,
30+
array $mapper = []
2531
) {
2632
$this->sorter = $sorter;
33+
$this->mapper = $mapper;
2734
}
2835

2936
public function apply(AbstractDb $collection): void
3037
{
31-
$collection->setOrder($this->sorter->getOrder(), $this->sorter->getDirection());
38+
$field = $this->sorter->getOrder();
39+
if (isset($this->mapper[$field])) {
40+
$collection->addFilterToMap($field, $this->mapper[$field]);
41+
}
42+
43+
$collection->setOrder($field, $this->sorter->getDirection());
3244
}
3345
}

ViewModel/DocumentType.php

Lines changed: 0 additions & 63 deletions
This file was deleted.

etc/di.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © OpenGento, All rights reserved.
5+
* See LICENSE bundled with this library for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="Opengento\DocumentWidget\Model\Document\Collection\CollectionModifier">
10+
<arguments>
11+
<argument name="modifiers" xsi:type="array">
12+
<item name="select" xsi:type="object">Opengento\Document\Model\Document\Collection\SelectModifier</item>
13+
<item name="sorter" xsi:type="object">Opengento\DocumentWidget\Model\Document\Collection\SorterModifier</item>
14+
</argument>
15+
</arguments>
16+
</type>
17+
<type name="Opengento\DocumentWidget\Block\Widget\Document\ListByType">
18+
<arguments>
19+
<argument name="collectionModifier" xsi:type="object">Opengento\DocumentWidget\Model\Document\Collection\CollectionModifier</argument>
20+
</arguments>
21+
</type>
22+
<type name="Opengento\DocumentWidget\Model\Document\Collection\SorterModifier">
23+
<arguments>
24+
<argument name="mapper" xsi:type="array">
25+
<item name="entity_id" xsi:type="string">main_table.entity_id</item>
26+
</argument>
27+
</arguments>
28+
</type>
29+
</config>

etc/frontend/di.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,13 @@
3232
</argument>
3333
</arguments>
3434
</type>
35+
<type name="Opengento\DocumentWidget\Model\Document\Collection\CollectionModifier">
36+
<arguments>
37+
<argument name="modifiers" xsi:type="array">
38+
<item name="select" xsi:type="object">Opengento\Document\Model\Document\Collection\SelectModifier</item>
39+
<item name="sorter" xsi:type="object">Opengento\DocumentWidget\Model\Document\Collection\SorterModifier</item>
40+
<item name="visibility" xsi:type="object">Opengento\Document\Model\Document\Collection\FrontendVisibilityModifier</item>
41+
</argument>
42+
</arguments>
43+
</type>
3544
</config>

0 commit comments

Comments
 (0)