Updated: Comment #179
drupal_get_path() infiltrates almost every module and isn't very intuitive for new developers.
Stream wrappers make much more sense and simplify the API for developers trying to refer to image, JS, CSS and other assets files in code.
Stream wrappers will make it easier to reference JS and CSS files from yml files in modules, e.g. *.libraries.yml and will support the API to be introduced by
Consensus is to go with the module:// theme:// and profile:// streamwrappers. Replace drupal_get_path() with stream wrappers in strings that are evaluated for a module, theme or profile path. Documentation will specify that this is *not* to be used to include PHP files.
Before patch examples:
_drupal_add_css(drupal_get_path('module', 'theme_test') . '/css/base-override.css');
$render_array['#attached']['css'] = drupal_get_path('module', 'theme_test') . '/css/base-override.css';
After patch examples:
$render_array['#attached']['css'] = 'module://theme_test/css/base-override.css';
There are 4 possible use cases:
Asset files attached in PSR-4/PSR-0 classes:
In a \Drupal\foo_module\Form\FooEntityForm where assets are attached,
$form['#attached']['css'] = 'module://foo_module/js/foo_bar.js'; is a far better DX than
$form['#attached']['css'] = __DIR__ . '/../../js/foo_bar.js'; or even
$form['#attached']['css'] = drupal_get_path('module', 'foo_module') . '/js/foo_bar.js';
Modules that provide resources consumed by other modules or themes
- color module
- ckeditor module
Modules that use resources provided by other modules
Mainly in contrib, these would utilize js, css and other files from modules in core.
Hooks for asset management
hook_library_info_alter(), hook_library_alter(), hook_css_alter(), etc. existing examples in system.api.php
The real benefit of this issue is the DX win of using "module://foo_module/js/foo_bar.js" which makes it easier to understand the code.
Replacement of most / all uses of drupal_get_path calls (separate issue). This can be scripted.
Simplifies getting paths to modules, themes and profiles by using a stream wrapper.
Original report by @Dave Reid
I wrote http://drupal.org/project/system_stream_wrapper and now let's merge it into core! It's super helpful to be able to access files via module://modulename/path/to/file.txt which nicely resolves to the actual file location.
FAILED: [[SimpleTest]]: [PHP 5.4 MySQL] 87,415 pass(es), 1 fail(s), and 5 exception(s).
FAILED: [[SimpleTest]]: [PHP 5.4 MySQL] 78,850 pass(es), 1 fail(s), and 4 exception(s).
FAILED: [[SimpleTest]]: [PHP 5.4 MySQL] Unable to apply patch add_module_theme-1308152-205.patch. Unable to apply patch. See the log in the details link for more information.