Skip to content

Commit

Permalink
Merge pull request #1681 from konarshankar07/used-in-filter--task-1503
Browse files Browse the repository at this point in the history
#1503 :- Add links to grids to the image details panel
  • Loading branch information
sivaschenko authored Aug 3, 2020
2 parents fa4c4f2 + 2d53a19 commit 528aff6
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 37 deletions.
10 changes: 8 additions & 2 deletions MediaGalleryCatalogUi/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@
<type name="Magento\MediaGalleryUi\Model\AssetDetailsProvider\UsedIn">
<arguments>
<argument name="contentTypes" xsi:type="array">
<item name="catalog_category" xsi:type="string">Categories</item>
<item name="catalog_product" xsi:type="string">Products</item>
<item name="catalog_category" xsi:type="array">
<item name="name" xsi:type="string">Categories</item>
<item name="link" xsi:type="string">media_gallery_catalog/category/index</item>
</item>
<item name="catalog_product" xsi:type="array">
<item name="name" xsi:type="string">Products</item>
<item name="link" xsi:type="string">catalog/product/index</item>
</item>
</argument>
</arguments>
</type>
Expand Down
10 changes: 8 additions & 2 deletions MediaGalleryCmsUi/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@
<type name="Magento\MediaGalleryUi\Model\AssetDetailsProvider\UsedIn">
<arguments>
<argument name="contentTypes" xsi:type="array">
<item name="cms_block" xsi:type="string">Blocks</item>
<item name="cms_page" xsi:type="string">Pages</item>
<item name="cms_block" xsi:type="array">
<item name="name" xsi:type="string">Blocks</item>
<item name="link" xsi:type="string">cms/block/index</item>
</item>
<item name="cms_page" xsi:type="array">
<item name="name" xsi:type="string">Pages</item>
<item name="link" xsi:type="string">cms/page/index</item>
</item>
</argument>
</arguments>
</type>
Expand Down
33 changes: 31 additions & 2 deletions MediaGalleryUi/Model/AssetDetailsProvider/UsedIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\MediaGalleryUi\Model\AssetDetailsProvider;

use Magento\Backend\Model\UrlInterface;
use Magento\Framework\Exception\IntegrationException;
use Magento\MediaContentApi\Api\GetContentByAssetIdsInterface;
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
Expand All @@ -27,15 +28,23 @@ class UsedIn implements AssetDetailsProviderInterface
*/
private $contentTypes;

/**
* @var UrlInterface
*/
private $url;

/**
* @param GetContentByAssetIdsInterface $getContent
* @param UrlInterface $url
* @param array $contentTypes
*/
public function __construct(
GetContentByAssetIdsInterface $getContent,
UrlInterface $url,
array $contentTypes = []
) {
$this->getContent = $getContent;
$this->url = $url;
$this->contentTypes = $contentTypes;
}

Expand All @@ -62,6 +71,26 @@ public function execute(AssetInterface $asset): array
* @throws IntegrationException
*/
private function getUsedIn(int $assetId): array
{
$details = [];

foreach ($this->getUsedInCounts($assetId) as $type => $number) {
$details[$type] = $this->contentTypes[$type] ?? ['name' => $type, 'link' => null];
$details[$type]['number'] = $number;
$details[$type]['link'] = $details[$type]['link'] ? $this->url->getUrl($details[$type]['link']) : null;
}

return array_values($details);
}

