mirror of
https://github.com/getgrav/grav.git
synced 2026-03-05 03:51:50 +01:00
Some bug fixes in Flex collection / index
This commit is contained in:
@@ -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';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,7 +163,7 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde
|
||||
*/
|
||||
public function getKeyField() : string
|
||||
{
|
||||
return $this->_keyField;
|
||||
return $this->_keyField ?? 'key';
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user