<p>Since Matrix blocks are elements as well it's the same</p>

<p>Your form could be</p>

<pre class="lang-twig prettyprint-override"><code>{% set movies = entry.movies.all() %}
{% for movie in movies %}
    &lt;form&gt;
        &lt;input type="hidden" value="plugin/controller/function" name="action"&gt;
        {{ csrfInput() }}
        &lt;input type="hidden" value="{{ movie.id }}" name="id"&gt;
        &lt;input type="submit" value="Vote this movie"&gt;
    &lt;/form&gt;
{% endfor %}
</code></pre>

<p>And your controller</p>

<pre class="lang-php prettyprint-override"><code>public function actionSave(){
    $id = Craft::$app-&gt;getRequest()-&gt;getRequiredBodyParam('id');
    $matrixBlock = Craft::$app-&gt;getMatrix()-&gt;getBlockById($id);
    if($matrixBlock === null){
        throw new ElementNotFoundException('Could not find Block with ID ' . $id);
    }

    $matrixBlock-&gt;setFieldValue('votes', $matrixBlock-&gt;getFieldValue('votes')+1);
    if(!Craft::$app-&gt;getElements()-&gt;saveElement($matrixBlock)){
        // handle an error
    }

    return $this-&gt;redirectToPostedUrl();
}
</code></pre>

CC BY-SA 4.0
2018-08-08T08:49:07.400Z
I don't totally understand, but it works great, thank you! :) I think my main problem was not understanding the docs.
evu
CC BY-SA 4.0
2018-08-08T13:26:23.930Z
Glad to hear that.. I didn't even test it at all. If you want to understand more about Craft you may want to join slack
Robin Schambach
CC BY-SA 4.0
2018-08-07T14:40:50.733Z
2018-08-07T16:33:41.177Z
2018-08-07T16:33:41.177Z
Robin Schambach
<p>I have a plugin that wants to set a value for an entries existing matrix field, from a form on the font-end. Think of it like:</p>

<pre><code>Movies (matrix field)
  Movie (matrix block)
    Name (plain text)
    votes (plain text)
  Movie (matrix block)
    Name (plain text)
    votes (plain text)
</code></pre>

<p>I want to increment the number of votes when a user clicks a button for a particular movie.</p>

<p>I've tried all sorts of methods, but being pretty novice at php I just can't seem to grasp how Craft is storing everything. I just keep getting back an array of ALL fields on the site, rather than the specific ones I'm searching for.</p>

<p>All existing SE questions seem to relate to adding new rows, I just want to edit an existing one.</p>

<p>Normal fields are (from existing code) along the lines of</p>

<pre><code>$currentVotes = $movie-&gt;getFieldValue('votes');
$movie-&gt;setFieldValue('votes', $currentVotes + 1);
</code></pre>

<p>Which seems rather simple. What is the Matrix version of that?</p>

<plugin-development><matrix><craft3>
Get and Set existing matrix field data in a plugin
# Get and Set existing matrix field data in a plugin

(Asked by: username_0 on 2018-08-07)

I have a plugin that wants to set a value for an entries existing matrix field, from a form on the font-end. Think of it like:
Movies (matrix field)
  Movie (matrix block)
    Name (plain text)
    votes (plain text)
  Movie (matrix block)
    Name (plain text)
    votes (plain text)

I want to increment the number of votes when a user clicks a button for a particular movie.
I've tried all sorts of methods, but being pretty novice at php I just can't seem to grasp how Craft is storing everything. I just keep getting back an array of ALL fields on the site, rather than the specific ones I'm searching for.
All existing SE questions seem to relate to adding new rows, I just want to edit an existing one.
Normal fields are (from existing code) along the lines of
$currentVotes = $movie->getFieldValue('votes');
$movie->setFieldValue('votes', $currentVotes + 1);

Which seems rather simple. What is the Matrix version of that?

**Tags:** plugin-development, matrix, craft3

---

## 1 answers

### Answer by username_1 on 2018-08-07. Score: 4 | Accepted answer

Since Matrix blocks are elements as well it's the same
Your form could be
{% set movies = entry.movies.all() %}
{% for movie in movies %}
    
        
        {{ csrfInput() }}
        
        
    
{% endfor %}

And your controller
public function actionSave(){
    $id = Craft::$app->getRequest()->getRequiredBodyParam('id');
    $matrixBlock = Craft::$app->getMatrix()->getBlockById($id);
    if($matrixBlock === null){
        throw new ElementNotFoundException('Could not find Block with ID ' . $id);
    }

    $matrixBlock->setFieldValue('votes', $matrixBlock->getFieldValue('votes')+1);
    if(!Craft::$app->getElements()->saveElement($matrixBlock)){
        // handle an error
    }

    return $this->redirectToPostedUrl();
}

> **Comments:**
> **username_0**: I don't totally understand, but it works great, thank you! :) I think my main problem was not understanding the docs.  
> **username_1**: Glad to hear that.. I didn't even test it at all. If you want to understand more about Craft you may want to join slack  

---

