Piqsel Posted April 13, 2013 Posted April 13, 2013 Hi, Just discovered Processwire and instantely got a new favorite CMS. Finally something that doesn't feel so bloated like some others. Anyway, I started working on a little product showcase and have a question about field labels and how to output them. Is there a way to get a certain field label so that I (in the example below) can replace "Price:" with something like {$product->product_price->label}? Also, could I use number_format directly on {$product->product_price}, or do I have to make a variable like so: $nf_price = number_format($product->product_price, 0, ' ', ' '); <?php function productList(){ $products = wire("pages")->find("parent=/products/"); $out =" "; foreach($products as $product){ $out .="<article class='six columns'>"; $out .="<img src='{$product->images->first()->url}'>"; $out .="<a href='{$product->url}'><h1>{$product->title}</h1>"; $out .="<span>Price: </span><span>{$product->product_price}</span>"; $out .="<span>Stock: </span><span>{$product->product_stock}</span>"; $out .="</article>"; } echo $out; } ?> Enough with dumb questions for now, but be prepared for a lot more in the near future .Thanks in advance!
adrian Posted April 13, 2013 Posted April 13, 2013 Hi Piqsel and welcome! This should get you what you want: $out .="<span>".$product->fields->product_price->label.": </span><span>" . number_format($product->product_price, 0, ' ', ' ') . "</span>"; Hope that helps, and feel free to keep asking away 1 1
Piqsel Posted April 13, 2013 Author Posted April 13, 2013 Hi Piqsel and welcome! This should get you what you want: $out .="<span>".$product->fields->product_price->label.": </span><span>" . number_format($product->product_price, 0, ' ', ' ') . "</span>"; Hope that helps, and feel free to keep asking away Hi Adrian and thank you for your quick reply! I tried the above but it just output a colon, so I guess I'm must be doing something wrong somewhere. Cant figure out what though since images and everything else works as it should.
Macrura Posted April 13, 2013 Posted April 13, 2013 $product->product_price->label this should work - the cheatsheet says you can do $field->label ( $product->product_price is your field ); i think i have done number_format on something similar, so I would have to say yes to this: "could I use number_format directly on ($product->product_price)"... maybe report back if your results are not as expected
adrian Posted April 13, 2013 Posted April 13, 2013 Sorry that doesn't work. Does your original: $out .="<span>Price: </span><span>{$product->product_price}</span>"; print out the price correctly? Might be a silly question - but does your product_price field definitely have a Label defined? I checked the $product->fields->product_price->label and it works well for me. Just to be sure, can you try: $product->fields->product_price->name instead? number_format should work like that, but maybe just to test any issues with $product->product_price, try testing with something simply like "round" I see Soma lurking here too, so maybe he'll chime in 1
Soma Posted April 13, 2013 Posted April 13, 2013 Field label is not in the page field value, you have to get it through the field itself. echo $fields->get("body")->label; 3
Piqsel Posted April 13, 2013 Author Posted April 13, 2013 ... ... ... Wow, what a great community! I'm embarrassing to say that Adrian was spot on when he asked if the field had a label defined . The products has 25+ custom fields and I labeled all but, of course, the price field. $product->fields->product_price->label did work so thank you all for helping me! 1
adrian Posted April 13, 2013 Posted April 13, 2013 Soma, correct me if I am wrong (although I tested it and it works ), but isn't that the same as doing $product->fields->product_price->label ? Is one better than the other? No worries - you should do a search and see all the stupid mistakes I have made Any luck with the number_format issue?
Soma Posted April 13, 2013 Posted April 13, 2013 Aah, wasn't reading careful and didn't spot the ...->fields... well yes it's the same at the end but using $fields->product_price->label; is little shorter
Piqsel Posted April 13, 2013 Author Posted April 13, 2013 Soma, correct me if I am wrong (although I tested it and it works ), but isn't that the same as doing $product->fields->product_price->label ? Is one better than the other? No worries - you should do a search and see all the stupid mistakes I have made Any luck with the number_format issue? Yes, forgot to say that the number_format is working also .
rizsti Posted December 3, 2015 Posted December 3, 2015 I was wondering if anyone could give me some input on this topic in regards to ProFields Textareas. I tried the method above ( $page->fields->prod_texts_data->$key->label inside the foreach loop below) but it threw an error. I've found a solution to drawing out the label of the defined textareas, but I'm not sure if it's the appropriate way to do it, so I figured I would ask. <?php $fieldType = $modules->get('FieldtypeTextareas'); foreach ($page->prod_texts_data as $key => $value){ echo $fieldType->getLabel($fields->get('prod_texts_data'), $key); } ?> Or is there an easier way to do this?
kixe Posted December 6, 2015 Posted December 6, 2015 use the label() method: // $page->textareasField->label($name); foreach ($page->prod_texts_data as $key => $val) echo $page->prod_texts_data->label($key);
NorbertH Posted October 10, 2018 Posted October 10, 2018 How do i get the label if i have overridden the label in the template ? I always get the original one from the field.
Soma Posted October 10, 2018 Posted October 10, 2018 That should help https://gist.github.com/somatonic/8323844 1
NorbertH Posted October 10, 2018 Posted October 10, 2018 Is there a way to fetch it in the $page context ?
NorbertH Posted October 10, 2018 Posted October 10, 2018 $page->template->fieldgroup->getField('title', true)->label finally does it . Thanks Soma! But to be true i really expected that : $page->title->label should return the label that is set in the context of the page template not the basic lable set in the field . Wouldnt' it be more intuitive to return the the overridden label than the original one from the field? If i "override" a label in a template i expect it to be the one to be sent to my page as the label for the title. 1
Soma Posted October 10, 2018 Posted October 10, 2018 $page->fieldgroup->getField() does work for me. And no, $page->title is a property that does return a string and is not the field object. Since the field is in context of the template/fieldgroup it seems natural you get the field settings through it. 1
alemachado Posted August 29, 2019 Posted August 29, 2019 Hi @Soma $fields->get("body")->label; Seems to not consider the multilanguage label. Is that why it is out of $page context?
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now