Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In our example the user is in France, so if we echo the result we get ‘lang=”fr-FR”’.

Code Block
languagephp
<?php
  echo $currentLang;
  // Output: lang="fr-FR"
?>

In our example, the dir1 schema field holds the language information, which for French is 'fr'. Therefore,
we just need to get the ‘fr' portion of the language attribute string and store it to a variable (alternatively you could use a switch statement to set the variable if you don’t have many languages!). To do this we will use some regex:

Code Block
languagephp
<?php 
	$currentLang = get_language_attributes(); // Get language attribute	
	$regex = '/"(.*?)-/s'; // Regex pattern to extract 'fr'
	preg_match($regex, $currentLang, $output); // Store 'fr' in $output
	echo $output[1];
?>

...

Without the variable in place the default filter looks like this:

Code Block
languagejson
"variables": {
  "filter": "dir1='fr'"
},

Adding the variable in we get the following:

Code Block
languagejson
"variables": {
	"filter": "dir1=\'' . $output[1] . '\'"
},

...

To do this we simply echo the html onto the page:

Code Block
languagephp
<?php echo '<div data-widget="overlay">
	  <script type="application/json">
		{
		  "account": "your-account",
		  "collection": "your-collection",
		  "endpoint": "//jsonapi-us-valkyrie.sajari.net",
		  "pipeline": "website",
		  "preset": "website",
		  "variables": {
				"filter": "dir1=\'' . $output[1] . '\'"
          },
		  "filters": [],
		  "options": {
			},
			"results": {
			  "viewType": "list",
			  "showVariantImage": false,
			  "imageAspectRatio": {
				"grid": 0.563,
				"list": 1
			  },
			  "imageObjectFit": {
				"grid": "cover",
				"list": "cover"
			  }
			},
			"showViewType": true,
			"input": {
			  "hide": false,
			  "position": "aside"
			},
			"mode": "overlay",
			"buttonSelector": ".btn-search"
		  },
		  "theme": {
			"color": {
			  "primary": {
				"base": "#A335FF",
				"text": "#ffffff",
				"active": "#A335FF"
			  }
			}
		  },
		  "customClassNames": {
			"results": {
			  "template": {
				"container": "list"
			  }
			}
		  }
		}
	  </script>
	  <noscript>This page requires JavaScript</noscript>
	</div>
	<script async src="https://cdn.sajari.com/embed/1/loader.js"></script>';
?>

...

If we look at our source code we can see that the correct filter is in place:

Code Block
languagejson
	<div data-widget="overlay">
	  <script type="application/json">
		{
		  "account": "your-account",
		  "collection": "your-collection",
		  "endpoint": "//jsonapi-us-valkyrie.sajari.net",
		  "pipeline": "website",
		  "preset": "website",
		  "variables": {
			"filter": "dir1='fr'"
},
	

...