Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Media collections

Started by Muut Archive 11 years ago · 1 replies · 302 views
11 years ago

For those interested how to do media collections, I just implemented a scheme were we can add taxonomy to images (and other media files) in a corresponding image.meta.yaml file like this:

someimage.jpg.meta.yaml:

YAML
alt: Information
taxonomy:
   tag: [sidebar]
   category: [landscape]
basename: info-box

and then using a wildcard Twig filer which allows using the taxonomy class as wildcard

TXT
    new \Twig_SimpleFilter('has_*', [$this, 'has_filter']),
PHP
    public function has_filter($class, $mediaCollection, $key)
    {
        $result = []; 
        foreach ($mediaCollection as $item) {
            if (in_array($key, $item->taxonomy[$class])) {
                $result[] = $item;
            }
        }
        return $result;
    }

and a template to render:

TWIG
    {% set sidebar = page.media.images|has_tag('sidebar')|first  %}

    {% if sidebar %}
        ...
        <img width="300" src="{{ sidebar.resize(300, 0).url }}" alt="{{ sidebar.alt }}">
        ...
    {% endif %}
11 years ago

Very cool! I love seeing stuff that I have never even thought of!

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1341 9 years ago
Archive · by Muut Archive, 9 years ago
2 928 9 years ago
Archive · by Muut Archive, 9 years ago
2 4057 9 years ago
Archive · by Muut Archive, 9 years ago
1 2942 9 years ago
Archive · by Muut Archive, 9 years ago
3 1115 9 years ago