Steps to reproduce:

  • clean D8 english install, standard profile, on today's HEAD
  • node/add/article, give it a title, save it
  • click to delete it (go to node/1/delete): click on delete on the confirm form
  • get an PHP error because the code tries to enumerate fields on a NULL in the id() method
( ! ) Fatal error: Call to a member function id() on a non-object in /(hidden)/core/modules/file/lib/Drupal/file/FileUsage/DatabaseFileUsageBackend.php on line 73
Call Stack
#TimeMemoryFunctionLocation
10.0001660768{main}( )../index.php:0
20.0014953416drupal_handle_request( )../index.php:15
30.02433658736Drupal\Core\DrupalKernel->handle( )../bootstrap.inc:1862
40.02503761008Drupal\Core\HttpKernel->handle( )../DrupalKernel.php:281
50.02503762664Symfony\Component\HttpKernel\HttpKernel->handle( )../HttpKernel.php:52
60.02503762664Symfony\Component\HttpKernel\HttpKernel->handleRaw( )../HttpKernel.php:61
70.06897917464call_user_func_array:{/(hidden)/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/HttpKernel.php:117} ( )../HttpKernel.php:117
80.06897918080Drupal\Core\Entity\HtmlEntityFormController->content( )../HttpKernel.php:117
90.06897918080Drupal\Core\Controller\HtmlFormController->content( )../HtmlEntityFormController.php:25
100.07008114096drupal_build_form( )../HtmlFormController.php:68
110.08489918880drupal_process_form( )../form.inc:419
120.113111691752form_execute_handlers( )../form.inc:935
130.113111692936call_user_func_array:{/(hidden)/core/includes/form.inc:1567} ( )../form.inc:1567
140.113111693000Drupal\node\Form\NodeDeleteForm->submit( )../form.inc:1567
150.113211693000Drupal\Core\Entity\Entity->delete( )../NodeDeleteForm.php:94
160.113211693432Drupal\Core\Entity\DatabaseStorageControllerNG->delete( )../Entity.php:436
170.122912128800Drupal\Core\Entity\EntityStorageControllerBase->invokeFieldMethod( )../DatabaseStorageControllerNG.php:598
180.123112133384Drupal\Core\Entity\Field\Field->delete( )../EntityStorageControllerBase.php:148
190.123112133520Drupal\Core\Entity\Field\Field->delegateMethod( )../Field.php:327
200.123112133520Drupal\file\Plugin\field\field_type\FileItem->delete( )../Field.php:346
210.123412145760Drupal\file\FileUsage\DatabaseFileUsageBackend->delete( )../FileItem.php:222

Reproduced on several different installs on several machines. Does not happen on a Page node, and it is apparently caused by the file field on articles.

I tried to reproduce it in a webtest, but strangely enough it does not trigger the problem. The patch for the test (which should fail but succeeds) is in the next comment.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

fgm’s picture

Status: Active » Needs review
FileSize
2.55 KB

Test reproducing the steps which fail in the UI, so should fail, but actually passes on my machine. Maybe an error in the test itself.

-enzo-’s picture

I reproduce the error with the same conditions mentioned.

[26-Sep-2013 21:43:15 Europe/Berlin] PHP Fatal error: Call to a member function id() on a non-object in /Users/enzo/www/drupal8/core/modules/file/lib/Drupal/file/FileUsage/DatabaseFileUsageBackend.php on line 73

-enzo-’s picture

Hey folks

After run a debug backtrace I detect the problem is in file core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileItem.php class Drupal\file\FileUsage\DatabaseFileUsageBackend and method delete.

The class don't sent the entity object properly

  /**
   * {@inheritdoc}
   */
  public function delete() {
    // @todo Move in FileField in https://drupal.org/node/2073033.
    $entity = $this->getRoot();

    // Delete all file usages within this entity.
    file_usage()->delete($this->entity, 'file', $entity->entityType(), $entity->id(), 0);
  }

So the problem is in the line $entity = $this->getRoot();

I don't know where is this method located, if someone can provide an advice about how to fix this issue, I could try.

Berdir’s picture

It should probably use getEntity() now, there shouldn't be a difference between the two but you can try?

getRoot() is implemented in TypedData, but that's pretty generic code.

netsensei’s picture

I'm looking at this same error since noon. I thought it was something on my machine, but apparently it's the code.

I changed the delete() method to read like this:

  public function delete() {
    // @todo Move in FileField in https://drupal.org/node/2073033.
    $entity = $this->getRoot();

    // Delete all file usages within this entity.
    
    file_usage()->delete($this->getEntity(), 'file', $entity->entityType(), $entity->id(), 0);
  }

But that triggers this error:

Fatal error: Call to undefined method Drupal\node\Entity\Node::setTemporary() in /Users/netsensei/Workspace/drupal/www/core/modules/file/lib/Drupal/file/FileUsage/FileUsageBase.php on line 36
Berdir’s picture

I meant to replace getRoot() with getEntity(), not ->entity. getEntity() is badly named for reference fields :(

-enzo-’s picture

Status: Needs review » Needs work

I tried

$entity = $this->getEntity();

and

$entity = getEntity();

Same error any glue?

netsensei’s picture

Status: Needs work » Needs review

I see. Seems to me that it's getting the entity to which the field is attached rather then the entities it's referencing. So, I agree: the method name definitely needs to be looked at.

netsensei’s picture

Status: Needs review » Needs work

Ow, sorry! :-(

Berdir’s picture

Priority: Normal » Critical
Status: Needs work » Needs review
Issue tags: +Needs tests

Ok, ignore what I said above.

The problem is that we don't have a file reference but we try to remove file usage of it. This is also related to #1988492: Avoid having empty field items by default.

The refactoring in #2073033: Optimize file usage updates in file/image fields fixes this bug, but we need test coverage.

Easy to reproduce fatal errors are critical, so...

claudiu.cristea’s picture

Priority: Critical » Normal
Status: Needs review » Closed (duplicate)

#2073033: Optimize file usage updates in file/image fields will fix this too. Marked that as critical and this as duplicate.

EDIT: Also added test there to prove the failure/fix.

fgm’s picture

Issue summary: View changes
Status: Closed (duplicate) » Fixed

Resetting to fixed so that whoever cared about this gets notification that the refactor in #2073033: Optimize file usage updates in file/image fields actually fixed this issue as expected.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.