Media collections

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:

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

    new \Twig_SimpleFilter('has_*', [$this, 'has_filter']),
    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:

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

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

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