Error: Call to undefined function Drupal\file\Element\file_move()

Created on 18 October 2023, 11 months ago
Updated 19 October 2023, 11 months ago

Problem/Motivation

With Drupal 10.1.3 I encounter an PHP error message when trying to replace an existing private file or when uploading a private file:

Error: Call to undefined function Drupal\file\Element\file_move() in Drupal\file\Element\ManagedFile::valueCallback() (line 110 of /srv/shared/builds/tilburg-university.edu-42/htdocs/core/modules/file/src/Element/ManagedFile.php).

According to this change record: https://www.drupal.org/node/3223520 β†’ this function has been removed since Drupal 9.3.0 and replaced by a service.

file: modules/file/src/Element/ManagedFile.php

        // Move files to another uri scheme if a change is detected.
        // Depends on $fids having only 1 entry.
        if (!empty($fid) && isset($input['uri_scheme']) && count($fids) === 1) {
          $file = File::load($fid);
          $current_uri_scheme = explode('://', $file->getFileUri())[0];
          if ($input['uri_scheme'] !== $current_uri_scheme && file_exists($file->getFileUri())) {
            $destination = str_replace($current_uri_scheme . '://', $input['uri_scheme'] . '://', $file->getFileUri());
            $directory = \Drupal::service('file_system')->dirname($destination);
            if (\Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY)) {
              file_move($file, $destination);
            }
          }
        }

Steps to reproduce

There are multiple ways:

  1. Upload a file as public.
  2. Change upload destination from public to private.
  1. Replace an existing private file with another file.
  1. Upload a file as private.

Proposed resolution

Double check core for deprecated functions mentioned in: https://www.drupal.org/node/3223520 β†’
Replace them with the new service.

πŸ’¬ Support request
Status

Closed: works as designed

Version

10.1 ✨

Component
File systemΒ  β†’

Last updated 1 day ago

Created by

πŸ‡³πŸ‡±Netherlands Tr4nzNRG

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Tr4nzNRG
  • Status changed to Postponed: needs info 11 months ago
  • πŸ‡ΊπŸ‡ΈUnited States cilefen

    There are no calls to function file_move in Drupal 10.1. Do you have patches applied?

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10
  • πŸ‡³πŸ‡±Netherlands Tr4nzNRG

    @cilefan was correct.

    (for anyone else that runs in this situation)

    We recently migrated from Drupal 9 > Drupal 10. For D9 we had a patch to solve the issue I'm describing (changing the public/private upload destination). This patch needs an update to fix the problem for Drupal 10.

    Patch info: https://www.drupal.org/project/drupal/issues/1202074#comment-14887638 ✨ Toggleable public/private upload Needs work

    This issue can be closed.

  • Status changed to Closed: works as designed 11 months ago
  • πŸ‡ΊπŸ‡ΈUnited States cilefen
Production build 0.71.5 2024