<p>You need to set the field value, not the options</p>

<pre><code>$entry-&gt;setFieldValue('fieldHandle' [
    'value1', 'value2'
});
Craft::$app-&gt;getElements()-&gt;saveElement($entry);
</code></pre>

<p>If you want to change the values, the easiest method is</p>

<pre><code>$allOptions = $entry-&gt;getFieldValue('fieldHandle');
$selected = [];
foreach($allOptions as $option){
    if($option-&gt;selected){
         $selected[] = $option-&gt;value;
    }
}
// unset a value
unset($selected[1]);
// add another value
$selected[] = 'anotherValue';
$entry-&gt;setFieldValue('fieldHandle', $selected);
Craft::$app-&gt;getElements()-&gt;saveElement($entry);
</code></pre>

<h3>edit</h3>

<p>Another approach - a little bit less code and more general since it's the same for every single field in Craft - but more resource heavy</p>

<pre><code>$field = Craft::$app-&gt;getFields()-&gt;getFieldByHandle('handle);
$selected = $field-&gt;serializeValue($entry-&gt;getFieldValue('handle'), $entry);
$selected[] = 'anotherValue';
$entry-&gt;setFieldValue('handle', $selected);
Craft::$app-&gt;getElements()-&gt;saveElement($entry);
</code></pre>

CC BY-SA 4.0
2018-05-07T08:22:42.120Z
Accepted solution! Works perfectly - Thanks a lot!
Mischa Sz.
CC BY-SA 4.0
2018-05-04T14:55:59.700Z
2018-05-05T10:38:15.817Z
2018-05-05T10:38:15.817Z
Robin Schambach
<p>I'm trying to update a checkbox field of an entry through a plugin service. I tried two things:</p>

<ol>
<li><p>Get the field values with <code>$entry-&gt;fieldHandle-&gt;getOptions()</code>, then manipulating the resulting array, then set the value with <code>$entry-&gt;fieldHandle-&gt;setOptions()</code></p>

<pre><code>$contact = Entry::find()
    -&gt;id(...)
    -&gt;one();
$newsletters = $contact-&gt;contactNewsletterSubscriptions-&gt;getOptions();
$newsletters[$index]-&gt;selected = false;
$contact-&gt;contactNewsletterSubscriptions-&gt;setOptions($newsletters);
Craft::$app-&gt;elements-&gt;saveElement($contact);
</code></pre></li>
</ol>

<p>(where $index is the array index of the setting I want to change)</p>

<p>-> this didn't work (no error message, changes just didn't get saved)</p>

<ol start="2">
<li><p>If I make a var_dump of <code>$contact</code>, the field contactNewsletterSubscriptions is showed as string. I played around with json_decode and serialize to get the right formatting (it looks exactly as when doing a var_dump):</p>

<pre><code>$newsletterSubscriptions = json_decode($contact-&gt;contactNewsletterSubscriptions-&gt;serialize());
$index = array_search($newsletterName, $newsletterSubscriptions);
unset($newsletterSubscriptions[$index]);
$contact-&gt;contactNewsletterSubscriptions = (string)json_encode(array_values($newsletterSubscriptions))
</code></pre></li>
</ol>

<p>-> doesn't work either; value gets saved as string; if I later call getOptions() on the field I'm getting "Call to a member function getOptions() on string"</p>

<p>Any idea how I can change the status of a checkbox from selected = true to selected = false?</p>

<p>Best regards.</p>

<plugin-development><craft3>
Update checkbox value through plugin (Craft 3)
# Update checkbox value through plugin (Craft 3)

(Asked by: username_0 on 2018-05-04)

I'm trying to update a checkbox field of an entry through a plugin service. I tried two things:

Get the field values with $entry->fieldHandle->getOptions(), then manipulating the resulting array, then set the value with $entry->fieldHandle->setOptions()
$contact = Entry::find()
    ->id(...)
    ->one();
$newsletters = $contact->contactNewsletterSubscriptions->getOptions();
$newsletters[$index]->selected = false;
$contact->contactNewsletterSubscriptions->setOptions($newsletters);
Craft::$app->elements->saveElement($contact);


(where $index is the array index of the setting I want to change)
-> this didn't work (no error message, changes just didn't get saved)

If I make a var_dump of $contact, the field contactNewsletterSubscriptions is showed as string. I played around with json_decode and serialize to get the right formatting (it looks exactly as when doing a var_dump):
$newsletterSubscriptions = json_decode($contact->contactNewsletterSubscriptions->serialize());
$index = array_search($newsletterName, $newsletterSubscriptions);
unset($newsletterSubscriptions[$index]);
$contact->contactNewsletterSubscriptions = (string)json_encode(array_values($newsletterSubscriptions))


-> doesn't work either; value gets saved as string; if I later call getOptions() on the field I'm getting "Call to a member function getOptions() on string"
Any idea how I can change the status of a checkbox from selected = true to selected = false?
Best regards.

**Tags:** plugin-development, craft3

---

## 1 answers

### Answer by username_1 on 2018-05-04. Score: 2 | Accepted answer

You need to set the field value, not the options
$entry->setFieldValue('fieldHandle' [
    'value1', 'value2'
});
Craft::$app->getElements()->saveElement($entry);

If you want to change the values, the easiest method is
$allOptions = $entry->getFieldValue('fieldHandle');
$selected = [];
foreach($allOptions as $option){
    if($option->selected){
         $selected[] = $option->value;
    }
}
// unset a value
unset($selected[1]);
// add another value
$selected[] = 'anotherValue';
$entry->setFieldValue('fieldHandle', $selected);
Craft::$app->getElements()->saveElement($entry);

edit
Another approach - a little bit less code and more general since it's the same for every single field in Craft - but more resource heavy
$field = Craft::$app->getFields()->getFieldByHandle('handle);
$selected = $field->serializeValue($entry->getFieldValue('handle'), $entry);
$selected[] = 'anotherValue';
$entry->setFieldValue('handle', $selected);
Craft::$app->getElements()->saveElement($entry);

> **Comments:**
> **username_0**: Accepted solution! Works perfectly - Thanks a lot!  

---

