diff --git a/system/src/Grav/Framework/Flex/FlexCollection.php b/system/src/Grav/Framework/Flex/FlexCollection.php index 9733a3c92..4a594f078 100644 --- a/system/src/Grav/Framework/Flex/FlexCollection.php +++ b/system/src/Grav/Framework/Flex/FlexCollection.php @@ -28,6 +28,9 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface /** @var FlexDirectory */ private $_flexDirectory; + /** @var string */ + private $_keyField; + /** * @return array */ @@ -81,13 +84,17 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface * instance should be created when constructor semantics have changed. * * @param array $elements Elements. + * @package string|null $keyField * * @return static * @throws \InvalidArgumentException */ - protected function createFrom(array $elements) + protected function createFrom(array $elements, $keyField = null) { - return new static($elements, $this->_flexDirectory); + $collection = new static($elements, $this->_flexDirectory); + $collection->setKeyField($keyField); + + return $collection; } /** @@ -237,19 +244,6 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface return $object ? $object->getMetaData() : []; } - /** - * @param string $flexKey - * @return mixed|null - */ - public function locate(string $flexKey) - { - $keys = $this->getFlexKeys(); - - $key = $keys[$flexKey] ?? null; - - return $key ? $this->get($key) : null; - } - /** * @return string */ @@ -300,7 +294,15 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface } } - return $this->createFrom($entries); + return $this->createFrom($entries, $keyField); + } + + /** + * @return string + */ + public function getKeyField() : string + { + return $this->_keyField ?? 'key'; } /** @@ -350,6 +352,16 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface return $elements; } + public function __debugInfo() + { + return [ + 'type' => $this->getType(), + 'key' => $this->getKey(), + 'entries_key' => $this->getKeyField(), + 'objects' => $this->jsonSerialize() + ]; + } + /** * @param string $layout * @return \Twig_Template @@ -385,4 +397,9 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface return $flex->getDirectory($type); } + + protected function setKeyField($keyField = null) + { + $this->_keyField = $keyField ?? 'storage_key'; + } } diff --git a/system/src/Grav/Framework/Flex/FlexIndex.php b/system/src/Grav/Framework/Flex/FlexIndex.php index 5fd7cfaee..c6e44cb07 100644 --- a/system/src/Grav/Framework/Flex/FlexIndex.php +++ b/system/src/Grav/Framework/Flex/FlexIndex.php @@ -163,7 +163,7 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde */ public function getKeyField() : string { - return $this->_keyField; + return $this->_keyField ?? 'key'; } /**