/**
* Get used in counts per type
*
* @param int $assetId
* @return int[]
* @throws IntegrationException
*/
private function getUsedInCounts(int $assetId): array
{
$usedIn = [];
$entityIds = [];
Expand All @@ -70,12 +99,12 @@ private function getUsedIn(int $assetId): array

foreach ($contentIdentities as $contentIdentity) {
$entityId = $contentIdentity->getEntityId();
$type = $this->contentTypes[$contentIdentity->getEntityType()] ?? $contentIdentity->getEntityType();
$type = $contentIdentity->getEntityType();

if (!isset($entityIds[$type])) {
$usedIn[$type] = 1;
} elseif ($entityIds[$type]['entity_id'] !== $entityId) {
++$usedIn[$type];
++$usedIn[$type];
}
$entityIds[$type]['entity_id'] = $entityId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
</actionGroup>
<actionGroup ref="AdminEnhancedMediaGalleryClickDeleteImagesButtonActionGroup" stepKey="clikDeleteSelectedButton"/>
<actionGroup ref="AdminEnhancedMediaGalleryAssertWarningMessageActionGroup" stepKey="assertMessageImageUsedIn">
<argument name="messageText" value="The selected assets are used for the following entities content: Categories(1)"/>
<argument name="messageText" value="The selected assets are used in the content of the following entities: Categories(1)"/>
</actionGroup>
<actionGroup ref="AdminEnhancedMediaGalleryConfirmDeleteImagesActionGroup" stepKey="deleteImage"/>
</test>
Expand Down
4 changes: 3 additions & 1 deletion MediaGalleryUi/Ui/Component/Listing/Filters/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public function __construct(
public function applyFilter()
{
if (isset($this->filterData[$this->getName()])) {
$ids = $this->filterData[$this->getName()];
$ids = is_array($this->filterData[$this->getName()])
? $this->filterData[$this->getName()]
: [$this->filterData[$this->getName()]];
$filter = $this->filterBuilder->setConditionType('in')
->setField($this->_data['config']['identityColumn'])
->setValue($this->getEntityIdsByAsset($ids))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,28 @@ define([
/**
* Get information about image use
*
* @param {Object|String} imageDetails
* @param {Object|String} images
* @return {String}
*/
getRecordRelatedContentMessage: function (imageDetails) {
var usedInMessage = $t('The selected assets are used for the following entities content: '),
usedIn = {};
getRecordRelatedContentMessage: function (images) {
var usedInMessage = $t('The selected assets are used in the content of the following entities: '),
usedIn = [];

$.each(imageDetails, function (key, image) {
if (_.isObject(image.details[6]) && !_.isEmpty(image.details[6].value)) {
$.each(image.details[6].value, function (entityName, count) {
usedIn[entityName] = usedIn[entityName] + count || count;
});
}
$.each(images, function (key, image) {
$.each(image.details, function (sectionIndex, section) {
if (section.title === 'Used In' && _.isObject(section) && !_.isEmpty(section.value)) {
$.each(section.value, function (entityTypeIndex, entityTypeData) {
usedIn.push(entityTypeData.name + '(' + entityTypeData.number + ')');
});
}
});
});

if (_.isEmpty(usedIn)) {
return '';
}
$.each(usedIn, function (entityName, count) {
usedInMessage += entityName + '(' + count + '), ';
});

return usedInMessage;
return usedInMessage + usedIn.join(', ') + '.';
}
};
});
35 changes: 22 additions & 13 deletions MediaGalleryUi/view/adminhtml/web/js/image/image-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,32 @@ define([
},

/**
* Get image details value
* Is value an object
*
* @param {Object|String} value
* @return {String}
* @param {*} value
* @returns {Boolean}
*/
getValueUnsanitizedHtml: function (value) {
var usedIn = '';

if (_.isObject(value)) {
$.each(value, function (entityName, count) {
usedIn += entityName + '(' + count + ') </br>';
});
isArray: function (value) {
return _.isArray(value);
},

return usedIn;
}
/**
* Get name and number text for used in link
*
* @param {Object} item
* @returns {String}
*/
getUsedInText: function (item) {
return item.name + '(' + item.number + ')';
},

return value;
/**
* Get filter url
*
* @param {String} link
*/
getFilterUrl: function (link) {
return link + '?filters[asset_id]=' + this.image().id;
},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,20 @@ <h3 translate="'Filename'"></h3>
<h3 translate="'Details'"></h3>
<div class="attributes">
<each args="image().details">
<div class="attribute" if="$parent.getValueUnsanitizedHtml(value)">
<div class="attribute" if="value">
<span class="title" translate="title"></span>
<div class="value" html="$parent.getValueUnsanitizedHtml(value)"></div>
<ifnot args="$parent.isArray(value)">
<div class="value" text="value"></div>
</ifnot>
<if args="$parent.isArray(value)">
<each args="{ data: value, as: 'item'}">
<div class="value">
<a attr="href: $parents[1].getFilterUrl(item.link)"
text="$parents[1].getUsedInText(item)"></a>
</br>
</div>
</each>
</if>
</div>
</each>
</div>
Expand Down

0 comments on commit 528aff6

Please sign in to comment.