Skip to Main Content
Menu
Close Menu

Updating deprecated file_prepare_directory() calls in Drupal 8

Summary

This covers only the most common use cases.

Replace:

file_prepare_directory($directory, $options);

With:

\Drupal::service('file_system')->prepareDirectory($directory, $options);

A little more information

If you are using options

Replace:

file_prepare_directory('/test/directory', FILE_CREATE_DIRECTORY);

With:

\Drupal::service('file_system')->prepareDirectory('/test/directory', \Drupal\Core\File\FileSystemInterface::CREATE_DIRECTORY);

Note: The static variable will be updated by the FileCreateDirectoryRector: https://www.palantir.net/rector/updating-deprecated-filecreatedirectory-calls-drupal-8

More examples

We have a set of examples as part of the Drupal Rector rector_examples module. See https://github.com/palantirnet/drupal-rector/tree/master/rector_examples.

Automating updates with Drupal Rector

Drupal Rector is an open source tool built using Rector which can help automate this deprecation as well as other common deprecations.

For more information, see https://github.com/palantirnet/drupal-rector.

What is covered by the rector

Drupal Rector currently replaces static calls to file_prepare_directory() as shown above.

Improvement opportunities

Drupal Rector does not inject the file_system service into classes.

Deprecation background

file_prepare_directory() used a global method call rather than a service, making unit testing more difficult.

References

The Drupal change record for this deprecation is available at https://www.drupal.org/node/3006851

Credits and thank yous

Thank you to Kelsey Bentham for leading Drupal Rector development to support this deprecation.

Thank you to Palantir.net for sponsoring this development.

Let's work together.

Have an exceptional idea? Let's talk and see how we can help.
New Call-to-action