Querying Nodes

You can query Nodes within a Vizy field using our query engine. It aims to be similar to Element Queries, and how you would query Matrix blocks. When you query a Vizy field, don't forget you're querying Nodes, which include Paragraph, Images and more - not just Vizy Blocks.

Examples

Let's look at a few example use-cases.

Fetch Nodes

Fetch all paragraph nodes in a field:

{% set paragraphs = entry.vizyField.query().where({ type: 'paragraph' }).all() %}

{# Alternative syntax #}
{% set paragraphs = entry.vizyField.query().where([ '=', 'type', 'paragraph' ]).all() %}

Fetch all Vizy Block and paragraph nodes in a field:

{% set blocks = entry.vizyField.query().where({ type: ['vizyBlock', 'paragraph'] }).all() %}

Fetch all Vizy Block nodes for a given handle:

{% set blocks = entry.vizyField.query().where({ type: 'vizyBlock', handle: 'textBlock' }).all() %}

Fetch all nodes that are not a paragraph node:

{% set blocks = entry.vizyField.query().where([ '!=', 'type', 'paragraph' ]).all() %}

{# Alternative syntax #}
{% set blocks = entry.vizyField.query().where([ 'not', { type: 'paragraph' } ]).all() %}

Limit

Return the first 2 nodes.

{% set nodes = entry.vizyField.query().limit(2).all() %}

Count

Return the total count of nodes in a field:

{# For all types of nodes #}
{{ entry.vizyField.query().count() }}

{# For all Vizy Block nodes #}
{{ entry.vizyField.query().where({ type: 'vizyBlock' }).count() }}

{# For all image nodes #}
{{ entry.vizyField.query().where({ type: 'image' }).count() }}

Order By

Return all nodes ordered by their type.

{% set paragraphs = entry.vizyField.query().orderBy('type DESC').all() %}

Return all Vizy Block nodes of type textBlock ordered by a plainText field:

{% set paragraphs = entry.vizyField.query().where({ type: 'vizyBlock', handle: 'textBlock' }).orderBy('plainText DESC').all() %}

Enabled

By default, only enabled Vizy blocks will be returned in a query. You can control this with enabled. This has no effect on other node types like Paragraph, Image, etc.

{# Return all blocks that are enabled _only_ (default) #}
{% set blocks = entry.vizyField.query().where({ enabled: true }).all() %}

{# Return all blocks that are disabled _only_ #}
{% set blocks = entry.vizyField.query().where({ enabled: false }).all() %}

{# Return all blocks that are both enabled and disabled #}
{% set blocks = entry.vizyField.query().where({ enabled: null }).all() %}

Fields

Return all Vizy Block nodes of type textBlock with a plainText field with a value equal to 123:

{% set blocks = entry.vizyField.query()
    .where({ type: 'vizyBlock', handle: 'textBlock', plainText: '123' })
    .all() %}

{# Alternative syntax #}
{% set blocks = entry.vizyField.query()
    .where({ type: 'vizyBlock', handle: 'textBlock' })
    .andWhere(['=', 'plainText', '123'])
    .all() %}

Return all Vizy Block nodes of type textBlock with a number field with a value greater than 10:

{% set blocks = entry.vizyField.query()
    .where({ type: 'vizyBlock', handle: 'textBlock' })
    .andWhere(['>', 'number', 10])
    .all() %}

Return all Vizy Block nodes of type textBlock with a date field with a value between 7 days ago and now:

{% set blocks = entry.vizyField.query()
    .where({ type: 'vizyBlock', handle: 'textBlock' })
    .andWhere(['between', 'date', now | date_modify('-7 day'), now])
    .all() %}

Available Methods

You can use the below methods on a query to either filter or fetch nodes.

OptionDescription
where()Used to filter items based on params.
andWhere()In addition to where(), filter using and matching conditions.
orWhere()In addition to where(), filter using or matching conditions.
filterWhere()See where().
andFilterWhere()See andWhere().
orFilterWhere()See orWhere().
exists()Whether any items match the query.
limit()Limit the number of nodes returned.
orderBy()Return nodes ordered by a property.
count()Return the total count for the query.
one()Return the first matching node.
all()Return a collection of all matching nodes.

Available Params

You can use the following operators in the above methods like where(), andWhere(),

ParamExample
notwhere(['not', ['username' => 'admin']])
andwhere(['and', { username: 'admin' }, { id: 3}])
orwhere(['or', { username: 'admin' }, { id: 3}])
betweenwhere(['between', 'id', 1, 2])
not betweenwhere(['not between', 'id', 1, 2])
inwhere(['in', 'id', [1, 3]])
not inwhere(['not in', 'id', [1, 3]])
likewhere(['like', 'username', 'admin'])
not likewhere(['not like', 'username', 'admin'])
or likewhere(['or like', 'username', 'admin'])
or not likewhere(['or not like', 'username', 'admin'])
>where(['>', 'id', 1])
<where(['<', 'id', 2])
>=where(['>=', 'id', 1])
<=where(['<=', 'id', 2])
=where(['=', 'id', 1])
!=where(['!=', 'id', 1])

Previous ← Modify Nodes Next Modular Templates →

Get started with Vizy

Available for Craft 4. Get it from the plugin store.