Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

magento/adobe-stock-integration#1467: Introduce Used In Pages filter … #1546

Closed
wants to merge 9 commits into from
Closed
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

This conversation was marked as resolved.
Show resolved Hide resolved
namespace Magento\MediaGalleryUi\Model\SearchCriteria\CollectionProcessor\FilterProcessor;

use Magento\Framework\Api\Filter;
use Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface;
use Magento\Framework\Data\Collection\AbstractDb;

This conversation was marked as resolved.
Show resolved Hide resolved
class EntityId implements CustomFilterInterface
{
/**
* @inheritDoc
*/
public function apply(Filter $filter, AbstractDb $collection)
This conversation was marked as resolved.
Show resolved Hide resolved
{
$collection->getSelect()->where('entity_id IN (?) ', $filter->getValue());
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaGalleryUi\Model\SearchCriteria\CollectionProcessor\JoinProcessor;

use Magento\Framework\Api\SearchCriteria\CollectionProcessor\JoinProcessor\CustomJoinInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Data\Collection\AbstractDb;

This conversation was marked as resolved.
Show resolved Hide resolved
class EntityId implements CustomJoinInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename the class to Page to better reflect it's responsibility

{
private const MEDIA_CONTENT_ASSET_TABLE_NAME = 'media_content_asset';

private const CMS_PAGE_TABLE_NAME = 'cms_page';

/**
* @var ResourceConnection
*/
private $connection;

/**
* @param ResourceConnection $connection
*/
public function __construct(ResourceConnection $connection)
{
$this->connection = $connection;
}

/**
* @inheritDoc
*/
public function apply(AbstractDb $collection): bool
{
$collection->getSelect()->joinLeft(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This join is going to be added in #1531 no need to join the same table twice

['mca' => $this->connection->getTableName(self::MEDIA_CONTENT_ASSET_TABLE_NAME)],
'mca.asset_id = main_table.id',
['entity_type']
);

$collection->getSelect()->joinLeft(
['cms_page' => $this->connection->getTableName(self::CMS_PAGE_TABLE_NAME)],
'cms_page.page_id = mca.entity_id AND mca.entity_type = "cms_page"',
[]
);

//print_r($collection->getSelect()->__toString()); die;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, remove this line.


return true;
}
}
62 changes: 62 additions & 0 deletions MediaGalleryUi/Ui/Component/Listing/Columns/Pages/Options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaGalleryUi\Ui\Component\Listing\Columns\Pages;

use Magento\Cms\Api\PageRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Data\OptionSourceInterface;
use Magento\Framework\Exception\LocalizedException;


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary empty line

/**
* Image source filter options
*/
class Options implements OptionSourceInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Description can be updated, to the page filter options

{
protected $_pageRepository;

protected $_searchCriteriaBuilder;

protected $_filterBuilder;

This conversation was marked as resolved.
Show resolved Hide resolved
public function __construct(
This conversation was marked as resolved.
Show resolved Hide resolved
PageRepositoryInterface $pageRepository,
SearchCriteriaBuilder $searchCriteriaBuilder,
FilterBuilder $filterBuilder
) {
$this->_pageRepository = $pageRepository;
$this->_searchCriteriaBuilder = $searchCriteriaBuilder;
$this->_filterBuilder = $filterBuilder;
}

/**
* @inheritdoc
*/
public function toOptionArray(): array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to load the items dynamically, based on the user input

{
$searchCriteria = $this->_searchCriteriaBuilder->create();
$pages = [];

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, remove this empty line.

try {

coderimus marked this conversation as resolved.
Show resolved Hide resolved
$result = $this->_pageRepository->getList($searchCriteria);
/** @var \Magento\Cms\Api\Data\PageInterface $page */
foreach ($result->getItems() as $page) {
$pages[] = [
'value' => $page->getId(),
'label' => $page->getTitle()
];
}
} catch (LocalizedException $e) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think that exception should be thrown in that case. Please, replace $e with $exception

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gregbalonzo thanks for renaming a variable. Why do we need to suppress the exception, though?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sivaschenko its for my local testing purpose, I can remove it if this is not required.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gregbalonzo please keep the code for local testing purposes on the local, try to go only with the production-ready code for the push to Github.

}

return $pages;
}
}
14 changes: 14 additions & 0 deletions MediaGalleryUi/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,18 @@
<argument name="collectionProcessor" xsi:type="object">Magento\MediaGalleryUi\Model\Api\SearchCriteria\CollectionProcessor</argument>
</arguments>
</type>
<virtualType name="Magento\MediaGalleryUi\Model\Api\SearchCriteria\CollectionProcessor\JoinProcessor">
<arguments>
<argument name="customJoins" xsi:type="array">
<item name="page_id" xsi:type="object">Magento\MediaGalleryUi\Model\SearchCriteria\CollectionProcessor\JoinProcessor\EntityId</item>
</argument>
</arguments>
</virtualType>
<virtualType name="Magento\MediaGalleryUi\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor" type="Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor">
<arguments>
<argument name="customFilters" xsi:type="array">
<item name="page_id" xsi:type="object">Magento\MediaGalleryUi\Model\SearchCriteria\CollectionProcessor\FilterProcessor\EntityId</item>
</argument>
</arguments>
</virtualType>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,29 @@
<dataScope>updated_at</dataScope>
</settings>
</filterRange>
<filterSelect name="page_id" provider="${ $.parentName }" sortOrder="70" component="Magento_Ui/js/form/element/ui-select" template="ui/grid/filters/elements/ui-select">
<!--settings>
This conversation was marked as resolved.
Show resolved Hide resolved
<caption translate="true">All</caption>
<options class="Magento\MediaGalleryUi\Ui\Component\Listing\Columns\Pages\Options"/>
<label translate="true">Pages</label>
<dataScope>page_id</dataScope>
</settings-->
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filterOptions" xsi:type="boolean">true</item>
<item name="levelsVisibility" xsi:type="number">1</item>
</item>
</argument>
<settings>
<options class="Magento\MediaGalleryUi\Ui\Component\Listing\Columns\Pages\Options"/>
<caption translate="true">– Please Select a Category –</caption>
This conversation was marked as resolved.
Show resolved Hide resolved
<label translate="true">Pages</label>
This conversation was marked as resolved.
Show resolved Hide resolved
<dataScope>page_id</dataScope>
<imports>
<link name="visible">componentType = column, index = ${ $.index }:visible</link>
</imports>
</settings>
</filterSelect>
</filters>
<paging name="listing_paging">
<settings>
Expand Down