How do I change a field with hook_node_presave()? In Drupal 7, I was using hook_node_presave() to update some hidden fields in a node.

function mymodule_node_presave($node){
    if ( CONDITIONS ) {
        $node-&gt;field_fieldname['und'][0]['value'] = importantfunction();
    }
}


In Drupal 8 I cannot do the same thing. Looking into the object I tried this:

function mymodule_node_presave($node){
    if ( CONDITIONS ) {
        $node-&gt;values['field_fieldname'][0]['value'];
    } 
}


It won't work though. I am aware that a lot have changed in Drupal 8 and I am studying it, but I did not find an answer to this yet.
A bit outdated but still a great resource:

http://wizzlern.nl/drupal/drupal-8-entity-cheat-sheet

Also the documentation on drupal.org: https://www.drupal.org/node/1795854

In short, fields are lists of objects that use property and array access magic methods to simplify reading and writing values to them:

// Set a value, the field class will decide what to do with it, usually write to the primary property...
$entity-&gt;field_fieldname = importantfunction();

// The default property is often called value, but for reference fields, it is target_id, text fields also have a format for example
$entity-&gt;field_fieldname-&gt;format = 'full_html';

// By default, you access delta 0, you can also set other deltas.
$entity-&gt;field_fieldname[1] = importantfunction();

// You can also append a new item, without hardcoding the delta.
$entity-&gt;field_fieldname-&gt;appendItem(importantfunction());

// Note that when accessing fields, you must always specify the property:
print $entity-&gt;field_fieldname-&gt;value;
print $entity-&gt;field_fieldname[1]-&gt;value;

// What basically happens internally for the above is:
$entity-&gt;get('field_fieldname')-&gt;get(0)-&gt;get('value')-&gt;getValue();