-
-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: Add relativePath
or themePath
to Asset
#227
Comments
Assets don't know what a theme is and have no direct relation to the theme. So I'm unlikely to add that sort of logic directly into the asset object. But I'm not opposed to determining the relative path to the asset from the manifest (which is I'll give it a bit of a think, but in the meantime, you can use something like this: app('files')->getRelativePath(get_theme_file_path(), $asset->path()); |
@QWp6t: Excellent point! The The approach with |
@QWp6t: |
@QWp6t: For now I solved this by instantiating the returned add_action('after_setup_theme', function () {
$fs = app('files');
$afs = new \Roots\Acorn\Filesystem\Filesystem($fs);
$asset = asset('app.css');
$relCssPath = $afs->getRelativePath(get_template_directory(), $asset->path());
add_editor_style($relCssPath);
}); However, the The most recent function of Symfony to get a relative path is It may make sense using the Symfony relative path function and add a fix for paths that don't end with a slash for the |
Looks like there was a misconfiguration in 2.x, which I've addressed in #235 if you want to pull that in and test (or update your config/app.php file accordingly). As for - app('files')->getRelativePath(get_theme_file_path(), asset('app.css')->path())
+ app('files')->getRelativePath(trailingslashit(get_theme_file_path()), asset('app.css')->path()) (apologies for the ugly terminal lol) Let me know if that works for you. PS- If you're unsure, no worries. I'm just a little curious. |
Tests for From the class names,
Judging from both of their respective tests, except for these two points, they should be equal in their behavior. |
@QWp6t: Yes, applying the PR branch fixes it: "require": {
"roots/acorn": "dev-227-use-acorn-filesystem"
} <?php
namespace App;
use function Roots\asset;
add_action('after_setup_theme', function () {
// Add frontend styles as editor styles
// Must be added by relative path (not remote URI)
// @see https://core.trac.wordpress.org/ticket/55728#ticket
// @see https://github.com/roots/acorn/issues/227#issuecomment-1166613881
$relCssPath = app('files')->getRelativePath(trailingslashit(get_theme_file_path()), asset('app.css')->path());
add_editor_style($relCssPath);
}); A patch-level release of |
Config option for |
With the newly added namespace App;
use function Roots\asset;
add_action('after_setup_theme', function () {
// Add frontend styles as editor styles
// Must be added by relative path (not remote URI)
// (@see https://core.trac.wordpress.org/ticket/55728#ticket)
$relCssPath = asset('app.css')->relativePath(get_theme_file_path());
add_editor_style($relCssPath);
}); Great work @QWp6t, thanks! |
Terms
Summary
Asset
currently has apath()
function that returns the full, absolute path to the asset.Sometimes the path relative to the theme directory is also needed, as for
add_editor_style
.Currently, additional code/library is needed to get the path relative to theme directory from the absolute path returned by the
path()
function.It would be nice if also a function is added that returns the path to asset relative to the (Sage) theme directory.
Motivation
Why are we doing this?
Sometimes the path relative to the theme directory is also needed.
What use cases does it support?
add_editor_style
requires either an URI (that currently cause an issue post-processing CSS in Gutenberg editor to rewrite URLs relative to stylehsheet) or a path to a local file relative to the theme directory (that doesn't cause this issue).What is the expected outcome?
Elimination of additional code/library for making the absolute asset path (from
path()
) relative to the theme directory path,so it can be used by functions like
add_editor_style
.Potential conflicts / foreseeable issues
One more API function in
acorn
, paths and their handling shouldn't change in the foreseeable future, so once the code (and tests) for a theme-directory-relative path function is in place, no changes should be expected any time soon.As this functionality would be requires in any theme that adds the frontend styles in the expected way (using
add_editor_style
), this should justify adding such an additional function.Additional Context
https://discourse.roots.io/t/absolute-domain-relative-path-for-asset-urls-in-css/23121/4
The text was updated successfully, but these errors were encountered: