Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
Problem/Motivation
When creating a new entity (that is unsaved) and attempting to access the id with:
$entity->id();
You can get the error:
PHP Fatal error: Call to a member function getFieldStorageDefinition() on null in core/lib/Drupal/Core/Entity/ContentEntityBase.php on line 1100
Proposed resolution
Figure out what would be causing this problem.
Remaining tasks
- Debug
- Write Patch
User interface changes
None.
API changes
None.
Data model changes
None.
Comments
Comment #2
davidwbarratt CreditAttribution: davidwbarratt at Golf Channel commentedLooks like this is because the bundle must be a string. :(
Comment #3
davidwbarratt CreditAttribution: davidwbarratt at Golf Channel commentedComment #4
Gogowitsch CreditAttribution: Gogowitsch commentedGoogle sent me here. To help future Googlers, here is the solution to my specific cause: my entity module was generated with the drupal console's "generate:entity" command and my colleague removed the "name" field on which the line in ContentEntityBase.php refers. Updating the value of label in the Entity annotation or re-adding a name field solved the problem.
Comment #5
hkirsman CreditAttribution: hkirsman commentedGoogle sent me here too :) I upgraded my Paragraphs to 1.1 and started getting fatal error when saving node. Patch from the related issue (link above) helped!
Comment #6
dawid_nawrot CreditAttribution: dawid_nawrot commentedI can confirm what @Gogowitsch says. When you create an entity with drupal console, so you have used command: drupal generate:entity:content and you have removed name field you need to provide couple of other changes. Let's assume that your entity name is 'YourEntity'. First in
/your_module/src/Entity/YourEntity.php
you need to remove'name'
from comment at the very top, so this:Changes to this:
Next in
/your_module/src/Entity/YourEntity.php
you should remove methods that refers to'name'
field, sogetName()
,setName($name)
are the one's to remove.Also modify
/your_module/src/Entity/YourEntityInterface.php
and removegetName()
andsetName($name)
methods.Last thing to modify is the
/your_module/src/YourEntityListBuilder.php
file because it creates list of entities based on name field.There's a
buildHeader()
method, so remove or modify to any existing field this line:There's also a
buildRow()
method, which uses'name'
field, so you can remove it or you can modify it:And that's it. Then you can rebuild entity with drupal console running:
drupal update:entities
Comment #7
Andy_D CreditAttribution: Andy_D at Investis Digital commentedSo I used Drupal Console to generate my entity as I thought it would save time! Of course I ran into this error:
Fatal error: Call to a member function getFieldStorageDefinition() on null in /var/www/html/docroot/core/lib/Drupal/Core/Entity/ContentEntityBase.php on line 1199
I tried changing name to title but to no avail so just remove the references to it to get the entity view to work.
Comment #8
drupalvikingThank you @david_nawrot, your solution was what helped me, making me realize how careful you have to be when removing stuff. In my case, I'm not using the
'name'
field, but I am overriding it in thebuildRow
method.Comment #9
qqboy CreditAttribution: qqboy commented#4 works thanks
Comment #10
mjsilverman CreditAttribution: mjsilverman as a volunteer commentedDitto Thank you @david_nawrot glad I found this and it turned into a simple fix.
Comment #11
justwondering CreditAttribution: justwondering commentedThank you @david_nawrot
your post/solution turned my cloudy day into sunshine.