Problem/Motivation

componentMetadata.path gives the path relative to the Drupal root. If the Drupal site is not at the root of the host, this is not enough to get full path to the component. It would be missing the base_path().

Example use case: A SDC which provides markup that refers to an image that is included in the SDC. The src attribute would need to include the full path to the image file.

Proposed resolution

Create componentMetadata.fullPath which is like base_path() . componentMetadata.path. Alternatively, create componentMetadata.base_path

Remaining tasks

Decide which to do. Implement.

User interface changes

None.

Introduced terminology

None.

API changes

Addition of componentMetadata.fullPath.

Data model changes

None.

Release notes snippet

Issue fork drupal-3567767

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

liam morland created an issue. See original summary.

liam morland’s picture

Issue summary: View changes
Status: Active » Needs review

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

smustgrave’s picture

pdureau’s picture

Thanks Liam for your contribution.

Because you sue base_path(), I guess the distinction is not between path and fullPath but about server and web path.

Looking at Core and some contrib themes, it seems componentMetadata.path is rarely used:

  • 26 times total on 1184 components
  • in 6 themes (mercury, nttdata_theme, umami, drupal_cms_olivero, governor, etc...) on 40

The usage is split between:

  • Server side path: {{ include (componentMetadata.path ~ '/images/arrow.svg') }}, {{ source(componentMetadata.path ~ '/icons/' ~ icon|default('knife') ~ '.svg') }}...
  • Web side path : <img src="{{ componentMetadata.path ~ '/' ~ media.src }}">...

If I understand well, componentMetadata.path is the good solution for server side path, but works on Web side (href, src...) only if base_path() === "/". That's it?

liam morland’s picture

Yes, there currently isn't a way to get the base_path() inside an SDC.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Maybe a test case showing why this is needed will help, regardless will need test coverage

liam morland’s picture

Issue summary: View changes

I have added an example use case to the issue summary. Tests are still needed.

oily’s picture

Re: #9 Does this require a CR? The example could be added there, too?

oily’s picture

Would core/tests/Drupal/FunctionalTests/Theme/OliveroTest.php be a place to put the test?