We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6848
    • 52 Posts
    Hi,
    I need to execute a snippet only for specific page language and cannot figure out how to do it
    Or may be this is not possible.

    There is a snippet like this:

    [[ConditionalRedirect? &variable=`<tvar>` &condition=`<value>`]]

    the snippet functionality is something like:

    if (ValueOfTV == ValueOfCondition) {header( ’Location: ’ . $url , TRUE, 303);

    So far so good. This construct is in general working.

    But if I use it like this:

    - I have multilingual variable ( var_bg and var_en)
    - var_bg=1 var_en=0
    - I call the snippet like this [[ConditionalRedirect? &variable=`var_(yams_id)` &condition=`0`]]
    - The snippet can be in a template, or in in the content (for each language)

    The expectation is to have the redirect if the page language is "en"
    The reality is that the redirect happens always.

    I’m not an expert in yams or in modx, so I don’t know the internals of parsing and executing the snippets,
    but from the behavior my conclusion is that the page is rendered (processed) in all available languages independent of the current.

    Is it possible to avoid this behavior and to execute the snippet only for current language?



      • 22851
      • 805 Posts
      I’ll have to check to be sure, but I think that uncacheable snippet calls are only executed for the served language, whereas cacheable snippet calls are executed and cached for all languages. So, please try making your snippet call uncacheable (which you need anyway) and see if that makes a difference.
        YAMS: Yet Another Multilingual Solution for MODx
        YAMS Forums | Latest: YAMS 1.1.9 | YAMS Documentation
        Please consider donating if you appreciate the time and effort spent developing and supporting YAMS.
        • 6848
        • 52 Posts
        No, not working in any combination. Always redirects

        To be more specific:

        This is the call:
        [!ConditionalRedirect? &mode=`yams` &tv=`available` &lang=`(yams_id)` &cond=`0`!]
        


        This is the real snippet:

        <?php
        $docid = (isset($docid))? $docid: $modx->documentIdentifier;
        $mode = (isset($mode))? $mode :"";
        $tv = (isset($tv))? $tv :"";
        $lang = (isset($lang))? $lang :"";
        $cond = (isset($cond))? $cond :"";
        $parent = $modx->getParent($docid,1,'id');
        $tv = $tv.'_'.$lang;
        $tvval='';
        
        if ($tv!="_") {
          $tvarray = $modx -> getTemplateVarOutput( $tv, $docid, $published = 1 );
          $tvval = $tvarray[$tv];
        }
        
        if ($parent['id'] != false) {
            $redirectId= $parent['id'];
            $redirectUrl= $modx->makeUrl($redirectId);
        } else {
            $redirectUrl= $modx->makeUrl($modx->config['site_start']);
        }
        if ($tvval==$cond){
        	if ($mode=="yams"){
        	    header( 'Location: ' . substr($redirectUrl,1) , TRUE, 303);
        	    return;
        	}
        	else {
        	  return $modx->sendRedirect($firstChildUrl);
        	}
        }
        else {
        	//Debug
        	return 'Parent: '.$parent['id'].' Redirect: '. $redirectUrl;
        }
        ?>


        I tried with setting the page to non cacheable and putting the snippet in the template and in the specific language content.
        Always the same behavior. Or may be I’m missing something?