getgrav / grav

Modern, Crazy Fast, Ridiculously Easy and Amazingly Powerful Flat-File CMS powered by PHP, Markdown, Twig, and Symfony
https://getgrav.org
MIT License
14.58k stars 1.41k forks source link

Support nested YAML in media metadata #2966

Open phmg701 opened 4 years ago

phmg701 commented 4 years ago

Hello, really nice work with Grav! I chose it on top of many other CMSs because:

I am trying to build a template/gallery module starting from the Cookbook recipe but when I add, in imageX.meta.yaml the following:

title:
  en: Title
  it: Titolo
description:
  en: Description
  it: Descrizione

Twig engine crashes as, for what I understand from debug bar and Clockwork, multilanguage meta is stored inside an array and Twig/Grav expects a string.

Array(1) object: Grav\Common\Page\Medium\ImageMedium
    object: Grav\Common\Page\Medium\ImageMedium
        *gettersVariable: "items"
        *items: Array(17)
            [...]
            title: Array(2)
                en: "Title"
                it: "Titolo"
            description: Array(2)
                en: "Description"
                it: "Descrizione"

Is there a workaround for supporting multi language metadata for images? It would be a great addition to the already excellent language support in Grav!

Thank you

rhukster commented 4 years ago

metadata is expected in a flat array, not a nested data currently.

phmg701 commented 4 years ago

Further investigation lead to this conclusion, so I managed to manually recreate the <img> tag without errors: <img src="{{image.resize(640, 640).url}}"></img> For multilanguage metadata I played around with bi-dimensional arrays this way (with checks and fallbacks):

{% if config.theme.imagemeta.enabled %}
    {% for mf in config.theme.imagemeta.fields %}
        {% if mf.enabled and (not image.meta[mf.meta_field] is null or not image.meta[mf.meta_field][lang] is null) %}
        <p class="mg-metadata mg-meta-{{mf.meta_field}}">{% if not image.meta[mf.meta_field][lang] is null %}{{image.meta[mf.meta_field][lang]}}{% else %}{{image.meta[mf.meta_field]}}{% endif %}</p>
        {% endif %}
    {% endfor %}
{% endif %}

The next big step is to centralize Images in a folder and work/filter them as a database, even by tag in .meta file. I still have to dig deep into Grav's Flex Objects, it might be an option.

Thanks