Craft CMS: Matrix blocks i Graphic Module – cz1.

Matrix blocks i Graphic Module – cz1.

11 June 2019

Matrix blocks

Working on the publication of the website, you may wonder whether it is necessary to prepare it in such a way that it is fully connected to the CMS system. From the developer's point of view, filling the page with content and graphics is not the slightest problem. However, the stairs appear when the client wants to have a full impact on the content management and the project we are preparing is a service with many subpages. Various types of content management systems step in, among which we recommend Craft CMS.

However, Craft is not only a convenient tool for the client, enabling him/her to type-in content on the website, it also facilitates the work of developers!

In today's post, we will focus on describing issues related to matrix fields.

A useful functionality that we get from Craft CMS is the option to organize fields in the so-called Matrix blocks and to link graphic modules with them. Put simply, we may say that Matrix blocks accelerates the work of developers because it calls them from the necessity of styling repeating elements on the page. Having this, we develop a code and styles for a given module only once and they are duplicated each time a given block is called.

Matrix Fields allow you to create multiple blocks of content in one field.

You can configure them to specify which block types should be available for a given matrix field, and what subfields each of these blocks should have. In practice, the matrix-type fields are often created under the name of "subpage content" or "build subpage" and in them we place elements that will be reproducible in various places on the site.

In order to take fully use the possibilities of matrix fields, we need to place them properly in the project. Therefore, it is most convenient to create a file named _matrix.html, where you can use the appropriate code to call the blocks built in the matrix.

In this code, using the switch loop, we pass through all the blocks we have created.

However, thanks to the entry at the very end, in the page preview, we should display the following inscription: "There is no support for the MODULE NAME", etc. Things will be like that as long as we do not support the appropriate template.

To replace the default description about the lack of service with the appropriate module:

1. Prepare the .html file (it is convenient to name it as the matrix block to which it refers, for example, headline-and-digit-with-title)

2. In the file you created, attach Craft elements, for example:

At the same time the entry highlighted in orange is essential for us, because it is the one that uploads the content of subpages and graphic modules,

3. In the _matrix.html file in the switch loop, we add a new reference:

the name of the case is best copied from the message about the lack of support for the module displayed on our screen (yellow color) and then specify the path from which the element structure should be retrieved and its formatting (blue).

In this way, if we encode and sharpen reusable blocks drawn from the matrix, such as headers and content, galleries, photos, margins, graphic modules, etc. Each time they are called anywhere on the site, we will use the code we have prepared only with the content changed by the client.

In the next post I will cover the blocking of the graphics module, which is part of the matrix.