Problem/Motivation

If you import data using:

    $file_data = file_get_contents($url);
    $file_repository = \Drupal::service('file.repository');
    $file = $file_repository
      ->writeData($file_data,
                  "public:/{$urlPath}",
                  FileSystemInterface::EXISTS_REPLACE);
  }

You will get the following error on the writeData method call:

Error: Call to a member function isEmpty() on null in /app/docroot/modules/contrib/filefield_paths/filefield_paths.module on line 364 #0 [internal function]: filefield_paths_file_presave(Object(Drupal\file\Entity\File))

Steps to reproduce

Use the above code to try to import a file.

Proposed resolution

The presave function should handle cases where the originname parameter does not exist.

Remaining tasks

Create a patch

User interface changes

None

API changes

None

Data model changes

None

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

cgmonroe created an issue. See original summary.

cgmonroe’s picture

Status: Active » Needs review
StatusFileSize
new915 bytes

Patch to fix this problem

Status: Needs review » Needs work

The last submitted patch, 2: filefield_paths-call_isEmpty_on_null-3339688-2.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

cgmonroe’s picture

Status: Needs work » Needs review
StatusFileSize
new910 bytes

Hmm, not sure why the token test failed. Trying a different variation that just skips setting the name if the originame parameter does not exist.

megadesk3000’s picture

Hi togethter

I had the same error when creating a new media image via the backend ui on /media/add/image, after i installed filefield_paths module.
For me it was the case, that $file->filename was not present and therefore the same error came up.

I adjusted the patch slighty and now it works in my case.

Added a patch and an interdiff.

Status: Needs review » Needs work

The last submitted patch, 5: filefield_paths-call_isEmpty_on_null-3339688-5.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

tim@lammar.be’s picture

Rework for version: 1.0.0-beta8

pwolanin’s picture

We suddenly started getting this error - trying to track down why, maybe something wrong will file system permissions. It solved the issue enough to get things working and is clearly a pretty safe change

pwolanin’s picture

Status: Needs work » Needs review

pwolanin’s picture

I made an issue fork and included patch #7

klemendev’s picture

I can also confirm this. I will try the patch later

voleger’s picture

Status: Needs review » Fixed

Fixed, thanks

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • voleger committed 96430b38 on 8.x-1.x authored by pwolanin
    Issue #3339688 defend against fatal error in hook_file_presave()
    

Status: Fixed » Closed (fixed)

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