Add stream wrappers to access extension files

Created on 13 October 2011, about 14 years ago
Updated 8 January 2024, almost 2 years ago

Problem/Motivation

Starting from #2351919: Replace uses of drupal_get_path() with __DIR__ where possible β†’ , when the PHP code needs to include or parse files inside the module or theme directory space, will simply use _DIR_ instead of drupal_get_path(). This is sufficient, intuitive and more performant than calling drupal_get_path().

However, drupal_get_path() is widely used to refer files (.js, .css, images, assets) outside a module or theme. This isn't very intuitive for new developers. Stream wrappers make much more sense and simplify the API for developers trying to refer directories or files from outside the current extension in code.

Proposed resolution

Introduce extension stream wrappers:

Examples

Profile: profile://

Assuming the standard profile is installed:

  • Referring a directory:
    • Actual: drupal_get_path('profile', 'standard') . '/config'
    • Proposed: profile://config
    • Path: core/profiles/standard/config
  • Referring a file:
    • Actual: drupal_get_path('profile', 'standard') . '/config/install/automated_cron.settings.yml'
    • Proposed: profile://config/install/automated_cron.settings.yml
    • Path: core/profiles/standard/config/install/automated_cron.settings.yml

Module: module://

Assuming the node module is enabled but color module is not:

  • Referring a directory:
    • Actual: drupal_get_path('module', 'node') . '/config'
    • Proposed: module://node/config
    • Path: core/modules/node/config
  • Referring a file:
    • Actual: drupal_get_path('module', 'node') . '/config/install/node.settings.yml'
    • Proposed: module://node/config/install/node.settings.yml
    • Path: core/modules/node/config/install/node.settings.yml
  • Referring a resource in a uninstalled or inexistent module:
    • Actual: drupal_get_path('module', 'color') . '/config'
    • Proposed: module://color/config
    • Path: Throws \RuntimeException

Theme: theme://

Assuming the bartik theme is installed but seven theme is not:

  • Referring a directory
    • Actual: drupal_get_path('theme', 'bartik') . '/config'
    • Proposed: theme://bartik/config
    • Path: core/themes/bartik/config
  • Referring a file
    • Actual: drupal_get_path('theme', 'bartik') . '/color/color.inc'
    • Proposed: theme://bartik/color/color.inc
    • Path: core/themes/bartik/color/color.inc
  • Referring a resource in a uninstalled or inexistent theme:
    • Actual: drupal_get_path('theme', 'seven') . '/config'
    • Proposed: theme://seven/config
    • Path: Throws \RuntimeException

Remaining tasks

  1. Profiling to compare performance of the new code with the old.

API changes

New abstract class for extensions stream wrappers:

  • \Drupal\Core\StreamWrapper\ExtensionStreamBase

New stream wrapper classes:

  • \Drupal\Core\StreamWrapper\ModuleStream
  • \Drupal\Core\StreamWrapper\ThemeStream
  • \Drupal\Core\StreamWrapper\ProfileStream

Data model changes

None.

✨ Feature request
Status

Needs work

Version

10.0 ✨

Component
File systemΒ  β†’

Last updated 2 months ago

Created by

πŸ‡ΊπŸ‡ΈUnited States dave reid Nebraska USA

