Skip to content

Commit

Permalink
xMerge branch '3333-elasticsuite-catalog-default-sort-direction-syste…
Browse files Browse the repository at this point in the history
…m-config-settings-2.10_feature' of github.com:vahonc/elasticsuite into vahonc-3333-elasticsuite-catalog-default-sort-direction-system-config-settings-2.10_feature
  • Loading branch information
romainruaud committed Sep 5, 2024
2 parents 3d5fd47 + b72b696 commit 4c1978e
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCatalog
* @author Vadym Honcharuk <vahonc@smile.fr>
* @copyright 2024 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteCatalog\Model\Config\Source;

use Magento\Framework\Data\OptionSourceInterface;

/**
* Custom sort direction source model.
*
* @category Smile
* @package Smile\ElasticsuiteCatalog
* @author Vadym Honcharuk <vahonc@smile.fr>
*/
class SortDirectionConfig implements OptionSourceInterface
{
/**
* Get options in "value-label" format.
*
* @return array
*/
public function toOptionArray()
{
return [
['value' => 'asc', 'label' => __('ASC')],
['value' => 'desc', 'label' => __('DESC')],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
*/
class DataProviderPlugin
{
/**
* @var array
*/
protected $elementsWithUseConfigSetting = [
'sort_direction',
];

/**
* @var AttributeCollectionFactory
*/
Expand Down Expand Up @@ -109,6 +116,8 @@ public function aroundGetMeta(CategoryDataProvider $dataProvider, \Closure $proc
* Append filter configuration (sort order and display mode) data.
* Meta is added in the ui_component via XML.
*
* @SuppressWarnings(PHPMD.ElseExpression)
*
* @param CategoryDataProvider $dataProvider Data provider.
* @param \Closure $proceed Original method.
*
Expand All @@ -122,11 +131,41 @@ public function aroundGetData(CategoryDataProvider $dataProvider, \Closure $proc

if ($currentCategory->getId() !== null && $currentCategory->getLevel() >= 2) {
$data[$currentCategory->getId()]['facet_config'] = $this->getFilterableAttributeList($currentCategory);
$categoryData = &$data[$currentCategory->getId()];

foreach ($this->elementsWithUseConfigSetting as $elementsWithUseConfigSetting) {
if (!isset($categoryData['use_config'][$elementsWithUseConfigSetting])) {
if (!isset($categoryData[$elementsWithUseConfigSetting]) ||
($categoryData[$elementsWithUseConfigSetting] == '')
) {
$categoryData['use_config'][$elementsWithUseConfigSetting] = true;
} else {
$categoryData['use_config'][$elementsWithUseConfigSetting] = false;
}
}
}
}

return $data;
}

/**
* Modify default metadata to include 'use_config.sort_direction'.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* @param CategoryDataProvider $dataProvider Data provider.
* @param array $result Original data.
*
* @return array
*/
public function afterGetDefaultMetaData(CategoryDataProvider $dataProvider, array $result)
{
$result['use_config.sort_direction']['default'] = true;

return $result;
}

/**
* Retrieve facet configuration for current category.
* Compute the intersection between existing data for the category, and all attributes set as filterable.
Expand Down
10 changes: 10 additions & 0 deletions src/module-elasticsuite-catalog/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,15 @@
</field>
</group>
</section>

<section id="catalog">
<group id="frontend">
<field id="default_sort_direction_by" translate="label comment" type="select" sortOrder="6" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Product Listing Sort Direction by</label>
<source_model>Smile\ElasticsuiteCatalog\Model\Config\Source\SortDirectionConfig</source_model>
<comment><![CDATA[Set the default sort direction for product listing.]]></comment>
</field>
</group>
</section>
</system>
</config>
3 changes: 3 additions & 0 deletions src/module-elasticsuite-catalog/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<catalog>
<frontend>
<default_sort_direction_by>asc</default_sort_direction_by>
</frontend>
<search>
<engine>elasticsuite</engine>
</search>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,27 +322,54 @@
</formElements>
</field>
</container>
<field name="sort_direction" formElement="select" sortOrder="26">
<container name="smile_default_sort_direction_group" component="Magento_Ui/js/form/components/group" sortOrder="26">
<argument name="data" xsi:type="array">
<item name="type" xsi:type="string">group</item>
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">category</item>
<item name="validation" xsi:type="array">
<item name="required-entry" xsi:type="boolean">false</item>
</item>
<item name="notice" xsi:type="string" translate="true">The preview will not be accurate when using DESC order with position sorting.</item>
<item name="breakLine" xsi:type="boolean">true</item>
</item>
</argument>
<settings>
<additionalClasses>
<class name="admin__field-default">true</class>
<class name="es-esfeature__logo">true</class>
</additionalClasses>
<label translate="true">Sort Direction</label>
<labelVisible>true</labelVisible>
<dataType>text</dataType>
<visible>true</visible>
<dataScope>sort_direction</dataScope>
</settings>
</field>
<field name="sort_direction" formElement="select" sortOrder="10">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">category</item>
<item name="validation" xsi:type="array">
<item name="required-entry" xsi:type="boolean">false</item>
</item>
<item name="notice" xsi:type="string" translate="true">The preview will not be accurate when using DESC order with position sorting.</item>
</item>
</argument>
<settings>
<additionalClasses>
<class name="admin__field-default">true</class>
<class name="es-esfeature__logo">true</class>
</additionalClasses>
<label translate="true">Sort Direction</label>
<labelVisible>true</labelVisible>
<dataType>text</dataType>
<visible>true</visible>
<dataScope>sort_direction</dataScope>
</settings>
</field>
<field name="use_config.sort_direction" formElement="checkbox">
<settings>
<dataType>boolean</dataType>
<exports>
<link name="checked">ns = ${ $.ns }, index = sort_direction :disabled</link>
</exports>
</settings>
<formElements>
<checkbox>
<settings>
<description translate="true">Use Config Settings</description>
<valueMap>
<map name="false" xsi:type="boolean">false</map>
<map name="true" xsi:type="boolean">true</map>
</valueMap>
</settings>
</checkbox>
</formElements>
</field>
</container>
</fieldset>
</form>

0 comments on commit 4c1978e

Please sign in to comment.