forked from Smile-SA/elasticsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes Smile-SA#1248 Dropdown attributes values not saved
Swatches handling for versions prior to 2.2.6
- Loading branch information
Showing
4 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/module-elasticsuite-swatches/Model/Serialize/Serializer/FormData.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade this module to newer | ||
* versions in the future. | ||
* | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteSwatches | ||
* @author Richard BAYET <richard.bayet@smile.fr> | ||
* @copyright 2019 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
|
||
namespace Smile\ElasticsuiteSwatches\Model\Serialize\Serializer; | ||
|
||
use Magento\Framework\Serialize\Serializer\Json; | ||
|
||
/** | ||
* Class for processing of serialized form data. | ||
* Copy of \Magento\Framework\Serialize\Serializer\FormData which became available in 2.2.7 | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteSwatches | ||
*/ | ||
class FormData | ||
{ | ||
/** | ||
* @var Json | ||
*/ | ||
private $serializer; | ||
|
||
/** | ||
* @param Json $serializer | ||
*/ | ||
public function __construct(Json $serializer) | ||
{ | ||
$this->serializer = $serializer; | ||
} | ||
|
||
/** | ||
* Provides form data from the serialized data. | ||
* | ||
* @param string $serializedData | ||
* @return array | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function unserialize(string $serializedData): array | ||
{ | ||
$encodedFields = $this->serializer->unserialize($serializedData); | ||
|
||
if (!is_array($encodedFields)) { | ||
throw new \InvalidArgumentException('Unable to unserialize value.'); | ||
} | ||
|
||
$formData = []; | ||
foreach ($encodedFields as $item) { | ||
$decodedFieldData = []; | ||
parse_str($item, $decodedFieldData); | ||
$formData = array_replace_recursive($formData, $decodedFieldData); | ||
} | ||
|
||
return $formData; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...lasticsuite-swatches/Plugin/Catalog/Controller/Adminhtml/Product/Attribute/SavePlugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade this module to newer | ||
* versions in the future. | ||
* | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticSuiteSwatches | ||
* @author Richard BAYET <richard.bayet@smile.fr> | ||
* @copyright 2019 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
|
||
namespace Smile\ElasticsuiteSwatches\Plugin\Catalog\Controller\Adminhtml\Product\Attribute; | ||
|
||
use Smile\ElasticsuiteSwatches\Model\Serialize\Serializer\FormData; | ||
use Magento\Framework\App\ProductMetadataInterface; | ||
use Magento\Catalog\Controller\Adminhtml\Product\Attribute; | ||
|
||
/** | ||
* Plugin to force deserialization of product attribute options if in a version < 2.2.6 where it was introduced. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticSuiteSwatches | ||
*/ | ||
class SavePlugin | ||
{ | ||
/** | ||
* @var FormData | ||
*/ | ||
private $formDataSerializer; | ||
|
||
/** | ||
* @var ProductMetadataInterface | ||
*/ | ||
private $productMetadata; | ||
|
||
/** | ||
* SavePlugin constructor. | ||
* | ||
* @param FormData $formDataSerializer Form data serializer/deserializer | ||
* @param ProductMetadataInterface $productMetadata Product metadata interface | ||
*/ | ||
public function __construct(FormData $formDataSerializer, ProductMetadataInterface $productMetadata) | ||
{ | ||
$this->formDataSerializer = $formDataSerializer; | ||
$this->productMetadata = $productMetadata; | ||
} | ||
|
||
/** | ||
* Before Plugin : if Magento version is < 2.2.6, deserialize attributes options | ||
* before re-inserting them in the request | ||
* | ||
* @param Attribute\Save $subject Controller | ||
* | ||
* @return void | ||
*/ | ||
public function beforeExecute(Attribute\Save $subject) | ||
{ | ||
if (version_compare($this->productMetadata->getVersion(), '2.2.6', '<')) { | ||
try { | ||
$optionData = $this->formDataSerializer->unserialize( | ||
$subject->getRequest()->getParam('serialized_options', '[]') | ||
); | ||
} catch (\InvalidArgumentException $e) { | ||
return; | ||
} | ||
|
||
$data = $subject->getRequest()->getPostValue(); | ||
unset($data['serialized_options']); | ||
$data = array_replace_recursive( | ||
$data, | ||
$optionData | ||
); | ||
|
||
$subject->getRequest()->setPostValue($data); | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...icsuite-swatches/Plugin/Catalog/Controller/Adminhtml/Product/Attribute/ValidatePlugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade this module to newer | ||
* versions in the future. | ||
* | ||
* | ||
* @category Smile | ||
* @package Smile\Elasticsuite | ||
* @author Richard BAYET <richard.bayet@smile.fr> | ||
* @copyright 2019 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
|
||
namespace Smile\ElasticsuiteSwatches\Plugin\Catalog\Controller\Adminhtml\Product\Attribute; | ||
|
||
use Smile\ElasticsuiteSwatches\Model\Serialize\Serializer\FormData; | ||
use Magento\Framework\App\ProductMetadataInterface; | ||
use Magento\Catalog\Controller\Adminhtml\Product\Attribute; | ||
|
||
/** | ||
* Plugin to force deserialization of product attribute options if in a version < 2.2.6 where it was introduced. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteSwatches | ||
*/ | ||
class ValidatePlugin | ||
{ | ||
/** | ||
* @var FormData | ||
*/ | ||
private $formDataSerializer; | ||
|
||
/** | ||
* @var ProductMetadataInterface | ||
*/ | ||
private $productMetadata; | ||
|
||
/** | ||
* ValidatePlugin constructor. | ||
* | ||
* @param FormData $formDataSerializer Form data serializer/deserializer | ||
* @param ProductMetadataInterface $productMetadata Product metadata interface | ||
*/ | ||
public function __construct(FormData $formDataSerializer, ProductMetadataInterface $productMetadata) | ||
{ | ||
$this->formDataSerializer = $formDataSerializer; | ||
$this->productMetadata = $productMetadata; | ||
} | ||
|
||
/** | ||
* Before Plugin : if Magento version is < 2.2.6, deserialize attributes options | ||
* before re-inserting them in the request | ||
* | ||
* @param Attribute\Validate $subject Controller | ||
* | ||
* @return void | ||
*/ | ||
public function beforeExecute(Attribute\Validate $subject) | ||
{ | ||
if (version_compare($this->productMetadata->getVersion(), '2.2.6', '<')) { | ||
try { | ||
$optionData = $this->formDataSerializer->unserialize( | ||
$subject->getRequest()->getParam('serialized_options', '[]') | ||
); | ||
} catch (\InvalidArgumentException $e) { | ||
return; | ||
} | ||
|
||
$data = $subject->getRequest()->getPostValue(); | ||
unset($data['serialized_options']); | ||
$data = array_replace_recursive( | ||
$data, | ||
$optionData | ||
); | ||
|
||
$subject->getRequest()->setPostValue($data); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters