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

  1. Debug
  2. Write Patch

User interface changes

None.

API changes

None.

Data model changes

None.

Comments

davidwbarratt created an issue. See original summary.

davidwbarratt’s picture

Looks like this is because the bundle must be a string. :(

davidwbarratt’s picture

Status: Active » Closed (duplicate)
Gogowitsch’s picture

Google 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.

hkirsman’s picture

Google 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!

dawid_nawrot’s picture

I 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:

*   entity_keys = {
 *     "id" = "id",
 *     "label" = "name",
 *     "uuid" = "uuid",
 *     "uid" = "user_id",
 *     "langcode" = "langcode",
 *     "status" = "status",
 *   },

Changes to this:

*   entity_keys = {
 *     "id" = "id",
 *     "uuid" = "uuid",
 *     "uid" = "user_id",
 *     "langcode" = "langcode",
 *     "status" = "status",
 *   },

Next in /your_module/src/Entity/YourEntity.php you should remove methods that refers to 'name' field, so getName(), setName($name) are the one's to remove.
Also modify /your_module/src/Entity/YourEntityInterface.php and remove getName() and setName($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:

$header['name'] = $this->t('Name');

There's also a buildRow() method, which uses 'name' field, so you can remove it or you can modify it:

  $row['test_exchange_rate'] = $this->l(
      $entity->label(),
      new Url(
        'entity.your_entity.edit_form', array(
          'your_entity' => $entity->id(),
        )
      )
    );

And that's it. Then you can rebuild entity with drupal console running:
drupal update:entities

Andy_D’s picture

So 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.

drupalviking’s picture

Thank 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 the buildRow method.

qqboy’s picture

#4 works thanks

mjsilverman’s picture

Ditto Thank you @david_nawrot glad I found this and it turned into a simple fix.

justwondering’s picture

Thank you @david_nawrot
your post/solution turned my cloudy day into sunshine.