Skip to content

Commit d257301

Browse files
[Bug]: Fix Inherited classification store data now displays correctly in grid view (#1042)
* inherited classification store data now displays correctly in grid view * add new function return type * refactor and fix table inheritance * refactor static to self --------- Co-authored-by: cameronfromtorq <[email protected]>
1 parent 6482d84 commit d257301

File tree

1 file changed

+53
-23
lines changed

1 file changed

+53
-23
lines changed

src/Service/GridData/DataObject.php

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Pimcore\Model\DataObject\Service;
2727
use Pimcore\Tool\Admin as AdminTool;
2828
use Pimcore\Tool\Session;
29+
use Pimcore\Model\DataObject\ClassDefinition\Data;
2930
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
3031

3132
/**
@@ -185,21 +186,27 @@ public static function getData(AbstractObject $object, array $fields = null, str
185186
}
186187

187188
// because the key for the classification store has not a direct getter, you have to check separately if the data is inheritable
188-
if (str_starts_with($key, '~')) {
189-
$curClassAttributeValue = $data[$key] ?? null;
190-
$isValueEmpty = is_array($curClassAttributeValue) ? empty($curClassAttributeValue['value'] ?? null) : empty($curClassAttributeValue);
191-
192-
if ($isValueEmpty) {
193-
$type = $keyParts[1];
194-
195-
if ($type === 'classificationstore') {
196-
if (!empty($inheritedData = self::getInheritedData($object, $key, $requestedLanguage))) {
197-
$data[$dataKey] = $inheritedData['value'];
198-
$data['inheritedFields'][$dataKey] = ['inherited' => $inheritedData['parent']->getId() != $object->getId(), 'objectid' => $inheritedData['parent']->getId()];
199-
}
189+
if (
190+
str_starts_with($key, '~') &&
191+
($keyParts[1] ?? null) === 'classificationstore'
192+
) {
193+
$fieldDef = self::getClassificationStoreFieldDefinition($key);
194+
$value = self::normalizeValue($data[$key]);
195+
196+
if ($fieldDef->isEmpty($value)) {
197+
$inheritedData = static::getInheritedData($object, $key, $requestedLanguage);
198+
199+
if (!empty($inheritedData)) {
200+
$parent = $inheritedData['parent'];
201+
$data[$dataKey] = $inheritedData['value'];
202+
$data['inheritedFields'][$dataKey] = [
203+
'inherited' => $parent->getId() !== $object->getId(),
204+
'objectid' => $parent->getId(),
205+
];
200206
}
201207
}
202208
}
209+
203210
if ($needLocalizedPermissions) {
204211
if (!$user->isAdmin()) {
205212
$locale = \Pimcore::getContainer()->get(LocaleServiceInterface::class)->findLocale();
@@ -358,17 +365,9 @@ protected static function getInheritedData(Concrete $object, string $key, string
358365
return [];
359366
}
360367

361-
$inheritedValue = self::getStoreValueForObject($parent, $key, $requestedLanguage);
362-
if (
363-
(!is_array($inheritedValue) && $inheritedValue !== null) ||
364-
(
365-
is_array($inheritedValue) &&
366-
(
367-
array_is_list($inheritedValue) || //for table field types
368-
!empty($inheritedValue['value'] ?? null)
369-
)
370-
)
371-
) {
368+
$inheritedValue = self::normalizeValue(self::getStoreValueForObject($parent, $key, $requestedLanguage));
369+
370+
if (!static::getClassificationStoreFieldDefinition($key)->isEmpty($inheritedValue)) {
372371
return [
373372
'parent' => $parent,
374373
'value' => $inheritedValue,
@@ -377,4 +376,35 @@ protected static function getInheritedData(Concrete $object, string $key, string
377376

378377
return self::getInheritedData($parent, $key, $requestedLanguage);
379378
}
379+
380+
/**
381+
* The actual data could be a plain array for tables,
382+
* an associative array for RGB or Quantity Value where the value is a key.
383+
*
384+
*/
385+
private static function normalizeValue(mixed $data): mixed
386+
{
387+
if (is_array($data)){
388+
if (array_is_list($data)) {
389+
return $data;
390+
}
391+
if (array_key_exists('value', $data)){
392+
return $data['value'];
393+
}
394+
}
395+
return $data;
396+
}
397+
398+
protected static function getClassificationStoreFieldDefinition(string $key): Data
399+
{
400+
$keyParts = explode('~', $key);
401+
$groupKeyId = explode('-', $keyParts[3]);
402+
403+
$keyid = (int) $groupKeyId[1];
404+
405+
$keyConfig = Model\DataObject\Classificationstore\KeyConfig::getById($keyid);
406+
$type = $keyConfig->getType();
407+
$definition = json_decode($keyConfig->getDefinition(), true);
408+
return \Pimcore\Model\DataObject\Classificationstore\Service::getFieldDefinitionFromJson($definition, $type);
409+
}
380410
}

0 commit comments

Comments
 (0)