Jump to content
Piqsel

Echo field label?

Recommended Posts

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!

Share this post


Link to post
Share on other sites

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 :)

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

$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

Share this post


Link to post
Share on other sites

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 :)

  • Like 1

Share this post


Link to post
Share on other sites

Field label is not in the page field value, you have to get it through the field itself.

echo $fields->get("body")->label;
  • Like 2

Share this post


Link to post
Share on other sites

...

...

...

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!

  • Like 1

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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 

Share this post


Link to post
Share on other sites

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 :).

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

use the label() method:

// $page->textareasField->label($name);
foreach ($page->prod_texts_data as $key => $val) echo $page->prod_texts_data->label($key);

Share this post


Link to post
Share on other sites

How do i get the label if i have overridden the label in the template ?

I always get the original one from the  field.

Share this post


Link to post
Share on other sites
$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.     

Share this post


Link to post
Share on other sites

$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.

  • Like 1

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...