Bobcares

“Call to Undefined Function file_create_url()” Error in Drupal 9 & 10

by | Feb 17, 2025

Learn how to fix “Call to Undefined Function file_create_url()” error in Drupal 9 & 10. Our Drupal Support team is here to help you with your questions and concerns.

“Call to Undefined Function file_create_url()” Error in Drupal 9 & 10

According to our Experts, the “Call to undefined function file_create_url()” error is a critical issue in Drupal 9 and 10. It prevents proper file URL generation.

This occurs because `file_create_url()` was deprecated in Drupal 9.3.0 and fully removed in Drupal 10.0.0. Developers must now use alternative methods to generate file URLs in newer Drupal versions.

drupal call to undefined function file_create_url ()

A typical error message looks like this:

Fatal error: Call to undefined function file_create_url() in [file_path] on line [line_number]

Impacts of the Error

  • Prevents correct file URL generation for images and documents.
  • Breaks custom themes and modules using the outdated function.
  • Requires immediate refactoring of custom modules and themes.
  • Demands a full code review to remove deprecated function calls.
  • It may break backward compatibility with older Drupal versions.
  • Potential security risks if not addressed promptly.
  • Reduced website performance due to failed file handling.
  • Increased risk of compatibility issues between Drupal versions.

Causes and Fixes

1. Outdated Function Usage

Custom modules or themes still use `file_create_url()`.

Click here for the Solution.
  1. Identify all occurrences of `file_create_url()` in the codebase.
  2. Replace `file_create_url()` with the modern service-based approach.


    public function generateFileUrl($uri) {
    return \Drupal::service('file_url_generator')->generateAbsoluteString($uri);
    }

  3. Test thoroughly after modifications.

2. Legacy Code Compatibility

Code written for older Drupal versions still relies on deprecated methods.

Click here for the Solution.
  1. Conduct a full code audit.
  2. Implement constructor dependency injection.


    class FileHandler {
    protected $fileUrlGenerator;
    public function __construct(FileUrlGeneratorInterface $fileUrlGenerator) {
    $this->fileUrlGenerator = $fileUrlGenerator;
    }
    public function processFile($uri) {
    return $this->fileUrlGenerator->generateAbsoluteString($uri);
    }
    }

  3. Remove global function calls.

3. Theme Template Compatibility

Custom themes using outdated file URL generation methods.

Click here for the Solution.

Update `.theme` files to use the new approach.


function mytheme_preprocess_image(&$variables) {
$variables['url'] = \Drupal::service('file_url_generator')->generateString($variables['uri']);
}

Best Practices:

  • Use service containers consistently.
  • Avoid direct global function calls.
  • Implement proper error handling.

4. Plugin Incompatibility

Contributed plugins not updated for Drupal 10.

Click here for the Solution.

Update or replace plugins using modern URL generation methods.


class MyCustomPlugin extends PluginBase {
public function generateUrl($uri) {
return $this->fileUrlGenerator->generateAbsoluteString($uri);
}
}

Update Checklist:

  • Check plugin compatibility.
  • Modify base classes to use new services.
  • Ensure thorough testing.

5. Module Dependency Issues

Hardcoded deprecated function calls in custom modules.

Click here for the Solution.

Refactor module code to use `file_url_generator` service.


public function createModuleFileUrl($uri) {
return \Drupal::service('file_url_generator')->generateAbsoluteString($uri);
}

Guidelines:

  • Implement dependency injection.
  • Define service wrappers for file handling.
  • Remove all global function calls.

6. Custom Module Challenges

Procedural file URL generation in custom modules.

Click here for the Solution.

Implement a service-based URL generation approach.


class CustomFileHandler {
protected $fileUrlGenerator; public function __construct(FileUrlGeneratorInterface $fileUrlGenerator) { $this->fileUrlGenerator = $fileUrlGenerator; } public function processFileUrl($uri) { return $this->fileUrlGenerator->generateString($uri); } }

Best Practices:

  • Use interface-based dependency injection.
  • Also, create abstraction layers.
  • Implement robust error handling.

7. Configuration and Installation Problems

Incorrect Drupal installation leading to missing dependencies.

Click here for the Solution.

Perform a clean installation and validate configurations.

  1. Backup existing configurations.
  2. Reinstall Drupal 10 cleanly.
  3. Then, restore configurations incrementally.
  4. Validate file URL generation functionality.

Validation Steps:

  • Test URL generation with different file types.
  • Additionally, verify absolute and relative URLs.
  • Also, conduct system-wide testing.

Prevention Strategies

  • Regularly update Drupal core and contributed modules.
  • Additionally, static analysis tools are used to detect deprecated function usage.
  • Also, implement Rector for automatic code upgrades.
  • Furthermore, inject the `file_url_generator` service into controllers and services.
  • Use constructor dependency injection.


    public function __construct(FileUrlGeneratorInterface $fileUrlGenerator) {
    $this->fileUrlGenerator = $fileUrlGenerator;
    }

  • Set up automated testing for deprecation warnings.
  • Use static code analysis tools.
  • Also, conduct regular code reviews.
  • Always check module and theme compatibility with the current Drupal version.
  • Additionally, test thoroughly after upgrades.

[Need assistance with a different issue? Our team is available 24/7.]

Conclusion

To resolve “Call to undefined function file_create_url()” error, developers must adopt modern service-based approaches for file URL generation. Implementing best practices such as dependency injection, systematic code updates, and continuous integration will ensure smooth functionality.

In brief, our Support Experts demonstrated how to fix “Call to Undefined Function file_create_url()” error in Drupal 9 & 10.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF