vhs icon indicating copy to clipboard operation
vhs copied to clipboard

v:page.resources.fal optional fallback for languages

Open MrMooky opened this issue 7 years ago • 7 comments

I've seen a few issues here regarding this, but non specified the following use-case: We're currently having 3 languages, with English as default, German secondary etc...

For most pages we'd want to use the default media image of the current page for all languages, but only if there is no image specified for the translation. We tried a few things but nothing worked as expected.

That's our current try:

<f:if condition="{page.media} >= 1">
    <f:then>
    <v:page.resources.fal table="pages" field="media" uid="{page.uid}" as="resources" slide="-1">
        <f:for each="{resources}" as="resource">
            <f:image src="{resource.id}" treatIdAsReference="1" width="1400" height="350c" class="heroimg" />
        </f:for>
    </v:page.resources.fal>
    </f:then>
    <f:else>
    <v:page.resources.fal table="pages_language_overlay" field="media" uid="{page.uid}" as="resources" slide="-1">
        <f:for each="{resources}" as="resource">
            <f:image src="{resource.id}" treatIdAsReference="1" width="1400" height="350c" class="heroimg" />
        </f:for>
    </v:page.resources.fal>
    </f:else>
</f:if>

But that does not output anything on the translated page. How could we accomplish this?

MrMooky avatar Mar 26 '18 11:03 MrMooky

Is this actually a bug? The following should work for all translations, when a header image is defined in the default language.

<v:page.resources.fal table="pages" field="media" uid="{page.uid}" as="resources" slide="-1">
    <f:for each="{resources}" as="resource">
        <f:image src="{resource.id}" treatIdAsReference="1" width="1400" height="350c" class="heroimg" />
    </f:for>
</v:page.resources.fal>

MrMooky avatar Apr 06 '18 12:04 MrMooky

I'm having the exact same issue. Did you find a solution for this? I know I can add the image manually in the translated page settings, but it makes no sense to not be able to just show the original image.

Scopestyle avatar Apr 19 '18 10:04 Scopestyle

Unfortunately not. We had to use an extra column for the header image as a workaround. No chance of getting the media field "translated".

MrMooky avatar Apr 19 '18 10:04 MrMooky

I solved this by adding a custom view helper before my <v:page.resources.fal>, which sets the global language to the default one if there are no images found:

$GLOBALS['TSFE']->sys_language_uid = 0;

After my loop I immediately set this back to the original language_uid.

Ugly, but works.

tkrebs avatar Apr 28 '18 15:04 tkrebs

@tkrebs Could you share your solution, please? We are having this issue with other projects as well. I'd rather have an ugly workaround than to create a new column in the backend for every project.

MrMooky avatar May 16 '18 08:05 MrMooky

As said, I am setting the global language uid to 0 before I call {v:page.resources.fal}. I used a custom PHP code parser view helper for this, though a more specialized helper would definitely be more secure.

{c:php(code: '$GLOBALS[\'TSFE\']->sys_language_uid = 0;')}
{v:page.resources.fal(table: 'pages', field: 'media', as: 'images')}
[...]
<f:for each="{images}" as="image" iteration="imageIteration">
[...]
</f:for>
[...]
{c:php(code: '$GLOBALS[\'TSFE\']->sys_language_uid = {languageCode};')}

The {languageCode} variable is injected via TypoScript.

tkrebs avatar May 17 '18 23:05 tkrebs

Uff, that's really nasty. :D Thanks for that, though.

MrMooky avatar May 18 '18 11:05 MrMooky

This is no longer a problem - v:page.resources.fal will consult the page overlay to look for a translated version of the associated FAL records. If no such translated records are found, it will default to using the untranslated version of associated FAL records; including when using "sliding" to fetch from parent page(s).

Assuming I read the report correctly, that is the exact desired behavior?

NamelessCoder avatar Jul 25 '23 13:07 NamelessCoder