Skip to content

Latest commit

 

History

History
1058 lines (702 loc) · 20.4 KB

File metadata and controls

1058 lines (702 loc) · 20.4 KB

Matrix Block Queries

You can fetch Matrix blocks in your templates or PHP code using Matrix block queries.

::: code

{# Create a new Matrix block query #}
{% set myMatrixBlockQuery = craft.matrixBlocks() %}
// Create a new Matrix block query
$myMatrixBlockQuery = \craft\elements\MatrixBlock::find();

:::

Once you’ve created a Matrix block query, you can set parameters on it to narrow down the results, and then execute it by calling .all(). An array of MatrixBlock objects will be returned.

::: tip See Introduction to Element Queries to learn about how element queries work. :::

Example

We can display content from all the Matrix blocks of an element by doing the following:

  1. Create a Matrix block query with craft.matrixBlocks().
  2. Set the owner, fieldId, and type parameters on it.
  3. Fetch the Matrix blocks with .all().
  4. Loop through the Matrix blocks using a for tag to output the contents.
{# Create a Matrix block query with the 'owner', 'fieldId', and 'type' parameters #}
{% set myMatrixBlockQuery = craft.matrixBlocks()
    .owner(myEntry)
    .fieldId(10)
    .type('text') %}

{# Fetch the Matrix blocks #}
{% set matrixBlocks = myMatrixBlockQuery.all() %}

{# Display their contents #}
{% for block in matrixBlocks %}
    <p>{{ block.text }}</p>
{% endfor %}

::: warning In order for the returned Matrix block(s) to be populated with their custom field content, you will need to either set the fieldId or id parameter. :::

Parameters

Matrix block queries support the following parameters:

anyStatus

Clears out the status and enabledForSite() parameters.

::: code

{# Fetch all Matrix blocks, regardless of status #}
{% set MatrixBlocks = craft.matrixBlocks()
    .anyStatus()
    .all() %}
// Fetch all Matrix blocks, regardless of status
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->anyStatus()
    ->all();

:::

asArray

Causes the query to return matching Matrix blocks as arrays of data, rather than MatrixBlock objects.

::: code

{# Fetch Matrix blocks as arrays #}
{% set MatrixBlocks = craft.matrixBlocks()
    .asArray()
    .all() %}
// Fetch Matrix blocks as arrays
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->asArray()
    ->all();

:::

dateCreated

Narrows the query results based on the Matrix blocks’ creation dates.

Possible values include:

Value Fetches Matrix blocks…
'>= 2018-04-01' that were created on or after 2018-04-01.
'< 2018-05-01' that were created before 2018-05-01
['and', '>= 2018-04-04', '< 2018-05-01'] that were created between 2018-04-01 and 2018-05-01.

::: code

{# Fetch Matrix blocks created last month #}
{% set start = date('first day of last month')|atom %}
{% set end = date('first day of this month')|atom %}

{% set MatrixBlocks = craft.matrixBlocks()
    .dateCreated(['and', ">= #{start}", "< #{end}"])
    .all() %}
// Fetch Matrix blocks created last month
$start = (new \DateTime('first day of last month'))->format(\DateTime::ATOM);
$end = (new \DateTime('first day of this month'))->format(\DateTime::ATOM);

$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->dateCreated(['and', ">= {$start}", "< {$end}"])
    ->all();

:::

dateUpdated

Narrows the query results based on the Matrix blocks’ last-updated dates.

Possible values include:

Value Fetches Matrix blocks…
'>= 2018-04-01' that were updated on or after 2018-04-01.
'< 2018-05-01' that were updated before 2018-05-01
['and', '>= 2018-04-04', '< 2018-05-01'] that were updated between 2018-04-01 and 2018-05-01.

::: code

{# Fetch Matrix blocks updated in the last week #}
{% set lastWeek = date('1 week ago')|atom %}

{% set MatrixBlocks = craft.matrixBlocks()
    .dateUpdated(">= #{lastWeek}")
    .all() %}
// Fetch Matrix blocks updated in the last week
$lastWeek = (new \DateTime('1 week ago'))->format(\DateTime::ATOM);

$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->dateUpdated(">= {$lastWeek}")
    ->all();

:::

draftCreator

Narrows the query results to only drafts created by a given user.

Possible values include:

Value Fetches drafts…
1 created by the user with an ID of 1.
a \craft\elements\db\User object by the user represented by the object.

::: code

{# Fetch drafts by the current user #}
{% set MatrixBlocks = craft.matrixBlocks()
    .draftCreator(currentUser)
    .all() %}
// Fetch drafts by the current user
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->draftCreator(Craft::$app->user->identity)
    ->all();

:::

draftId

Narrows the query results based on the Matrix blocks’ draft’s ID (from the drafts table).

Possible values include:

Value Fetches drafts…
1 for the draft with an ID of 1.

::: code

{# Fetch a draft #}
{% set MatrixBlocks = craft.matrixBlocks()
    .draftId(10)
    .all() %}
// Fetch a draft
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->draftIf(10)
    ->all();

:::

draftOf

Narrows the query results to only drafts of a given Matrix block.

Possible values include:

Value Fetches drafts…
1 for the Matrix block with an ID of 1.
a MatrixBlock object for the Matrix block represented by the object.

::: code

{# Fetch drafts of the Matrix block #}
{% set MatrixBlocks = craft.matrixBlocks()
    .draftOf(myBlock)
    .all() %}
// Fetch drafts of the Matrix block
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->draftOf($myBlock)
    ->all();

:::

drafts

Narrows the query results to only drafts Matrix blocks.

::: code

{# Fetch a draft Matrix block #}
{% set MatrixBlocks = {twig-function}
    .drafts()
    .id(123)
    .one() %}
// Fetch a draft Matrix block
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->drafts()
    ->id(123)
    ->one();

:::

fieldId

Narrows the query results based on the field the Matrix blocks belong to, per the fields’ IDs.

Possible values include:

Value Fetches Matrix blocks…
1 in a field with an ID of 1.
'not 1' not in a field with an ID of 1.
[1, 2] in a field with an ID of 1 or 2.
['not', 1, 2] not in a field with an ID of 1 or 2.

::: code

{# Fetch Matrix blocks in the field with an ID of 1 #}
{% set MatrixBlocks = craft.matrixBlocks()
    .fieldId(1)
    .all() %}
// Fetch Matrix blocks in the field with an ID of 1
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->fieldId(1)
    ->all();

:::

fixedOrder

Causes the query results to be returned in the order specified by id.

::: code

{# Fetch Matrix blocks in a specific order #}
{% set MatrixBlocks = craft.matrixBlocks()
    .id([1, 2, 3, 4, 5])
    .fixedOrder()
    .all() %}
// Fetch Matrix blocks in a specific order
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->id([1, 2, 3, 4, 5])
    ->fixedOrder()
    ->all();

:::

id

Narrows the query results based on the Matrix blocks’ IDs.

Possible values include:

Value Fetches Matrix blocks…
1 with an ID of 1.
'not 1' not with an ID of 1.
[1, 2] with an ID of 1 or 2.
['not', 1, 2] not with an ID of 1 or 2.

::: code

{# Fetch the Matrix block by its ID #}
{% set MatrixBlock = craft.matrixBlocks()
    .id(1)
    .one() %}
// Fetch the Matrix block by its ID
$MatrixBlock = \craft\elements\MatrixBlock::find()
    ->id(1)
    ->one();

:::

::: tip This can be combined with fixedOrder if you want the results to be returned in a specific order. :::

ignorePlaceholders

Causes the query to return matching Matrix blocks as they are stored in the database, ignoring matching placeholder elements that were set by craft\services\Elements::setPlaceholderElement().

inReverse

Causes the query results to be returned in reverse order.

::: code

{# Fetch Matrix blocks in reverse #}
{% set MatrixBlocks = craft.matrixBlocks()
    .inReverse()
    .all() %}
// Fetch Matrix blocks in reverse
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->inReverse()
    ->all();

:::

limit

Determines the number of Matrix blocks that should be returned.

::: code

{# Fetch up to 10 Matrix blocks  #}
{% set MatrixBlocks = craft.matrixBlocks()
    .limit(10)
    .all() %}
// Fetch up to 10 Matrix blocks
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->limit(10)
    ->all();

:::

offset

Determines how many Matrix blocks should be skipped in the results.

::: code

{# Fetch all Matrix blocks except for the first 3 #}
{% set MatrixBlocks = craft.matrixBlocks()
    .offset(3)
    .all() %}
// Fetch all Matrix blocks except for the first 3
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->offset(3)
    ->all();

:::

orderBy

Determines the order that the Matrix blocks should be returned in.

::: code

{# Fetch all Matrix blocks in order of date created #}
{% set MatrixBlocks = craft.matrixBlocks()
    .orderBy('dateCreated asc')
    .all() %}
// Fetch all Matrix blocks in order of date created
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->orderBy('dateCreated asc')
    ->all();

:::

owner

Sets the ownerId and siteId parameters based on a given element.

::: code

{# Fetch Matrix blocks created for this entry #}
{% set MatrixBlocks = craft.matrixBlocks()
    .owner(myEntry)
    .all() %}
// Fetch Matrix blocks created for this entry
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->owner($myEntry)
    ->all();

:::

ownerId

Narrows the query results based on the owner element of the Matrix blocks, per the owners’ IDs.

Possible values include:

Value Fetches Matrix blocks…
1 created for an element with an ID of 1.
'not 1' not created for an element with an ID of 1.
[1, 2] created for an element with an ID of 1 or 2.
['not', 1, 2] not created for an element with an ID of 1 or 2.

::: code

{# Fetch Matrix blocks created for an element with an ID of 1 #}
{% set MatrixBlocks = craft.matrixBlocks()
    .ownerId(1)
    .all() %}
// Fetch Matrix blocks created for an element with an ID of 1
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->ownerId(1)
    ->all();

:::

preferSites

If unique is set, this determines which site should be selected when querying multi-site elements.

For example, if element “Foo” exists in Site A and Site B, and element “Bar” exists in Site B and Site C, and this is set to ['c', 'b', 'a'], then Foo will be returned for Site C, and Bar will be returned for Site B.

If this isn’t set, then preference goes to the current site.

::: code

{# Fetch unique Matrix blocks from Site A, or Site B if they don’t exist in Site A #}
{% set MatrixBlocks = craft.matrixBlocks()
    .site('*')
    .unique()
    .preferSites(['a', 'b'])
    .all() %}
// Fetch unique Matrix blocks from Site A, or Site B if they don’t exist in Site A
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->site('*')
    ->unique()
    ->preferSites(['a', 'b'])
    ->all();

:::

relatedTo

Narrows the query results to only Matrix blocks that are related to certain other elements.

See Relations for a full explanation of how to work with this parameter.

::: code

{# Fetch all Matrix blocks that are related to myCategory #}
{% set MatrixBlocks = craft.matrixBlocks()
    .relatedTo(myCategory)
    .all() %}
// Fetch all Matrix blocks that are related to $myCategory
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->relatedTo($myCategory)
    ->all();

:::

revisionCreator

Narrows the query results to only revisions created by a given user.

Possible values include:

Value Fetches revisions…
1 created by the user with an ID of 1.
a \craft\elements\db\User object by the user represented by the object.

::: code

{# Fetch revisions by the current user #}
{% set MatrixBlocks = craft.matrixBlocks()
    .revisionCreator(currentUser)
    .all() %}
// Fetch revisions by the current user
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->revisionCreator(Craft::$app->user->identity)
    ->all();

:::

revisionId

Narrows the query results based on the Matrix blocks’ revision’s ID (from the revisions table).

Possible values include:

Value Fetches revisions…
1 for the revision with an ID of 1.

::: code

{# Fetch a revision #}
{% set MatrixBlocks = craft.matrixBlocks()
    .revisionId(10)
    .all() %}
// Fetch a revision
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->revisionIf(10)
    ->all();

:::

revisionOf

Narrows the query results to only revisions of a given Matrix block.

Possible values include:

Value Fetches revisions…
1 for the Matrix block with an ID of 1.
a MatrixBlock object for the Matrix block represented by the object.

::: code

{# Fetch revisions of the Matrix block #}
{% set MatrixBlocks = craft.matrixBlocks()
    .revisionOf(myBlock)
    .all() %}
// Fetch revisions of the Matrix block
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->revisionOf($myBlock)
    ->all();

:::

revisions

Narrows the query results to only revision Matrix blocks.

::: code

{# Fetch a revision Matrix block #}
{% set MatrixBlocks = {twig-function}
    .revisions()
    .id(123)
    .one() %}
// Fetch a revision Matrix block
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->revisions()
    ->id(123)
    ->one();

:::

search

Narrows the query results to only Matrix blocks that match a search query.

See Searching for a full explanation of how to work with this parameter.

::: code

{# Get the search query from the 'q' query string param #}
{% set searchQuery = craft.app.request.getQueryParam('q') %}

{# Fetch all Matrix blocks that match the search query #}
{% set MatrixBlocks = craft.matrixBlocks()
    .search(searchQuery)
    .all() %}
// Get the search query from the 'q' query string param
$searchQuery = \Craft::$app->request->getQueryParam('q');

// Fetch all Matrix blocks that match the search query
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->search($searchQuery)
    ->all();

:::

site

Determines which site(s) the Matrix blocks should be queried in.

The current site will be used by default.

Possible values include:

Value Fetches Matrix blocks…
'foo' from the site with a handle of foo.
['foo', 'bar'] from a site with a handle of foo or bar.
['not', 'foo', 'bar'] not in a site with a handle of foo or bar.
a \craft\elements\db\Site object from the site represented by the object.
'*' from any site.

::: tip If multiple sites are specified, elements that belong to multiple sites will be returned multiple times. If you only want unique elements to be returned, use unique in conjunction with this. :::

::: code

{# Fetch Matrix blocks from the Foo site #}
{% set MatrixBlocks = craft.matrixBlocks()
    .site('foo')
    .all() %}
// Fetch Matrix blocks from the Foo site
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->site('foo')
    ->all();

:::

siteId

Determines which site(s) the Matrix blocks should be queried in, per the site’s ID.

The current site will be used by default.

::: code

{# Fetch Matrix blocks from the site with an ID of 1 #}
{% set MatrixBlocks = craft.matrixBlocks()
    .siteId(1)
    .all() %}
// Fetch Matrix blocks from the site with an ID of 1
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->siteId(1)
    ->all();

:::

status

Narrows the query results based on the Matrix blocks’ statuses.

Possible values include:

Value Fetches Matrix blocks…
'enabled' (default) that are enabled.
'disabled' that are disabled.

::: code

{# Fetch disabled Matrix blocks #}
{% set MatrixBlocks = craft.matrixBlocks()
    .status('disabled')
    .all() %}
// Fetch disabled Matrix blocks
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->status('disabled')
    ->all();

:::

trashed

Narrows the query results to only Matrix blocks that have been soft-deleted.

::: code

{# Fetch trashed Matrix blocks #}
{% set MatrixBlocks = craft.matrixBlocks()
    .trashed()
    .all() %}
// Fetch trashed Matrix blocks
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->trashed()
    ->all();

:::

type

Narrows the query results based on the Matrix blocks’ block types.

Possible values include:

Value Fetches Matrix blocks…
'foo' of a type with a handle of foo.
'not foo' not of a type with a handle of foo.
['foo', 'bar'] of a type with a handle of foo or bar.
['not', 'foo', 'bar'] not of a type with a handle of foo or bar.
an MatrixBlockType object of a type represented by the object.

::: code

{# Fetch Matrix blocks with a Foo block type #}
{% set MatrixBlocks = myEntry.myMatrixField
    .type('foo')
    .all() %}
// Fetch Matrix blocks with a Foo block type
$MatrixBlocks = $myEntry->myMatrixField
    ->type('foo')
    ->all();

:::

typeId

Narrows the query results based on the Matrix blocks’ block types, per the types’ IDs.

Possible values include:

Value Fetches Matrix blocks…
1 of a type with an ID of 1.
'not 1' not of a type with an ID of 1.
[1, 2] of a type with an ID of 1 or 2.
['not', 1, 2] not of a type with an ID of 1 or 2.

::: code

{# Fetch Matrix blocks of the block type with an ID of 1 #}
{% set MatrixBlocks = myEntry.myMatrixField
    .typeId(1)
    .all() %}
// Fetch Matrix blocks of the block type with an ID of 1
$MatrixBlocks = $myEntry->myMatrixField
    ->typeId(1)
    ->all();

:::

uid

Narrows the query results based on the Matrix blocks’ UIDs.

::: code

{# Fetch the Matrix block by its UID #}
{% set MatrixBlock = craft.matrixBlocks()
    .uid('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')
    .one() %}
// Fetch the Matrix block by its UID
$MatrixBlock = \craft\elements\MatrixBlock::find()
    ->uid('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')
    ->one();

:::

unique

Determines whether only elements with unique IDs should be returned by the query.

This should be used when querying elements from multiple sites at the same time, if “duplicate” results is not desired.

::: code

{# Fetch unique Matrix blocks across all sites #}
{% set MatrixBlocks = craft.matrixBlocks()
    .site('*')
    .unique()
    .all() %}
// Fetch unique Matrix blocks across all sites
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->site('*')
    ->unique()
    ->all();

:::

with

Causes the query to return matching Matrix blocks eager-loaded with related elements.

See Eager-Loading Elements for a full explanation of how to work with this parameter.

::: code

{# Fetch Matrix blocks eager-loaded with the "Related" field’s relations #}
{% set MatrixBlocks = craft.matrixBlocks()
    .with(['related'])
    .all() %}
// Fetch Matrix blocks eager-loaded with the "Related" field’s relations
$MatrixBlocks = \craft\elements\MatrixBlock::find()
    ->with(['related'])
    ->all();

:::