Skip to content
Grav 2.0 is officially stable. Read the announcement →
Themes & Styling

Access media made from a blueprint of type:file

Solved by Ace A. View solution

Started by Ace A. 4 years ago · 1 replies · 409 views
4 years ago

I have defined the following in my blueprints.yaml

YAML
        media: # load user assets for the theme
          type: tab
          title: Media
          fields:
            custom_favicon:
              type: section
              title: Custom Favicon
              fields:
                media.favicon:
                  type: file
                  label: Upload your custom favicon
                  destination: 'theme://assets/user_favicon/'
                  multiple: false
                  avoid_overwriting: yes
                  limit: 1
                  filesize: 1
                  accept:
                    - image/*

It works. I was able to upload a file that generated the following yaml markup

YAML
media:
  favicon:
    user/themes/canvas/assets/user_favicon/android-chrome-512x512.png:
      name: android-chrome-512x512.png
      full_path: android-chrome-512x512.png
      type: image/png
      size: 11710
      path: user/themes/canvas/assets/user_favicon/android-chrome-512x512.png

all the user-made attributes can be accessed if I dumped theme_config.media.favicon. But I cannot access the path of the uploaded image with an url() filter or a dot notation i.e. {{ theme_config.media.favicon.path }}. I am lost as to which I got wrong here. Is my blueprints malformed or am I using the wrong dot notation?

Please help. thanks.

4 years ago Solution

Solved. this solution is based on Quark's logo partials.

TWIG
{# extract the media array #}
{% set favicon = theme_config.media.favicon %}

{# assign the uploaded file name to a variable #}
{% set favicon_file = (favicon|first).name %}

{# append the filename to the intended source url #}
{{ url('theme://assets/user_favicon/' ~ favicon_file) }}

last edited 12/10/22 by Ace A.

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by stuart young, 2 weeks ago
3 251 2 weeks ago
Themes & Styling · by Sebadamus, 3 weeks ago
5 317 3 weeks ago
Themes & Styling · by martynfoster735, 4 weeks ago
1 310 4 weeks ago
Themes & Styling · by Justin Young, 1 month ago
1 348 1 month ago
Themes & Styling · by Slebeig, 2 months ago
4 474 2 months ago