Jump to content

Echo field label?


Piqsel
 Share

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!

Link to comment
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
Link to comment
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.

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

Link to comment
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
Link to comment
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
Link to comment
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?

Link to comment
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 :).

Link to comment
Share on other sites

  • 2 years later...

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?

Link to comment
Share on other sites

  • 2 years later...
$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.     

  • Like 1
Link to comment
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
Link to comment
Share on other sites

  • 10 months later...

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