Jump to content

Custom formatting of non-editable fields


Jonathan Lahijani
 Share

Recommended Posts

I have a decimal field that has been set to non-editable.  I want to format the value so it's nicer when editing a page.  For example, instead of showing 123456, I want to format it to show $123,456.00

What is the proper way to hook into it and modify the rendered value?

Link to comment
Share on other sites

FieldtypeDecimal uses InputfieldFloat as its inputfield, and it's the renderValue() method that determines what is shown when a field is not editable.

So you could do this:

$wire->addHookAfter('InputfieldFloat::renderValue', function(HookEvent $event) {
	/** @var InputfieldFloat $inputfield */
	$inputfield = $event->object;
	$field = $inputfield->hasField;
	if($field && $field->name === 'your_field_name') {
		$event->return = '$' . number_format((float) $inputfield->value, 2);
	}
});

 

  • Like 4
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...