Matrix Blocks
If you’ve ever worked with a hairball of content and markup managed in an increasingly-fragile WYSIWYG field, you’re probably going to like Matrix blocks.
Matrix blocks are groupings of fields an editor can use to build and rearrange content. They can be critical in supporting flexible, customizable content for an editor that’s balanced with the kind of discrete, well-modeled content a developer wants to work with.
Querying Matrix Blocks
You can fetch Matrix blocks in your templates or PHP code using Matrix block queries.
{# 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.
See 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:
- Create a Matrix block query with
craft.matrixBlocks()
. - Set the owner, fieldId, and type parameters on it.
- Fetch the Matrix blocks with
.all()
. - 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 %}
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:
| Param | Description
| ------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| afterPopulate | Performs any post-population processing on elements.
| allowOwnerDrafts | Narrows the query results based on whether the Matrix blocks’ owners are drafts.
| allowOwnerRevisions | Narrows the query results based on whether the Matrix blocks’ owners are revisions.
| andRelatedTo | Narrows the query results to only Matrix blocks that are related to certain other elements.
| anyStatus | Removes element filters based on their statuses.
| asArray | Causes the query to return matching Matrix blocks as arrays of data, rather than MatrixBlock objects.
| cache | Enables query cache for this Query.
| clearCachedResult | Clears the cached result.
| dateCreated | Narrows the query results based on the Matrix blocks’ creation dates.
| dateUpdated | Narrows the query results based on the Matrix blocks’ last-updated dates.
| field | Narrows the query results based on the field the Matrix blocks belong to.
| fieldId | Narrows the query results based on the field the Matrix blocks belong to, per the fields’ IDs.
| fixedOrder | Causes the query results to be returned in the order specified by id.
| id | Narrows the query results based on the Matrix blocks’ IDs.
| 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.
| limit | Determines the number of Matrix blocks that should be returned.
| offset | Determines how many Matrix blocks should be skipped in the results.
| orderBy | Determines the order that the Matrix blocks should be returned in. (If empty, defaults to sortOrder ASC
.)
| owner | Sets the ownerId and siteId parameters based on a given element.
| ownerId | Narrows the query results based on the owner element of the Matrix blocks, per the owners’ IDs.
| preferSites | If unique is set, this determines which site should be selected when querying multi-site elements.
| relatedTo | Narrows the query results to only Matrix blocks that are related to certain other elements.
| search | Narrows the query results to only Matrix blocks that match a search query.
| site | Determines which site(s) the Matrix blocks should be queried in.
| siteId | Determines which site(s) the Matrix blocks should be queried in, per the site’s ID.
| siteSettingsId | Narrows the query results based on the Matrix blocks’ IDs in the elements_sites
table.
| status | Narrows the query results based on the Matrix blocks’ statuses.
| trashed | Narrows the query results to only Matrix blocks that have been soft-deleted.
| type | Narrows the query results based on the Matrix blocks’ block types.
| typeId | Narrows the query results based on the Matrix blocks’ block types, per the types’ IDs.
| uid | Narrows the query results based on the Matrix blocks’ UIDs.
| unique | Determines whether only elements with unique IDs should be returned by the query.
| with | Causes the query to return matching Matrix blocks eager-loaded with related elements.
afterPopulate
Performs any post-population processing on elements.
allowOwnerDrafts
Narrows the query results based on whether the Matrix blocks’ owners are drafts.
Possible values include:
| Value | Fetches Matrix blocks…
| - | -
| true
| which can belong to a draft.
| false
| which cannot belong to a draft.
allowOwnerRevisions
Narrows the query results based on whether the Matrix blocks’ owners are revisions.
Possible values include:
| Value | Fetches Matrix blocks…
| - | -
| true
| which can belong to a revision.
| false
| which cannot belong to a revision.
andRelatedTo
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 myCategoryA and myCategoryB #}
{% set MatrixBlocks = craft.matrixBlocks()
.relatedTo(myCategoryA)
.andRelatedTo(myCategoryB)
.all() %}
// Fetch all Matrix blocks that are related to $myCategoryA and $myCategoryB
$MatrixBlocks = \craft\elements\MatrixBlock::find()
->relatedTo($myCategoryA)
->andRelatedTo($myCategoryB)
->all();
:::
anyStatus
Removes element filters based on their statuses.
::: 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();
:::
cache
Enables query cache for this Query.
clearCachedResult
Clears the cached result.
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();
:::
field
Narrows the query results based on the field the Matrix blocks belong to.
Possible values include:
| Value | Fetches Matrix blocks…
| - | -
| 'foo'
| in a field with a handle of foo
.
| 'not foo'
| not in a field with a handle of foo
.
| ['foo', 'bar']
| in a field with a handle of foo
or bar
.
| ['not', 'foo', 'bar']
| not in a field with a handle of foo
or bar
.
| a craft\fields\Matrix object | in a field represented by the object.
::: code
{# Fetch Matrix blocks in the Foo field #}
{% set MatrixBlocks = craft.matrixBlocks()
.field('foo')
.all() %}
// Fetch Matrix blocks in the Foo field
$MatrixBlocks = \craft\elements\MatrixBlock::find()
->field('foo')
->all();
:::
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.
::: tip
If no IDs were passed to id, setting this to true
will result in an empty result set.
:::
::: 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. (If empty, defaults to sortOrder ASC
.)
::: 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.
| [1, 2]
| 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 B, and Bar will be returned
for Site C.
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();
:::
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\models\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.
Possible values include:
| Value | Fetches Matrix blocks…
| - | -
| 1
| from the site with an ID of 1
.
| [1, 2]
| from a site with an ID of 1
or 2
.
| ['not', 1, 2]
| not in a site with an ID of 1
or 2
.
| '*'
| from any site.
::: 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();
:::
siteSettingsId
Narrows the query results based on the Matrix blocks’ IDs in the elements_sites
table.
Possible values include:
| Value | Fetches Matrix blocks…
| - | -
| 1
| with an elements_sites
ID of 1.
| 'not 1'
| not with an elements_sites
ID of 1.
| [1, 2]
| with an elements_sites
ID of 1 or 2.
| ['not', 1, 2]
| not with an elements_sites
ID of 1 or 2.
::: code
{# Fetch the Matrix block by its ID in the elements_sites table #}
{% set MatrixBlock = craft.matrixBlocks()
.siteSettingsId(1)
.one() %}
// Fetch the Matrix block by its ID in the elements_sites table
$MatrixBlock = \craft\elements\MatrixBlock::find()
->siteSettingsId(1)
->one();
:::
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.
| ['not', 'disabled']
| that are not 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();
:::