Problem/Motivation

\Drupal\file\Element\ManagedFile::valueCallback() is a static method that calls \Drupal::currentUser() and \Drupal::service('private_key') directly. This makes the logic hard to unit test and inconsistent with the OOP goals of the file upload initiative (#3221796: [META] Modernise file upload logic).

It also calls the procedural file_managed_file_save_upload(), one of the functions targeted for deprecation in #3375423: Deprecate file_managed_file_save_upload(), file_save_upload() and _file_save_upload_from_form() and replace with a service. That issue previously stalled from trying to do too much in one change (comment #40, comment #42). Splitting this piece out keeps each change reviewable.

This issue is blocked on #3616645: Form #value_callback does not support CallableResolver-style callables, which allows #value_callback to use CallableResolver-style callables.

Steps to reproduce

Proposed resolution

Move the logic into a new constructor-injected service, Drupal\file\Upload\ManagedFileValueResolver::resolveValue(), autowired for AccountInterface, PrivateKey, and EntityTypeManagerInterface. Wire it via ManagedFile::getInfo()'s #value_callback. Keep ManagedFile::valueCallback() as a deprecated wrapper for BC, since it is public API that contrib code may call directly, even though FormElementBase already provides a default implementation.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3616654

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

kim.pepper created an issue. See original summary.

kim.pepper’s picture

Title: Convert ManagedFile::valueCallback() logic into an injectable service » [PP-1] Convert ManagedFile::valueCallback() logic into an injectable service
kim.pepper’s picture

Title: [PP-1] Convert ManagedFile::valueCallback() logic into an injectable service » [PP-2] Convert ManagedFile::valueCallback() logic into an injectable service
kim.pepper’s picture