21
votes
Accepted
How to save Matrix data programmatically in Craft 3
Saving Matrix fields programmatically got a lot easier in Craft 3 thanks to all the content refactoring.
use craft\elements\Entry;
use craft\elements\MatrixBlock;
// Figure out the section & ...
- 34k
20
votes
Solution for Matrix-within-Matrix (or More Robust Table Fields)
Rather than trying to store multiple stats together in a single block, another option would be to make a "Stat" (singular) block type, which only holds a single stat, and just have authors create as ...
- 34k
17
votes
Solution for Matrix-within-Matrix (or More Robust Table Fields)
Folks may also want to checkin the Super Table plugin that just came out. It allows you to create table fields that include almost any field type:
https://github.com/engram-design/SuperTable
Keep in ...
- 735
16
votes
Accepted
Check to See if Field Exists
This should work:
{% if block['blockTitle'] is defined %}
// Do your thing
{% endif %}
- 21.7k
15
votes
Accepted
How Can I Include a Conditional for First Type of Matrix
You could first set a variable to test against and then change it the first time the block type appears.
{% set firstTime = true %}
{% for block in entry.matrixField %}
{% if block.type == "...
- 611
13
votes
Accepted
How to output an Entries Field value within a Matrix Field block
You get this error because {{ block.slideLink }} returns an array of EntryModels (even if you set your Entries field up to only allow 1 entry). So you need to add the first() method to get the first (...
- 36k
12
votes
Accepted
Is there a maximum number of fields in a single matrix?
This is because of a combination of settings in your php.ini file: max_input_vars & post_max_size, not any limitation in Craft CMS. c.f.:
Matrix field input error from within the control panel
- 11.3k
11
votes
Accepted
Checking for existence of a field within a Matrix block
Use brackets instead of dot notation for the field handle when you test for defined:
...
{% if block['test'] is defined %}
...
{% endif %}
- 21.7k
11
votes
How to: Flexible Twig Templates in Craft
I'm not sure I understand your question 100%, but I'm assuming you're talking about a thoroughly modular approach to templating, and not about letting your end users/content editors actually edit the ...
- 21.7k
11
votes
How to save Matrix data programmatically in Craft 3
Just an additional note:
If you want to append certain elements to your matrix field instead of overwriting them all you have to do
/** @var \craft\fields\Matrix $field */
$field = Craft::$app->...
- 19.6k
10
votes
Accepted
Possible have matrix rows collapsed by default in the Control Panel?
Inject this piece of JS in your CP and they will always be closed by default:
$('.matrixblock').each(function() {
$(this).addClass('collapsed');
Craft.MatrixInput.rememberCollapsedBlockId($(...
- 1,173
10
votes
Accepted
Is it possible to find what Matrix Block aren't being used
Nothing native, but it can be done with an SQL query – something like this should work (note that all the queries assume that your dbPrefix setting is set to 'craft_'):
SELECT bt.id, bt.handle, f....
- 21.7k
9
votes
Output matrix field data
Assuming you have an entry to work with, something like this should work:
{% for block in entry.myMatrixFieldHandle if block.type == "employment" %}
<select>
{% for option ...
- 13.4k
8
votes
How to order by matrix field?
My SuperSort plugin is built for use cases exactly such as this. It allows you to render each object in your array using a Twig object template and sort the array based on the rendered value.
- 3,400
8
votes
Templating SuperTable
There's no block types with SuperTable, so no need to check that. Other that that it works just the same as Matrix fields.
{% for row in superTableField %}
{{ row.textFieldInSuperTableField }}
{% ...
- 36k
8
votes
Accepted
{% if loop.first %} and {{ loop.index0 }} not working with Matrix
At the moment loop.first and loop.index0 are relative to your block.image loop.
If you want them to be set based on your entry.locationSlideshow loop (the parent loop) you can use {{ loop.parent.loop....
- 3,843
7
votes
How to output an Entries Field value within a Matrix Field block
All related fields (assets, entries, categories, tags, etc) are designed to store more than one item, and thus always return an "array-like" object (although not technically an array), even if you ...
- 13.4k
7
votes
Matrix fields within matrix field
As of 2016, I have come across two options that attempt to handle the matrix-within-a-matrix scenario (to varying extents):
Neo plugin
https://github.com/benjamminf/craft-neo
Provides the ability to ...
- 890
7
votes
Accepted
Accessing a Matrix field in a Global
Not sure why you're referencing "entry" in your code, I'd think the code should be:
{% for block in footer.links %}
{% if block.navigationLinks | length %}
<h3>{{ block.navigationHeadline ...
- 7,258
7
votes
Accepted
Inserting Relationship Into Entries Field - Plugin Not Saving Relation
Figured it out, trying to use the Entry as the source was wrong as the 'stores' relationship field actually belongs to the matrix field.
So by setting the matrix field as the source this is now ...
- 4,519
7
votes
Accepted
Matrix: get the index number of a certain block
You can use a custom counter instead of using the loop.index value.
{% set step = 0 %}
{% for block in entry.matrixBlocks %}
{% if block.type == 'stepBlockType' %}
{% set step = step + ...
- 36k
6
votes
Programatically create a Matrix field
FYI, this is how it's done in Craft 3:
use craft\fields\Matrix;
use craft\fields\PlainText;
//Create the matrix field
$field = Craft::$app->getFields()->createField([
'type' => Matrix::...
- 181
6
votes
Solution for Matrix-within-Matrix (or More Robust Table Fields)
This question was asked prior to the release of the Neo plugin/field type. I think this would offer a better approach than the SuperTable (which was/is still awesome), just because I think it's easier ...
- 340
6
votes
Accepted
Is it possible to take an excerpt from the first rich text area in a matrix?
You can filter by block type: http://buildwithcraft.com/docs/matrix-fields#filtering-by-block-type
In short:
{% set ogDescription = false %}
{% for block in entry.myMatrixField.type('richText')....
- 962
6
votes
Modelling flexible content sections and recommendations
I'm not familiar with ACF, but if you're looking for blocks-within-blocks, you could use the Neo plugin/fieldType. It allows you to define blocks, and then have nested blocks. It allows for decent ...
- 340
6
votes
Accepted
How to create code blocks with syntax highlighting
There is the Ace Freely plugin, which adds an Ace Editor Field Type that would work well in a Matrix field.
- 4,381
6
votes
Accepted
group Matrix blocks and also order by Matrix field for a given block
This is a great chance to use the group filter.
First, get all your matrix blocks as an array:
{% set talkBlocks = entry.talks.find() %}
Then group them by type:
{% set talksByCategories = ...
- 12k
6
votes
If Conditional for Lightswitch in Matrix Field
You're close! The way you have your conditional set up now will print Hero Image Markup if either of your conditionals fail. By checking if block.displayHeroVideo is 1 on a block type that doesn't ...
- 3,821
6
votes
Accepted
Reverse Related entries with Matrix Field
Have a look at the 'Going Through Matrix' section at http://buildwithcraft.com/docs/relations#the-relatedTo-param.
{% set articles = craft.entries.section('articles').relatedTo({
targetElement: ...
- 3,507
6
votes
Managing Frontend JS for Matrix Fields
I'll be flat-out honest; I didn't read through your code. Secondly, I think to set expectations when you say a matrix field on the front-end you'll need to understand it isn't going to look like what ...
- 4,696
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
matrix × 636templating × 83
craft3 × 62
plugin-development × 45
relations × 40
entries × 38
field × 38
assets × 33
entry × 22
block-types × 21
blocks × 20
search × 19
loop × 19
categories × 18
entries-field × 18
forloop × 17
plugin-supertable × 17
conditional × 15
localization × 14
control-panel × 13
datetime × 12
plugins × 11
fieldtypes × 11
database × 10
elementcriteriamodel × 10