diff --git a/system/src/Grav/Framework/Collection/AbstractIndexCollection.php b/system/src/Grav/Framework/Collection/AbstractIndexCollection.php index 0e4fae22d..7bb5af620 100644 --- a/system/src/Grav/Framework/Collection/AbstractIndexCollection.php +++ b/system/src/Grav/Framework/Collection/AbstractIndexCollection.php @@ -110,7 +110,7 @@ abstract class AbstractIndexCollection implements CollectionInterface */ public function removeElement($element) { - $key = $this->isAllowedElement($element) ? $element->getKey() : null; + $key = $this->isAllowedElement($element) ? $this->getCurrentKey($element) : null; if (!$key || !isset($this->entries[$key])) { return false; @@ -178,7 +178,7 @@ abstract class AbstractIndexCollection implements CollectionInterface */ public function contains($element) { - $key = $this->isAllowedElement($element) ? $element->getKey() : null; + $key = $this->isAllowedElement($element) ? $this->getCurrentKey($element) : null; return $key && isset($this->entries[$key]); } @@ -196,7 +196,7 @@ abstract class AbstractIndexCollection implements CollectionInterface */ public function indexOf($element) { - $key = $this->isAllowedElement($element) ? $element->getKey() : null; + $key = $this->isAllowedElement($element) ? $this->getCurrentKey($element) : null; return $key && isset($this->entries[$key]) ? $key : null; } @@ -246,10 +246,6 @@ abstract class AbstractIndexCollection implements CollectionInterface throw new \InvalidArgumentException('Invalid argument $value'); } - if ($key !== $value->getKey()) { - $value->setKey($key); - } - $this->entries[$key] = $this->getElementMeta($value); } @@ -262,7 +258,7 @@ abstract class AbstractIndexCollection implements CollectionInterface throw new \InvalidArgumentException('Invalid argument $element'); } - $this->entries[$element->getKey()] = $this->getElementMeta($element); + $this->entries[$this->getCurrentKey($element)] = $this->getElementMeta($element); return true; } @@ -477,6 +473,11 @@ abstract class AbstractIndexCollection implements CollectionInterface $this->entries = $entries; } + protected function getCurrentKey($element) + { + return $element->getKey(); + } + /** * @param string $key * @param mixed $value diff --git a/system/src/Grav/Framework/Flex/FlexCollection.php b/system/src/Grav/Framework/Flex/FlexCollection.php index 23954ee1d..63528d1ed 100644 --- a/system/src/Grav/Framework/Flex/FlexCollection.php +++ b/system/src/Grav/Framework/Flex/FlexCollection.php @@ -293,6 +293,11 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface return $this->getFlexDirectory()->getIndex($this->getKeys(), $this->getKeyField()); } + public function getCollection() + { + return $this; + } + /** * {@inheritdoc} * @see FlexCollectionInterface::render() diff --git a/system/src/Grav/Framework/Flex/FlexIndex.php b/system/src/Grav/Framework/Flex/FlexIndex.php index 3b6ac2891..9d3e020b3 100644 --- a/system/src/Grav/Framework/Flex/FlexIndex.php +++ b/system/src/Grav/Framework/Flex/FlexIndex.php @@ -258,6 +258,11 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde return $this; } + public function getCollection() + { + return $this->loadCollection(); + } + /** * {@inheritdoc} * @see FlexCollectionInterface::render() @@ -359,7 +364,7 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde } // Order by current field. - if ($ordering === 'DESC') { + if (strtoupper($ordering) === 'DESC') { arsort($search, SORT_NATURAL); } else { asort($search, SORT_NATURAL); @@ -579,6 +584,22 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde return $object->getMetaData(); } + protected function getCurrentKey($element) + { + $keyField = $this->getKeyField(); + if ($keyField === 'storage_key') { + return $element->getStorageKey(); + } + if ($keyField === 'flex_key') { + return $element->getFlexKey(); + } + if ($keyField === 'key') { + return $element->getKey(); + } + + return $element->getKey(); + } + /** * @param FlexStorageInterface $storage * @param array $index Saved index diff --git a/system/src/Grav/Framework/Flex/FlexObject.php b/system/src/Grav/Framework/Flex/FlexObject.php index 4a316bfeb..3e8826f8f 100644 --- a/system/src/Grav/Framework/Flex/FlexObject.php +++ b/system/src/Grav/Framework/Flex/FlexObject.php @@ -630,12 +630,15 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface $result = $storage->replaceRows([$key => $this->prepareStorage()]); $value = reset($result); - $storageKey = (string)key($result); + $meta = $value['__META'] ?? null; + if ($meta) { + $this->_meta = $meta; + } + + $storageKey = $meta['storage_key'] ?? (string)key($result); if ($value && $storageKey) { $this->setStorageKey($storageKey); - if (!$this->hasKey()) { - $this->setKey($storageKey); - } + $this->setKey($meta['key'] ?? $storageKey); } // FIXME: For some reason locator caching isn't cleared for the file, investigate! @@ -810,6 +813,8 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface { return [ 'type:private' => $this->getFlexType(), + 'storage_key:protected' => $this->getStorageKey(), + 'storage_timestamp:protected' => $this->getTimestamp(), 'key:private' => $this->getKey(), 'elements:private' => $this->getElements(), 'storage:private' => $this->getStorage()