1

I'm building a multilingual site, which has a fallback to English if the article isn't available in the users selected language. I'm using freebie_1 to switch the custom field for each language.

I'm still fumbling my way round Stash and guessing the following code should work. However, on the front end I'm only seeing {cf_news_title_en}

How can I pass the {lang} variable in to the custom field to render correctly?

{exp:channel:entries channel="news" dynamic="off"}

    {if "{cf_news_title_{freebie_1}}" != ""}
        {exp:stash:set_value name="lang" value="{freebie_1}"} 
    {/if}

    {if "{cf_news_title_{freebie_1}}" == ""}
        {exp:stash:set_value name="lang" value="en"} 
    {/if}

    {exp:stash:append_list name="news"}

        {stash:news_title}{cf_news_title_{lang}}{/stash:news_title}
        {stash:news_page_url}{site_url}{freebie_1}/news/{url_title}{/stash:news_page_url}               
        {stash:news_intro}{cf_news_intro_{lang}}{/stash:news_intro}

    {/exp:stash:append_list}

{/exp:channel:entries}


{exp:stash:get_list name="news" type="snippet"}
    <article>
        <header>
             <h2><a href="{news_page_url}">{news_title}</a></h2>
        </header>
        {if news_intro != ""}<p class="lead">{news_intro}</p>{/if}

    </article>
{/exp:stash:get_list}
7
  • Is this minus sign on your real code too? Take a look, just before the "=": {if "{cf_news_title_{freebie_1}}" -= ""}
    – Sobral
    Commented Jun 6, 2013 at 12:07
  • Oops! Edited. No, that code isn't in my real template :-/
    – Matt Ellis
    Commented Jun 6, 2013 at 12:26
  • Do you really need to use freebie for this segment? I don't know when freebie enters on parsing order. if the first segment isn't on freebie segments, try to use the default '{segment_1}'.
    – Sobral
    Commented Jun 6, 2013 at 13:40
  • Yes, I need Freebie to switch the languages. It's set to ignore segment_1, so the URL is domain.com/en/news - 'en' being the language prefix.
    – Matt Ellis
    Commented Jun 6, 2013 at 21:58
  • I have found a very unDRY approach to this situation that does work, but it repeats a lot of code. Was hoping this would work as it's much leaner / manageable.
    – Matt Ellis
    Commented Jun 6, 2013 at 22:00

1 Answer 1

1

To make "{cf_news_title_{freebie_1}}" to work, freebie has to be parsed early.

So, let's create an alternative to this.

If all of the first segments of URLs is code of languages, on your config. php, add this:

global $assign_to_config;

if(!isset($assign_to_config['global_vars']))$assign_to_config['global_vars'] = array();

$lang_code = substr($_SERVER['REQUEST_URI'], 1, 2);
$assign_to_config['xml_lang']               = $lang_code;
$assign_to_config['language']               = $lang_code;
$assign_to_config['global_vars']['cv-lang']     = $lang_code;

This way, you can use {cv-lang} instead of {freebie_1}.

By the way, I think you can do all of this without stash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.