Skip to content

Commit

Permalink
implement visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Dec 8, 2020
1 parent 23cbe14 commit 60674b2
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 67 deletions.
2 changes: 1 addition & 1 deletion Block/Widget/Document/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private function resolveDocument(): ?DocumentInterface
{
if (!$this->hasData('document')) {
try {
$this->setData('document', $this->documentRepository->getById((int)$this->getData('document_id')));
$this->setData('document', $this->documentRepository->getById((int) $this->getData('document_id')));
} catch (NoSuchEntityException $e) {
$this->_logger->error($e->getLogMessage(), $e->getTrace());
$this->setData('document');
Expand Down
10 changes: 9 additions & 1 deletion Block/Widget/Document/ListByType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Opengento\DocumentWidget\Block\Widget\Document;

use Magento\Framework\Data\CollectionModifierInterface;
use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\Template;
Expand Down Expand Up @@ -42,18 +43,25 @@ class ListByType extends Template implements BlockInterface, IdentityInterface
*/
private $imageViewModel;

/**
* @var CollectionModifierInterface
*/
private $collectionModifier;

public function __construct(
Context $context,
DocumentTypeRepositoryInterface $docTypeRepository,
CollectionFactory $collectionFactory,
UrlViewModel $urlViewModel,
ImageViewModel $imageViewModel,
CollectionModifierInterface $collectionModifier,
array $data = []
) {
$this->docTypeRepository = $docTypeRepository;
$this->collectionFactory = $collectionFactory;
$this->urlViewModel = $urlViewModel;
$this->imageViewModel = $imageViewModel;
$this->collectionModifier = $collectionModifier;
parent::__construct($context, $data);
}

Expand Down Expand Up @@ -102,7 +110,7 @@ private function createCollection(): Collection
/** @var Collection $collection */
$collection = $this->collectionFactory->create();
$collection->addFieldToFilter('type_id', ['eq' => $this->getData('type_id')]);
$collection->addDefaultImage();
$this->collectionModifier->apply($collection);

return $collection;
}
Expand Down
35 changes: 35 additions & 0 deletions Model/Document/Collection/CollectionModifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\DocumentWidget\Model\Document\Collection;

use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Data\CollectionModifierInterface;

/**
* @api
*/
final class CollectionModifier implements CollectionModifierInterface
{
/**
* @var CollectionModifierInterface[]
*/
private $modifiers;

public function __construct(
array $modifiers = []
) {
$this->modifiers = $modifiers;
}

public function apply(AbstractDb $collection): void
{
foreach ($this->modifiers as $modifier) {
$modifier->apply($collection);
}
}
}
16 changes: 14 additions & 2 deletions Model/Document/Collection/SorterModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,26 @@ final class SorterModifier implements CollectionModifierInterface
*/
private $sorter;

/**
* @var string[]
*/
private $mapper;

public function __construct(
Sorter $sorter
Sorter $sorter,
array $mapper = []
) {
$this->sorter = $sorter;
$this->mapper = $mapper;
}

public function apply(AbstractDb $collection): void
{
$collection->setOrder($this->sorter->getOrder(), $this->sorter->getDirection());
$field = $this->sorter->getOrder();
if (isset($this->mapper[$field])) {
$collection->addFilterToMap($field, $this->mapper[$field]);
}

$collection->setOrder($field, $this->sorter->getDirection());
}
}
63 changes: 0 additions & 63 deletions ViewModel/DocumentType.php

This file was deleted.

29 changes: 29 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Opengento\DocumentWidget\Model\Document\Collection\CollectionModifier">
<arguments>
<argument name="modifiers" xsi:type="array">
<item name="select" xsi:type="object">Opengento\Document\Model\Document\Collection\SelectModifier</item>
<item name="sorter" xsi:type="object">Opengento\DocumentWidget\Model\Document\Collection\SorterModifier</item>
</argument>
</arguments>
</type>
<type name="Opengento\DocumentWidget\Block\Widget\Document\ListByType">
<arguments>
<argument name="collectionModifier" xsi:type="object">Opengento\DocumentWidget\Model\Document\Collection\CollectionModifier</argument>
</arguments>
</type>
<type name="Opengento\DocumentWidget\Model\Document\Collection\SorterModifier">
<arguments>
<argument name="mapper" xsi:type="array">
<item name="entity_id" xsi:type="string">main_table.entity_id</item>
</argument>
</arguments>
</type>
</config>
9 changes: 9 additions & 0 deletions etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@
</argument>
</arguments>
</type>
<type name="Opengento\DocumentWidget\Model\Document\Collection\CollectionModifier">
<arguments>
<argument name="modifiers" xsi:type="array">
<item name="select" xsi:type="object">Opengento\Document\Model\Document\Collection\SelectModifier</item>
<item name="sorter" xsi:type="object">Opengento\DocumentWidget\Model\Document\Collection\SorterModifier</item>
<item name="visibility" xsi:type="object">Opengento\Document\Model\Document\Collection\FrontendVisibilityModifier</item>
</argument>
</arguments>
</type>
</config>

0 comments on commit 60674b2

Please sign in to comment.