Jump to content

Get overriden field label


tpr
 Share

Recommended Posts

I try to re-use fields and would like to display the field label on the frontend, but all I got is the default label, not the overriden one.

It's a multilanguage site but I don't think it matters.

This is what I've tried:

echo $page->fields->body->label;
echo $templates->get('package')->fields->body->label;

Is there a way to get the overriden label text?

Link to comment
Share on other sites

You need to get in in context

$p = $pages->get(1040);
$label = $p->fields->get('body', true);
echo $label;

// or if you are sure the page is there, a one-liner
echo $pages->get(1040)->fields->get('body', true);

 
Edit: I could have sworn @LostKobrakai was asleep :-)  :P .  Beaten, again :-)

Edited by kongondo
  • Like 2
Link to comment
Share on other sites

Thanks guys, could've asked earlier to save some time :)

Finally I settled with this:

$templates->get('package')->fields->getFieldContext('body')->$labelLang

Where $labelLang is 'label' + language ID (if it's not the default language, otherwise simply 'label').

Link to comment
Share on other sites

  • 3 years later...

Had this same issue today, ie get the customised label for a page/template field and tried all the above. They all returned the original field label and couldn't find any reference to the 'true' option when getting 'fields' or 'field' in the API Docs. Maybe PW has moved on or more likely, I missed something. Anyway, what did work for me was:

$page->getField("body")->label;

https://processwire.com/api/ref/page/get-field/

 

  • Like 3
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...