Jump to content


Photo

[API] show field label

api field

  • Please log in to reply
5 replies to this topic

#1 buothz

buothz

    Jr. Member

  • Members
  • PipPip
  • 45 posts
  • 3

Posted 16 April 2012 - 07:26 AM

Hi, i've this field:
NAME LABEL TYPE NOTES TEMPLATES

bandiera Flag TextLanguage
1

I also did a search on the forum and the API section but could not find answer, is I use
echo $page->bandiera;
I will only output the value (italia for example), I would rather have as output
LABEL: VALUE (Flag: Italia for example)
thank u...

#2 ryan

ryan

    Hero Member

  • Administrators
  • 5,773 posts
  • 3118

  • LocationAtlanta, GA

Posted 16 April 2012 - 09:59 AM

Welcome to the forums Buothz! Good question. Here's how you could do it:

$language = $languages->get("spanish");
$field = $fields->get('bandiera');
$label = $field->get("label$language");  
echo "$label: $page->bandiera";

If you want a specific other language replace the first line above with this:

$language = $languages->get('italia');
.

Edit: Code fixed first example and changed to make more sense. (Soma)

#3 buothz

buothz

    Jr. Member

  • Members
  • PipPip
  • 45 posts
  • 3

Posted 16 April 2012 - 10:08 AM

Welcome to the forums Buothz! Good question. Here's how you could do it:

$field = $fields->get('bandiera');
$label = $field->get("label$language");  
echo "$label: $page->bandiera";

If you want a specific other language replace the line above with this:

$italia = $languages->get('italia');
$label = $field->get("label$italia");

thank u ;) another little question, to show "FieldsetOpen" and "FieldsetOpenTab" with API?

Edited by buothz, 16 April 2012 - 10:14 AM.


#4 ryan

ryan

    Hero Member

  • Administrators
  • 5,773 posts
  • 3118

  • LocationAtlanta, GA

Posted 17 April 2012 - 08:49 AM

another little question, to show "FieldsetOpen" and "FieldsetOpenTab" with API?


These two fieldtypes don't have any function outside of the PW PageEdit Process, so aren't really applicable in the API for the most part. Let me know if I've misunderstood your question? If you are building a form elsewhere, it's more likely that you would want to use the InputfieldFieldset:

$form = wire('modules')->get('InputfieldForm');
$form->attr('method', 'post'); 
$form->attr('action', './'); 

$fieldset = wire('modules')->get('InputfieldFieldset'); 
$fieldset->label = 'This is a fieldset';
$fieldset->description = 'Some optional extra copy if you want it.';

$field = wire('modules')->get('InputfieldText');
$field->attr('name', 'title'); 
$field->label = 'Title';

$field->description = 'Extra description';


$fieldset->add($field);
$form->add($fieldset);
echo $form->render();


#5 buothz

buothz

    Jr. Member

  • Members
  • PipPip
  • 45 posts
  • 3

Posted 17 April 2012 - 11:41 AM

These two fieldtypes don't have any function outside of the PW PageEdit Process, so aren't really applicable in the API for the most part. Let me know if I've misunderstood your question? If you are building a form elsewhere, it's more likely that you would want to use the InputfieldFieldset:

$form = wire('modules')->get('InputfieldForm');
$form->attr('method', 'post');
$form->attr('action', './');

$fieldset = wire('modules')->get('InputfieldFieldset');
$fieldset->label = 'This is a fieldset';
$fieldset->description = 'Some optional extra copy if you want it.';

$field = wire('modules')->get('InputfieldText');
$field->attr('name', 'title');
$field->label = 'Title';

$field->description = 'Extra description';


$fieldset->add($field);
$form->add($fieldset);
echo $form->render();

Thank you so much for your prompt reply, I just want to take the field and divide them into categories, so I decided to use the fieldset to preserve the multi-language.
For example: http://www.yachtbrok.../barca37016.htm
"specifiche" in English must be specific.
But if I use an editable field risk being edited by the user should not edit it instead ...

To have an ordered structure wanted to use the possibility of fieldset ...

Otherwise I can use simple PHP like IF or SWITCH

if ($_GET['language']=='it') {
 echo 'specifiche';
  } else {
  echo 'specifics';
  }

thanks again

Edited by buothz, 17 April 2012 - 12:08 PM.


#6 ryan

ryan

    Hero Member

  • Administrators
  • 5,773 posts
  • 3118

  • LocationAtlanta, GA

Posted 18 April 2012 - 09:06 AM

You may also want to look at using multi-language fields, or multi-language alternate fields for this (if you aren't already):
http://processwire.c...anguage-fields/

As for detecting a language from a URL, a GET var is a fine way to go. Though note that you'll want to keep your templates cache settings off, since GET vars aren't cachable. Other options include a URL segment (if turned on in your template) or a subdomain.

A URL segment might look like this (for english): /boats/barca37016/en

if($input->urlSegment1) {
    $language = $languages->get($sanitizer->pageName($input->urlSegment1)); 
    if($language) $user->language = $language; 
}

A subdomain might look like this (for english): en.yachbrokers.com

$name = strstr($config->httpHost, '.', true); // requires PHP 5.3
$language = $languages->get($sanitizer->pageName($name));
if($language) $user->language = $language;





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users