Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 52 additions & 28 deletions src/DataObject/Data/Adapter/ClassificationStoreAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public function getDataForSetter(
$store = $data[$key];

if ($isPatch) {
$elementStore = $this->normalize($element->get($key), $fieldDefinition);
$elementStore = $this->handleNormalize($element->get($key), $fieldDefinition, $user);
$store['activeGroups'] = $this->getActiveGroupsFromStore($store);
$store = array_replace_recursive($elementStore, $store);
}

Expand All @@ -116,33 +117,7 @@ public function normalize(
mixed $value,
Data $fieldDefinition
): ?array {
if (!$value instanceof ClassificationstoreModel ||
!$fieldDefinition instanceof ClassificationstoreDefinition
) {
return null;
}

$validLanguages = $this->getValidLanguages($value->getObject(), $fieldDefinition->isLocalized());
$resultItems = [];

$resultItems['activeGroups'] = $value->getActiveGroups();
$resultItems['groupCollectionMapping'] = $value->getGroupCollectionMappings();

foreach ($this->getActiveGroupsConfig($resultItems['activeGroups']) as $groupId => $groupConfig) {
$resultItems[$groupId] = [];
$keys = $this->getClassificationStoreKeysFromGroup($groupId);
foreach ($validLanguages as $validLanguage) {
foreach ($keys as $key) {
$normalizedValue = $this->getNormalizedValue($value, $groupId, $key, $validLanguage);

if ($normalizedValue !== null) {
$resultItems[$groupId][$validLanguage][$key->getKeyId()] = $normalizedValue;
}
}
}
}

return $resultItems;
return $this->handleNormalize($value, $fieldDefinition);
}

public function getFieldInheritance(
Expand Down Expand Up @@ -214,6 +189,45 @@ public function getPreviewFieldData(
return $data;
}

private function handleNormalize(
mixed $value,
Data $fieldDefinition,
?UserInterface $user = null
): ?array {
if (!$value instanceof ClassificationstoreModel ||
!$fieldDefinition instanceof ClassificationstoreDefinition
) {
return null;
}

$validLanguages = $this->getValidLanguages(
$value->getObject(),
$fieldDefinition->isLocalized(),
ElementPermissions::LANGUAGE_VIEW_PERMISSIONS,
$user
);
$resultItems = [];

$resultItems['activeGroups'] = $value->getActiveGroups();
$resultItems['groupCollectionMapping'] = $value->getGroupCollectionMappings();

foreach ($this->getActiveGroupsConfig($resultItems['activeGroups']) as $groupId => $groupConfig) {
$resultItems[$groupId] = [];
$keys = $this->getClassificationStoreKeysFromGroup($groupId);
foreach ($validLanguages as $validLanguage) {
foreach ($keys as $key) {
$normalizedValue = $this->getNormalizedValue($value, $groupId, $key, $validLanguage);

if ($normalizedValue !== null) {
$resultItems[$groupId][$validLanguage][$key->getKeyId()] = $normalizedValue;
}
}
}
}

return $resultItems;
}

/**
* @throws NotFoundException
*/
Expand Down Expand Up @@ -402,6 +416,16 @@ private function getActiveGroupsConfig(array $activeGroups): array
return $groups;
}

private function getActiveGroupsFromStore(array $store): array
{
$activeGroups = [];
foreach ($store as $groupId => $groupData) {
$activeGroups[$groupId] = true;
}

return $activeGroups;
}

/**
* @return KeyGroupRelation[]
*/
Expand Down
Loading