Skip to content

Commit

Permalink
Fixes Smile-SA#1248 Dropdown attributes values not saved
Browse files Browse the repository at this point in the history
Swatches handling for versions prior to 2.2.6
  • Loading branch information
rbayet committed Jan 22, 2019
1 parent 097a7b2 commit 789405d
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 0 deletions.
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;
}
}
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);
}
}
}
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);
}
}
}
13 changes: 13 additions & 0 deletions src/module-elasticsuite-swatches/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,17 @@
/>
</type>

<type name="Magento\Catalog\Controller\Adminhtml\Product\Attribute\Validate">
<plugin
name="elasticsuite_swatches_product_attribute_validate"
type="Smile\ElasticsuiteSwatches\Plugin\Catalog\Controller\Adminhtml\Product\Attribute\ValidatePlugin"
/>
</type>

<type name="Magento\Catalog\Controller\Adminhtml\Product\Attribute\Save">
<plugin
name="elasticsuite_swatches_product_attribute_save"
type="Smile\ElasticsuiteSwatches\Plugin\Catalog\Controller\Adminhtml\Product\Attribute\SavePlugin"
/>
</type>
</config>

0 comments on commit 789405d

Please sign in to comment.