Flex
Warning
TIP: Complete list of methods can be found from Customizing Flex Objects section.
count()
count(): int Count the number of directories registered to Flex.
Returns:
intNumber of Directories
{% set flex = grav.get('flex') %}
Flex has {{ flex.count() }} enabled directories.
use Grav\Common\Grav;
use Grav\Framework\Flex\Interfaces\FlexInterface;
/** @var FlexInterface $flex */
$flex = Grav::instance()->get('flex');
/** @var int $count */
$count = $flex->count();
getDirectories()
getDirectories( [names] ): array Get list of directories.
Parameters:
- names Optional: List of directory names (
array)
Returns:
arraylist of Directories
Warning
TIP: If no list of names was provided, method returns all directories registered to Flex.
{% set flex = grav.get('flex') %}
{# Get all directories #}
{% set directories = flex.directories() %}
{# Get listed directories #}
{% set listed_directories = flex.directories(['contacts', 'phonebook']) %}
{# Do something with the directories #}
use Grav\Common\Grav;
use Grav\Framework\Flex\Interfaces\FlexInterface;
use Grav\Framework\Flex\Interfaces\FlexDirectoryInterface;
/** @var FlexInterface $flex */
$flex = Grav::instance()->get('flex');
/** @var FlexDirectoryInterface[] $directories */
$directories = $flex->getDirectories();
// = ['contacts' => FlexDirectory, ...]
/** @var FlexDirectoryInterface[] $directories */
$listedDirectories = $flex->getDirectories(['contacts', 'phonebook']);
// = ['contacts' => FlexDirectory]
/** @var array<FlexDirectoryInterface|null> $directories */
$listedDirectoriesWithMissing = $flex->getDirectories(['contacts', 'phonebook'], true);
// = ['contacts' => FlexDirectory, 'phonebook' => null]
Warning
TIP: You may want to make sure you return only the directories you want to.
hasDirectory()
hasDirectory( name ): bool: Check if directory exists.
Parameters:
- name Name of the directory (
string)
Returns:
boolTrue if found, false otherwise
{% set flex = grav.get('flex') %}
Flex has {{ not flex.hasDirectory('contacts') ? 'not' }} contacts directory.
use Grav\Common\Grav;
use Grav\Framework\Flex\Interfaces\FlexInterface;
/** @var FlexInterface $flex */
$flex = Grav::instance()->get('flex');
/** @var bool $exists */
$exists = $flex->hasDirectory('contacts');
getDirectory()
getDirectory( name ): Directory | null Get a directory, returns null if it was not found.
Parameters:
- name Name of the directory (
string)
Returns:
- Directory (
object) nullDirectory not found
{% set flex = grav.get('flex') %}
{# Get contacts directory (null if not found) #}
{% set directory = flex.directory('contacts') %}
{# Do something with the contacts directory #}
use Grav\Common\Grav;
use Grav\Framework\Flex\Interfaces\FlexInterface;
use Grav\Framework\Flex\Interfaces\FlexDirectoryInterface;
/** @var FlexInterface $flex */
$flex = Grav::instance()->get('flex');
/** @var FlexDirectoryInterface|null $directory */
$directory = $flex->getDirectory('contacts');
if ($directory) {
// Directory exists, do something with it...
}
Note
Check what you can do with Flex Directory
getObject()
getObject( id, directory ): Object | null Get an object, returns null if it was not found.
Parameters:
- id ID of the object (
string) - directory Name of the directory (
string)
Returns:
- Object (
object) nullObject not found
{% set flex = grav.get('flex') %}
{% set contact = flex.object('ki2ts4cbivggmtlj', 'contacts') %}
{# Do something #}
{% if contact %}
{# Got Bruce Day #}
{{ contact.first_name|e }} {{ contact.last_name|e }} has a website: {{ contact.website|e }}
{% else %}
Oops, contact has been removed!
{% endif %}
use Grav\Common\Grav;
use Grav\Framework\Flex\Interfaces\FlexInterface;
use Grav\Framework\Flex\Interfaces\FlexObjectInterface;
/** @var FlexInterface $flex */
$flex = Grav::instance()->get('flex');
/** @var FlexObjectInterface|null $object */
$object = $flex->getObject('ki2ts4cbivggmtlj', 'contacts');
if ($object) {
// Object exists, do something with it...
}
Note
Check what you can do with Flex Object
getCollection()
getCollection( directory ): Collection | null Get collection, returns null if it was not found.
Parameters:
directoryName of the directory (string)
Returns:
- Collection (
object) nullDirectory not found
{% set flex = grav.get('flex') %}
{% set contacts = flex.collection('contacts') %}
{# Do something #}
<h2>Ten random contacts:</h2>
<ul>
{% for contact in contacts.filterBy({published: true}).shuffle().limit(0, 10) %}
<li>{{ contact.first_name|e }} {{ contact.last_name|e }}</li>
{% endfor %}
</ul>
use Grav\Common\Grav;
use Grav\Framework\Flex\Interfaces\FlexCollectionInterface;
use Grav\Framework\Flex\Interfaces\FlexInterface;
/** @var FlexInterface $flex */
$flex = Grav::instance()->get('flex');
/** @var FlexCollectionInterface|null $collection */
$collection = $flex->getCollection('contacts');
if ($collection) {
// Collection exists, do something with it...
}
Note
Check what you can do with Flex Collection