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.


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; 
| improve this answer | |
  • thank you, belatedly showing appreciation. :) – MEI Mar 27 '19 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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