I have a Shopware 6 plugin currently configured with a boolean reviewSkipModeration value that automatically publishes any review submitted through the plugin:
config.xml
<input-field type="bool"> <name>reviewSkipModeration</name> <label>Accept reviews automatically</label> <defaultValue>true</defaultValue> </input-field>
The current version of this plugin is 1.7.2. In the new version 1.7.3, I want to remove the reviewSkipModeration boolean configuration and add the new autoPublishStars multi-select configuration:
config.xml
<input-field type="single-select">
<name>autoPublishStars</name>
<label>Auto Publish review with stars</label>
<label lang="de-DE">Sterne vorausgewählt</label>
<defaultValue>0</defaultValue>
<options>
<option>
<id>None</id>
<name>None</name>
<name lang="de-DE">None</name>
</option>
<option>
<id>0</id>
<name>0 Stars</name>
<name lang="de-DE">0 Stars</name>
</option>
<option>
<id>1</id>
<name>1 Star</name>
<name lang="de-DE">1 Stern</name>
</option>
<option>
<id>2</id>
<name>2 Stars</name>
<name lang="de-DE">2 Sterne</name>
</option>
<option>
<id>3</id>
<name>3 Stars</name>
<name lang="de-DE">3 Sterne</name>
</option>
<option>
<id>4</id>
<name>4 Stars</name>
<name lang="de-DE">4 Sterne</name>
</option>
<option>
<id>5</id>
<name>5 Stars</name>
<name lang="de-DE">5 Sterne</name>
</option>
</options>
</input-field>
The current logic is implemented in the controller. Because I want to remove the new version if (!$this->config->getReviewSkipModeration()) {...} What changes should be made in the controller? :
$commentStatus = true;
if($reviewPoints < $this->config->getAutoPublishStars()){
$commentStatus = !$commentStatus;
}
if (!$this->config->getReviewSkipModeration()) {
$commentStatus = false;
}
$create = [
'productId' => $sArticleId,
'customerId' => $customer->getCustomerId(),
'salesChannelId' => $salesChannelContext->getSalesChannel()->getId(),
'languageId' => $salesChannelContext->getContext()->getLanguageId(),
'externalUser' => $this->anonymizeName($isAnon, $customer),
'externalEmail' => $orderNumberAsEmail,
'title' => $commentHeadline,
'content' => $commentContent,
'points' => $reviewPoints,
'status' => $commentStatus,
];
I wish to migrate the boolean configuration functionality to the new version, avoid changing the default behavior for existing users during updates to a multi-select configuration with the following options. How to use "None" in config.xml to controller? Can I access the "None" option the same way I access "0" in the controller? :
In my update method, how to migrate the data correctly:
My plug-in update function is as follows:
public function update(UpdateContext $context): void {
parent::update($context);
}
But I'm not sure how to handle the mapping. Any help would be greatly appreciated!
If I understand correctly, you should first change the update method, you can check whether the old boolean configuration is true or false, and then map it to the corresponding new multi-select configuration
public function update(UpdateContext $context): void { parent::update($context); $previousConfigVal = $this->config->getReviewSkipModeration(); $newConfigVal = $previousConfigVal ? 0 : 'None'; $this->config->setAutoPublishStars($newConfigVal); }Afterwards, if you're using PHP 8, you can use a simple [switch][1] or [match][2] to get different options
$autoPublishStars = $this->config->getAutoPublishStars(); switch ($autoPublishStars) { case 'None': $commentStatus = false; break; case '0': $commentStatus = false; break; case '1': $commentStatus = ($reviewPoints >= 1); break; case '2': $commentStatus = ($reviewPoints >= 2); break; case '3': $commentStatus = ($reviewPoints >= 3); break; case '4': $commentStatus = ($reviewPoints >= 4); break; case '5': $commentStatus = ($reviewPoints >= 5); break; default: $commentStatus = true; }I think this might actually work for you, if not we can brainstorm together to find a solution [1]: https://www.php.net/manual/ fr/control-structs.switch.php [2]: https://www.php.net/manual/ en/control-structs.match.php