Skip to main content
Share Your Experience: Take the 2024 Developer Survey
24 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 & ...
Brandon Kelly's user avatar
13 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 Update Craft 4.0 there is a new sortOrder key that makes my previous ...
Robin Schambach's user avatar
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 ...
Mats Mikkel Rummelhoff's user avatar
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....
Mats Mikkel Rummelhoff's user avatar
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....
Luke Pearce's user avatar
  • 3,863
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 ...
Simon East's user avatar
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 + ...
carlcs's user avatar
  • 36.3k
6 votes
Accepted

Limiting the Upload Location for assets in a Matrix Block

owner is the element which contains your Matrix field. For example, if you attach a Matrix field to a Section, the owner would be the specific Entry in that Section. {owner.slug} - The slug of your ...
Lindsey D's user avatar
  • 24k
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::...
danbrellis's user avatar
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 ...
Ian DeRanieri's user avatar
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 ...
Ian DeRanieri's user avatar
6 votes
Accepted

What's the most user friendly way to create a video block in a matrix field?

From basic to robust: Redactor has a plugin along that same vein but it's more basic—feed it a URL and it spits out the embed code in a Redactor field. Craft-video-embed-utilty sounds like it might ...
RitterKnight's user avatar
  • 6,582
6 votes
Accepted

Reusable Matrix block types

A few options to consider: Use one big Matrix Block and a plugin such as Pimp My Matrix to get added control over which blocks display in which channels and various other details. If you find ...
Ben Parizek's user avatar
  • 13.5k
6 votes
Accepted

After updating, getting "Could not resolve the subpath" error

If you're setting an dynamic upload folder location for an Assets field inside of a Matrix field, you need to preface things like {slug} with {owner.slug}. See the note about dynamic subfolders on an ...
Brad Bell's user avatar
  • 67.5k
6 votes
Accepted

Loop through fields of a matrix block

Check if the matrixField is populated: {% if entry.matrixField|length %}{% endif %} There are two options to pull out fields from a block type. Option A: use a for-loop pointed at your Matrix field ...
Dominik Krulak's user avatar
6 votes
Accepted

How to pull the first block of its kind from a Matrix?

You could try something like this: {% set block = post.buildStory.type('textEditorBlock').first() %} {{block.textEditor}}
alexr's user avatar
  • 618
5 votes

Matrix max block type limit

I just published a plugin called Limit Block Type, which adds the ability to limit Matrix blocks per block type. There's also a feature request for adding this feature to core.
Mats Mikkel Rummelhoff's user avatar
5 votes
Accepted

Querying for an exact match on a Matrix block field with multiple types

When I understand Brandon correctly in this answer Query for blocks across Block Types using the new field parameter?, it's not possible to set up what you want with a criteria model. What you can do ...
carlcs's user avatar
  • 36.3k
5 votes
Accepted

Is there a better way than stacked if else

There is certainly a lot of repeated code here so we can definitely improve on that! There are so many ways you could handle your Twig templates but this is how I would do it: <div id="slider-with-...
Jamie Wade's user avatar
  • 3,644
5 votes
Accepted

Recommended Structure - One Site with Pages With Totally Different Layouts?

Option 1 is a safe bet. This allows you to have completely separate, editable templates for each chosen year. You can then change the page slug to match the year of that page. You can reuse fields ...
mrtpain's user avatar
  • 176
5 votes
Accepted

Getting fields of Matrix?

Looping through a Matrix field is pretty easy, here is an example. Also read the docs on the Matrix field here - https://craftcms.com/docs/matrix-fields {% for block in entry.myMatrix %} {% set ...
Fyrebase's user avatar
  • 735
5 votes

Dynamic upload location for Super Table within a Matrix field?

Have you tried using {owner.owner.slug}? If you've got Matrix > Super Table > Asset, and you're trying to get an attribute on the Entry/Element, you'll need to go two level out from the Super Table ...
crawf's user avatar
  • 3,562
5 votes
Accepted

How to check if matrix block type is empty or not

You can fetch the number of entries for the first type, check if you receive an empty array if so fetch matrix blocks of type B Craft 2 {% set blocksTypeOne = entry.matrixFieldHandle.type('...
Robin Schambach's user avatar
5 votes
Accepted

Loop through matrix field find first block of a type

{% set myFirstMatrixBlockWithType = entry.blogPostMainSection.type('blogText').one() %} {{ myFirstMatrixBlockWithType.articleText }} Keep in mind entry.blogPostMainSection is not an array of blocks ...
Robin Schambach's user avatar
5 votes

Matrix fields in an entry form

The trick, as it were, is to define the Matrix field in your form as an array. First, the Matrix entry field itself: <input type="hidden" name="fields[traveller][new1][type]" value="...
Roi Agneta's user avatar
  • 1,463
5 votes
Accepted

How do you delete a matrix block in module/plugin assuming you have the matrix block id and entry

You can use the Elements service to delete any element, including matrix blocks: use craft\elements\MatrixBlock; /** @var craft\services\Elements $elementsService */ $elementsService = Craft::$app-&...
MoritzLost's user avatar
  • 11.4k
5 votes
Accepted

Adding New Matrix Block via Frontend in Craft 3

After talking to Craft Support (Thanks Oli!), the solution for Craft 3 is... {{ hiddenInput('fields[vendorLocations][sortOrder][]', 'new:1') }} {{ hiddenInput('fields[vendorLocations][blocks][new:1][...
cherrykoda's user avatar
  • 1,114
4 votes

Output matrix field data

Craft 3 updated the code so this is the new way to get a matrix field that worked for me. The only difference is matrixField.blockTypes vs Craft 2 matrixField.getFieldType.getSettings.getBlockTypes {...
Nathan S's user avatar
4 votes
Accepted

"Forbidden" response when saving entry with HTML code in matrix field

There are a couple possible explanations for this. You have Suhosin installed, a PHP extension that enhances security by placing some pretty drastic restrictions on what data can be sent to the ...
Brandon Kelly's user avatar
4 votes

"Forbidden" response when saving entry with HTML code in matrix field

I contacted the host and they found that it was mod_security, and disabled the rule that was causing this.
user5149's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible