Jump to content

How to utilise "Field-Template Context"?


homma
 Share

Recommended Posts

Hello, I'm new to processwire and trying to build a multilingual (Japanese-English) website.

Installation was done and multilingual feature is nicely working. Thank you for this great CMS!

My question is about the use of "Field-Template Context" discussed in this topic.

Is there a way to display context-field-labels set by template on front-end?

For example, if there's a field like;

field name = price

default label = price /default label (ja) = price-ja

label in templateA = shipping / label in templateA (ja) = shipping-ja

then how I can display the label "shipping" and "shipping-ja"on the pages that use templateA?

Link to comment
Share on other sites

A field is displayed in a frontend template in two ways:

<?php echo $page->field_name; ?>

Or (if supported by your server):

<?=$page->field_name?>

You should take a look at the following tutorial, which covers all of the first steps http://processwire.c...ct-walkthrough/

http://processwire.com/api/multi-language-support/multi-language-fields/

if($input->get->language) {
   // user clicked on a link to set the language
   $name = $sanitizer->pageName($input->get->language);
   $language = $languages->get($name);
   if($language) $session->language = $language->name;
}
if($session->language) {
   // language is defined in user's session
   $user->language = $languages->get($session->language);
}
Link to comment
Share on other sites

Hello homma

This example code should get you there.


$fieldID = $fields->get("body")->id;
$bodyfield = $page->template->fieldgroup->getFieldContextArray($fieldID);

$currUserLangID = $user->language->name != 'default' ? $user->language : '';
echo "current body label:" . $bodyfield["label$currUserLangID"] . "<br/>";

echo "default body label:" . $bodyfield["label"]. "<br/>";

$langDE = $languages->get("de");
echo "de body label:" . $bodyfield["label$langDE"]. "<br/>";
Link to comment
Share on other sites

Hello Exceptionz Project and Soma,

Thank you very much for your help! I modified the example slightly and tested the code below.

In the front-end page, $label_FIELDNAME should display the context-field-label (if exists).

The code is working fine, I think :)

$currUserLangID = $user->language->name != 'default' ? $user->language : '';
$f_items = $page->template->fields;

foreach($f_items as $f_item){
$f_itemname = $f_item->name;
$f_itemdefault = $fields->get("$f_item");
$fieldID = $fields->get("$f_itemname")->id;
$contextfield = $page->template->fieldgroup->getFieldContextArray("$fieldID");
$f_itemcontent = $contextfield["label$currUserLangID"];

if($f_itemcontent){
${'label_' . $f_itemname} = $contextfield["label$currUserLangID"];
}else{
${'label_' . $f_itemname} = $f_itemdefault->get("label$currUserLangID");
}

}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...