Programmatically get field values of fields added to custom entity I created a custom entity in Drupal 8, and I added base fields.

I add the getter/setter functions to my entity definition file.
They return NULL when called from a preprocess function. (I'm trying to add some variables for the Twig template.)

Why would this be?  I'm using the following code.

public function getProjectId() {
    return $this-&gt;get('project_id')-&gt;value;
}


project_id is en entity reference field.
The column for an entity reference field is target_id, so you should just need to replace the getter with:

public function getProjectId() { 
    return $this-&gt;get('project_id')-&gt;target_id; 
}