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->get('project_id')->value;

project_id is en entity reference field.

1 Answer 1


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->get('project_id')->target_id; 
  • thank you, belatedly showing appreciation. :)
    – MEI
    Mar 27, 2019 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.