How to use set() on a field within a field collection? I have a custom entity type, and I'm defining its form, which extends ContentEntityForm. During my save() function, I want the form to conditionally unset fields based on the values of other fields. (example: unset the value of a list select field if a certain checkbox is checked off)

I can't seem to accomplish this with $form_state-&gt;setValue() or $form_state-&gt;setValueForElement(), in both cases my code is seemingly ignored and the select widget is not unset upon finishing saving (although a var_dump of $form_state-&gt;getValues() at the end of the function shows that the values array is indeed being modified the way I want it to be).

However, the following code seems to work nicely for the title field (defined via baseFieldDefinitions(), is not inside a field collection):

$entity = $this-&gt;getEntity();
    $entity-&gt;set('title', array(array("value" =&gt; "aaaaaaaa")));
$entity-&gt;save();


My title for the newly saved entity does indeed show up as "aaaaaaaa"

Is it possible to target fields inside field collections in this way using this $entity object? Or is this the totally wrong approach?
It's not really clear to me why you don't change the values on the entity, but save() is too late.

The entity is built in buildEntity()/copyFormValuesToEntity(), if you want to change how it is build based on form_state values, you have to change them by overriding those methods and doing your changes first.