Someone recently asked the following in Slack. I didn't want it to get lost in Slack's history, so I thought I'd post it here:
Question: I’m setting a css background image inside my Pattern Lab footer template which displays correctly in Pattern Lab; however, Drupal isn’t locating the image. How is sharing images between PL and Drupal supposed to work?
My Answer: I’ve been using Pattern Lab’s built-in data.json files to handle this lately. e.g. you could do something like:
footer-component.twig
:
...
{% footer_background_image = footer_background_image|default('/path/relative/to/drupal/root/footer-image.png') %}
...
This makes the image load for Drupal, but fails for Pattern Lab.
At first, to fix that, we used the footer-component.yml
file to set the path relative to PL. e.g.:
footer-component.yml
:
footer_background_image: /path/relative/to/pattern-lab/footer-image.png
The problem with this is that on every Pattern Lab page, when we included the footer copmonent, we had to add that line to the yml file for the page. e.g:
basic-page.twig
:
...
{% include /whatever/footer-component.twig %}
...
basic-page.yml
:
...
footer_background_image: /path/relative/to/pattern-lab/footer-image.png
...
Rinse and repeat for each example page... That’s annoying.
Then we realized we could take advantage of Pattern Labs global data files.
So with the same footer-component.twig
file as above, we can skip the yml files, and just add the following to a data file.
theme/components/_data/paths.json
: (* see P.S. below)
{
"footer_background_image": "/path/relative/to/pattern-lab/footer-image.png"
}
Now, we can include the footer component in any example Pattern Lab pages we want, and the image is globally replaced in all of them. Also, Drupal doesn’t know about the json files, so it pulls the default value, which of course is relative to the Drupal root. So it works in both places.
We did this with our icons in Emulsify:
=== End of answer to your original question… Now for a little more info that might help:
P.S. You can create as many json files as you want here. Just be careful you don’t run into name-spacing issues. We accounted for this in the header.json
file by namespacing everything under the “header” array. That way the footer nav doesn’t pull our header menu items, or vise-versa.
example homepage home.twig
that pulls menu items for the header and the footer from data.json files
header.json
footer.json