Fix bug: Ensure data['tags'] is normalized
[MAILPOET-5515]
This commit is contained in:
@@ -304,16 +304,32 @@ class SubscriberSaveController {
|
|||||||
|
|
||||||
private function updateTags(array $data, SubscriberEntity $subscriber): void {
|
private function updateTags(array $data, SubscriberEntity $subscriber): void {
|
||||||
$removedTags = [];
|
$removedTags = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* $data['tags'] is either an array of arrays containing name, id etc. of the tag or an array of strings - the names
|
||||||
|
* of the tag.
|
||||||
|
*
|
||||||
|
* Therefore we map it to be only an array of strings, containing the names of the tag.
|
||||||
|
*/
|
||||||
|
$tags = array_map(
|
||||||
|
function($tag): string {
|
||||||
|
if (is_array($tag)) {
|
||||||
|
return array_key_exists('name', $tag) ? (string)$tag['name'] : '';
|
||||||
|
}
|
||||||
|
return (string)$tag;
|
||||||
|
},
|
||||||
|
(array)$data['tags']
|
||||||
|
);
|
||||||
foreach ($subscriber->getSubscriberTags() as $subscriberTag) {
|
foreach ($subscriber->getSubscriberTags() as $subscriberTag) {
|
||||||
$tag = $subscriberTag->getTag();
|
$tag = $subscriberTag->getTag();
|
||||||
if (!$tag || !in_array($tag->getName(), $data['tags'], true)) {
|
if (!$tag || !in_array($tag->getName(), $tags, true)) {
|
||||||
$subscriber->getSubscriberTags()->removeElement($subscriberTag);
|
$subscriber->getSubscriberTags()->removeElement($subscriberTag);
|
||||||
$removedTags[] = $subscriberTag;
|
$removedTags[] = $subscriberTag;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$newlyAddedTags = [];
|
$newlyAddedTags = [];
|
||||||
foreach ($data['tags'] as $tagName) {
|
foreach ($tags as $tagName) {
|
||||||
$tag = $this->tagRepository->createOrUpdate(['name' => $tagName]);
|
$tag = $this->tagRepository->createOrUpdate(['name' => $tagName]);
|
||||||
$subscriberTag = $subscriber->getSubscriberTag($tag);
|
$subscriberTag = $subscriber->getSubscriberTag($tag);
|
||||||
if (!$subscriberTag) {
|
if (!$subscriberTag) {
|
||||||
|
Reference in New Issue
Block a user