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

Created on 18 October 2023, about 1 year ago
Updated 19 October 2023, about 1 year 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 3 days ago

Created by

πŸ‡³πŸ‡±Netherlands Tr4nzNRG

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

Comments & Activities

Production build 0.71.5 2024