Live updates comments and jobs are added and updated live.
  • needs profiling

    It may affect performance, and thus requires in-depth technical reviews and profiling.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

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

  • πŸ‡©πŸ‡ͺGermany geek-merlin Freiburg, Germany

    Bump...

  • last update over 1 year ago
    Custom Commands Failed
  • πŸ‡§πŸ‡ͺBelgium dtrdewaele

    This version of the patch works for Drupal 10.3

  • Status changed to RTBC over 1 year ago
  • πŸ‡¨πŸ‡¦Canada SKAUGHT

    patch 404 applies as expected on Drupal 11.0.x.

    There is something wrong with gitlab. I can not start a new branch or pull in current dev head to get this patch into. This is probably why 404 is a file.

  • Status changed to Needs work over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

    A patch can't be RTBC. Back to NW.

  • πŸ‡³πŸ‡΄Norway steinmb

    Needs a manual rebase against 11.x

  • First commit to issue fork.
  • πŸ‡ΊπŸ‡ΈUnited States trackleft2 Tucson, AZ πŸ‡ΊπŸ‡Έ

    Target branch in MR needs to be updated to 11.x

  • Pipeline finished with Failed
    9 months ago
    Total: 153s
    #408638
  • πŸ‡ΊπŸ‡ΈUnited States trackleft2 Tucson, AZ πŸ‡ΊπŸ‡Έ

    Target branch currently set to 9.4.x

  • πŸ‡ΊπŸ‡ΈUnited States rpayanm

    rpayanm β†’ changed the visibility of the branch 11.x to hidden.

  • Pipeline finished with Failed
    9 months ago
    Total: 136s
    #419254
  • Pipeline finished with Failed
    9 months ago
    Total: 549s
    #419261
  • Pipeline finished with Failed
    9 months ago
    Total: 436s
    #419277
  • Pipeline finished with Failed
    9 months ago
    Total: 591s
    #419293
  • Pipeline finished with Canceled
    8 months ago
    Total: 317s
    #447554
  • Pipeline finished with Canceled
    8 months ago
    Total: 219s
    #447563
  • Pipeline finished with Success
    8 months ago
    Total: 411s
    #447568
  • Pipeline finished with Success
    7 months ago
    Total: 1215s
    #463783
  • Pipeline finished with Failed
    4 months ago
    #553915
  • Pipeline finished with Failed
    4 months ago
    #553947
  • Pipeline finished with Failed
    3 months ago
    Total: 481s
    #557828
  • Pipeline finished with Failed
    3 months ago
    Total: 3687s
    #568784
  • Pipeline finished with Failed
    3 months ago
    Total: 3694s
    #568809
  • Pipeline finished with Success
    3 months ago
    Total: 7433s
    #569203
  • Pipeline finished with Skipped
    3 months ago
    #569603
  • Pipeline finished with Failed
    19 days ago
    Total: 482s
    #632150
  • Pipeline finished with Failed
    19 days ago
    Total: 242s
    #632321
  • Pipeline finished with Failed
    19 days ago
    Total: 252s
    #632364
  • Pipeline finished with Failed
    18 days ago
    Total: 627s
    #632775
  • Pipeline finished with Failed
    15 days ago
    Total: 674s
    #636226
  • Pipeline finished with Failed
    15 days ago
    Total: 612s
    #636307
  • Pipeline finished with Failed
    15 days ago
    Total: 562s
    #636480
  • Pipeline finished with Failed
    14 days ago
    Total: 363s
    #636984
  • Pipeline finished with Failed
    14 days ago
    Total: 293s
    #636991
  • Pipeline finished with Success
    14 days ago
    Total: 456s
    #636995
  • Pipeline finished with Failed
    14 days ago
    Total: 6499s
    #637014
  • Pipeline finished with Failed
    14 days ago
    Total: 499s
    #637253
  • Pipeline finished with Failed
    14 days ago
    Total: 684s
    #637263
  • Pipeline finished with Canceled
    8 days ago
    Total: 135s
    #643785
  • Pipeline finished with Canceled
    8 days ago
    Total: 127s
    #643786
  • Pipeline finished with Success
    8 days ago
    Total: 912s
    #643789
  • Pipeline finished with Canceled
    8 days ago
    Total: 607s
    #643840
  • Pipeline finished with Canceled
    8 days ago
    Total: 68s
    #643857
  • Pipeline finished with Failed
    8 days ago
    Total: 559s
    #643858
  • Pipeline finished with Success
    8 days ago
    Total: 581s
    #643871
  • Pipeline finished with Canceled
    3 days ago
    Total: 466s
    #647844
  • Pipeline finished with Failed
    3 days ago
    Total: 781s
    #647853
  • Pipeline finished with Canceled
    3 days ago
    Total: 224s
    #647887
  • Pipeline finished with Success
    3 days ago
    Total: 822s
    #647890
  • Pipeline finished with Canceled
    3 days ago
    Total: 178s
    #647923
  • Pipeline finished with Canceled
    3 days ago
    Total: 160s
    #647930
  • Pipeline finished with Canceled
    3 days ago
    Total: 127s
    #647932
  • Pipeline finished with Canceled
    3 days ago
    Total: 161s
    #647934
  • Pipeline finished with Failed
    3 days ago
    Total: 666s
    #647935
  • Pipeline finished with Failed
    3 days ago
    Total: 568s
    #647949
  • Pipeline finished with Failed
    3 days ago
    Total: 626s
    #647969
  • Pipeline finished with Success
    about 9 hours ago
    Total: 616s
    #651183
  • Pipeline finished with Success
    about 8 hours ago
    Total: 3230s
    #651229
Production build 0.71.5 2024