3

I have created a content entity type. When I save form, I got id in routing url, like:

entityform/1

entityform/2

But if I directly access some non-existing id like entityform/999, page dies.

In watchdog, it is showing error,

Error: Call to a member function get() on null in Drupal...

I can not call function get() on the fields of entity, if entity doesn't exists.

$content_type = $entity->get('ctype')->value;

How to apply this check, whether entity actually created or not?

  • Are you fetching the field value or you are checking if content type is 'dsf' – Shreya Shetty Jun 23 '16 at 11:22
  • ctype is the field of an entity. I'm getting this field value from $content_type = $entity->get('ctype')->value;. But what If I'm fetching the field value of entity that no where exists. Thatswhy it is giving error, 'calling functiojn get() on null', because field doesnt et created if there is no entity. – Sugandh Khanna Jun 23 '16 at 11:26
  • 1
    You can check if the entity object isset() or is not NULL, and use get() only if the condition passes. – Oana Hulpoi Jun 23 '16 at 11:32
  • Thanks, I used NULL before, it didn't work, don't know what happen now. Its working. thanks. – Sugandh Khanna Jun 23 '16 at 11:40
  • Can you update us the whole snippet of code so that we can could help you better – Shreya Shetty Jun 23 '16 at 11:45
3

You can check if the entity object isset() or is not NULL, and use get() only if the condition passes.

  • you can upvote this question, as this thing can be easily missed. – Sugandh Khanna Jun 23 '16 at 12:32

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.