<p>After a bit of testing, it appears you don't have anything to do to have your values saved, just set them and Craft will take care of the rest.</p>
<pre><code>    Event::on(
        Elements::class,
        Elements::EVENT_BEFORE_SAVE_ELEMENT,
        static function (ElementEvent $event) {
            $element = $event-&gt;element;
            if ($element instanceof Entry &amp;&amp; $element-&gt;getSection()-&gt;handle === 'testSection') {
                /** @var MatrixBlock[] $blocks */
                $blocks = $element-&gt;getFieldValue('testMatrix')-&gt;all();
                foreach ($blocks as $block) {
                    $block-&gt;setFieldValue('automaticField', uniqid('some unique value', false));
                }
            }
        }
</code></pre>

CC BY-SA 4.0
2020-09-18T15:30:08.070Z
This did not work for me. I implemented it the same way as you but the changes were not applied and not shown in the ControlPanel edit page.
Leonardo
CC BY-SA 4.0
2020-09-22T08:58:16.977Z
Okey so i handled my problem for the after save in another way.
But would you know how to save it in, lets say, a controller? Getting the element via an id , then change the field value of certaint matrix blocks and then resave it?
Leonardo
CC BY-SA 4.0
2020-09-22T09:31:31.700Z
Not tested but I guess you could just call the `save()` method on the element after making your changes. But if my previous code example didn't work for you, I'm afraid just calling `save()` might not be enough. Can you explain what you did in the after save?
nstCactus
CC BY-SA 4.0
2020-09-25T08:02:57.193Z
This irritates me. A `save()` function on MatrixBlocks does not exist for me. Or are we both talking about different things?

After Save:
We are now generating and saving a custom id on the element. If the custom id of each matrix block is needed, a combination from the element's custom id and the matrix block id is generated inside a simple sevice.
Leonardo
CC BY-SA 4.0
2020-09-25T11:30:04.233Z
I'm talking about the `save()` method on the element owning the matrix field (this is most probably an Entry).
nstCactus
CC BY-SA 4.0
2020-09-18T07:39:48.920Z
2020-09-18T07:39:48.920Z
nstCactus
<p>So i have a Matrix with one type of matrixblocks.</p>
<p>These all have a field for a custom id which should be set by the system, if empty, before the entry is saved.</p>
<p>I can access the individual matrixblocks and change the field value in all of them. But i am not sure how to add them back to the matrix so the changes will apply.</p>
<p>example code that is not working:
<a href="https://i.sstatic.net/lXqz1.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/lXqz1.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2020-09-17T19:10:53.627Z
Where do you use this code? In a module? If so it would help to know which event you hook to, if any, or if it's in a controller action. Have you tried `$execution->save()`?
nstCactus
CC BY-SA 4.0
2020-09-18T06:41:00.997Z
I am using this code inside a module. It is triggered inside the EVENT_BEFORE_SAVE_ELEMENT Event.
Leonardo
<craft3><matrix><modules><saveentry>
Change matrixblock field value before save element
# Change matrixblock field value before save element

(Asked by: username_0 on 2020-09-17)

So i have a Matrix with one type of matrixblocks.

These all have a field for a custom id which should be set by the system, if empty, before the entry is saved.

I can access the individual matrixblocks and change the field value in all of them. But i am not sure how to add them back to the matrix so the changes will apply.

example code that is not working:
[![enter image description here](https://i.sstatic.net/lXqz1.png)](https://i.sstatic.net/lXqz1.png)

**Tags:** craft3, matrix, modules, saveentry

> **Comments:**
> **username_1**: Where do you use this code? In a module? If so it would help to know which event you hook to, if any, or if it's in a controller action. Have you tried `$execution->save()`?  
> **username_0**: I am using this code inside a module. It is triggered inside the EVENT_BEFORE_SAVE_ELEMENT Event.  

---

## 1 answers

### Answer by username_1 on 2020-09-18. Score: 1

After a bit of testing, it appears you don't have anything to do to have your values saved, just set them and Craft will take care of the rest.
    Event::on(
        Elements::class,
        Elements::EVENT_BEFORE_SAVE_ELEMENT,
        static function (ElementEvent $event) {
            $element = $event->element;
            if ($element instanceof Entry && $element->getSection()->handle === 'testSection') {
                /** @var MatrixBlock[] $blocks */
                $blocks = $element->getFieldValue('testMatrix')->all();
                foreach ($blocks as $block) {
                    $block->setFieldValue('automaticField', uniqid('some unique value', false));
                }
            }
        }

> **Comments:**
> **username_0**: This did not work for me. I implemented it the same way as you but the changes were not applied and not shown in the ControlPanel edit page.  
> **username_0**: Okey so i handled my problem for the after save in another way. But would you know how to save it in, lets say, a controller? Getting the element via an id , then change the field value of certaint matrix blocks and then resave it?  
> **username_1**: Not tested but I guess you could just call the `save()` method on the element after making your changes. But if my previous code example didn't work for you, I'm afraid just calling `save()` might not be enough. Can you explain what you did in the after save?  
> **username_0**: This irritates me. A `save()` function on MatrixBlocks does not exist for me. Or are we both talking about different things?  After Save: We are now generating and saving a custom id on the element. If the custom id of each matrix block is needed, a combination from the element's custom id and the matrix block id is generated inside a simple sevice.  
> **username_1**: I'm talking about the `save()` method on the element owning the matrix field (this is most probably an Entry).  

---

