Remove unnecessary calls to drupal_get_path(), replace with __DIR__

Created on 15 June 2020, over 4 years ago
Updated 18 August 2024, 5 months ago

Problem/Motivation

drupal_get_path() allows you to find the location of a module via the module name - this accounts for the different possible locations where a module could be installed ( contrib/, sites/SITENAME/modules/contrib etc.)

However, there are multiple uses on core where it's not necessary, because the call is inside the module being referred to.

In these cases, just __DIR__ returns the same thing (or sometimes __DIR__ . '../' if the call is in a subdirectory of the module) without having to go through all the Drupal logic.

When we eventually deprecate drupal_get_path(), this will reduce the number of replacements that need to happen. It should also make it clearer what use-cases the replacement actually needs to solve.

An obvious example is this one in statistics.module:

$settings = ['data' => ['nid' => $node->id()], 'url' => \Drupal::request()->getBasePath() . '/' . drupal_get_path('module', 'statistics') . '/statistics.php'];

A previous attempt was made in #2351919: Replace uses of drupal_get_path() with __DIR__ where possible β†’ , but either it didn't catch everything, or new uses have crept in.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Closed: outdated

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated about 5 hours ago

Created by

πŸ‡¬πŸ‡§United Kingdom catch

Live updates comments and jobs are added and updated live.
  • Novice

    It would make a good project for someone who is new to the Drupal contribution process. It's preferred over Newbie.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024