Skip to content

Commit

Permalink
Merge pull request Smile-SA#3369 from Smile-SA/vahonc-3333-elasticsui…
Browse files Browse the repository at this point in the history
…te-catalog-default-sort-direction-system-config-settings-2.10_feature

Default sort direction system config settings 2.10 feature
  • Loading branch information
romainruaud authored Sep 5, 2024
2 parents 3d5fd47 + 126f302 commit 2aa4b18
Show file tree
Hide file tree
Showing 7 changed files with 237 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
@@ -0,0 +1,96 @@
<?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 Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2024 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteCatalog\Plugin\Catalog\Controller\Adminhtml\Category;

use Magento\Catalog\Controller\Adminhtml\Category\Save;

/**
* Plugin on Category Save controller that allows to handle additional "use_config" elements.
* The legacy ones are hardcoded in the controller class...
*
* @category Smile
* @package Smile\ElasticsuiteCatalog
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class SavePlugin
{
/**
* @var array
*/
private $stringToBoolInputs = [
'use_config' => ['sort_direction'],
];

/**
* Constructor
*
* @param array $stringToBoolInputs The input names to cast
*/
public function __construct(array $stringToBoolInputs = [])
{
$this->stringToBoolInputs = array_merge_recursive($this->stringToBoolInputs, $stringToBoolInputs);
}

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* @param \Magento\Catalog\Controller\Adminhtml\Category\Save $subject Category controller
* @param array $result Result
* @param array $data Legacy Data
* @param array $stringToBoolInputs Inputs to be converted
*
* @return array
*/
public function afterStringToBoolConverting(Save $subject, array $result, array $data, array $stringToBoolInputs = null)
{
return $this->stringToBoolConverting($result);
}

/**
* Copy paste of the legacy method of the category save controller.
* Copy pasted because the legacy method cannot be called again in a plugin since it's recursive.
*
* @SuppressWarnings(PHPMD.ElseExpression)
*
* @param array $data The data
* @param array $stringToBoolInputs The inputs
*
* @return mixed
*/
private function stringToBoolConverting($data, array $stringToBoolInputs = null)
{
if (null === $stringToBoolInputs) {
$stringToBoolInputs = $this->stringToBoolInputs;
}

foreach ($stringToBoolInputs as $key => $value) {
if (is_array($value)) {
if (isset($data[$key])) {
$data[$key] = $this->stringToBoolConverting($data[$key], $value);
}
} else {
if (isset($data[$value])) {
if ($data[$value] === 'true') {
$data[$value] = true;
}
if ($data[$value] === 'false') {
$data[$value] = false;
}
}
}
}

return $data;
}
}
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
5 changes: 5 additions & 0 deletions src/module-elasticsuite-catalog/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@
</argument>
</arguments>
</virtualType>

<!-- Handle category additional "use_config" checkboxes because the native ones are hardcoded in controller -->
<type name="Magento\Catalog\Controller\Adminhtml\Category\Save">
<plugin name="handle_additional_use_config" type="Smile\ElasticsuiteCatalog\Plugin\Catalog\Controller\Adminhtml\Category\SavePlugin" />
</type>
</config>
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 2aa4b18

Please sign in to comment.