backdrop-issues
backdrop-issues copied to clipboard
Enhanced Float-Value Display Options
Description of the need
An enhanced level of flexibility in how float-values are displayed is needed.
This is a [modified] snip from the admin page of my site at "/admin/structure/types/manage/sell-line-item/display/default":
My current use case
I have a quantity field which can refer to units of (each), (lbs), (packets of X), etc. I need to be able to specify a value such as 215.326 in the case of pounds (for instance) or 5,000 in the case of 5k units (or items). So it makes sense to display the full "scale" of the float in the case of lbs, whereas I'd rather the value for quantity of items read as "5,000" instead or "5,000.000."
This feature would furthermore provide for the value "0.3750" to be displayed as "0.375" or ".375", either of which might be preferable over "0.3750".
I have been unable to find a module which provides this functionality, though it is possible that views offers this option. In any case, it seems appropriate that it would be offered in core, since it is so closely tied with the basic Display Mode functions.
Proposed solution
Looking at the BD documentation, it appears to me that this might be accomplished by adding two booleans to the data structure, and some code to the functions:
function number_field_formatter_info()
function number_field_formatter_settings_form()
...and others, found in the number.module.
@ericfoy - Welcome to the core issue queue!
Thank you for creating this issue @ericfoy (and welcome from me too).
Is the screenshot you have provided in the issue summary a mockup, or actual functionality on your site? If existing functionality, then do you know how it's implemented? (custom module perhaps?).
No; sorry, I "custom-coded" that with MS-Paint. It is only a mockup. It just seems the natural place for those options to appear in the UI. There may be a more appropriate placement of those formatting options, but this is what I hoped would be there when I went there...
Mockups are perfectly fine @ericfoy ...I was asking because I was looking for existing contrib modules that we could possibly port over to Backdrop, but I wasn't able to find any, or the ones that I found were either D8/9-specific, or only doing trailing zeros and not leading.
It does sound like an option that could benefit people if provided by core.
...related: https://drupal.stackexchange.com/questions/128170/views-how-to-remove-ending-zeros-from-decimal-field/128172
Yes, that link is strikingly similar to my request (if not identical).
I'm thinking that the implementation could be handled like so... In the number.module, the following [partially shown] function would have the asterisked lines added
function number_field_formatter_info() {
return array(
// The 'Default' formatter is different for integer fields on the one hand,
// and for decimal and float fields on the other hand, in order to be able
// to use different default values for the settings.
'number_integer' => array(
...
),
),
'number_decimal' => array(
'label' => t('Default'),
'field types' => array('number_decimal', 'number_float'),
'settings' => array(
'thousand_separator' => '',
'decimal_separator' => '.',
'scale' => 2,
** 'trim_leading' => FALSE,
** 'trim_trailing' => FALSE,
'prefix_suffix' => TRUE,
),
),
'number_unformatted' => array(
'label' => t('Unformatted'),
'field types' => array('number_integer', 'number_decimal', 'number_float'),
),
);
}
How those attributes would be handled by all the rest of the code is obviously to be determined. For instance, I am not conversant enough to be able to determine (as of yet) whether these settings should apply to 'settings' or 'instance_settings' (as, e.g., in the number_field_info() function), etc. I am a total newb to this code, and I suspect there's probably someone out there who could pretty much slam-dunk this modification.
Okay, I couldn't resist... I thought of one more common option: the ability to have the system either "round" or "truncate" when trimming it to the designated scale.
Example. Say the actual value is 3.14159, and your scale is 3. The display would then be
"Round": 3.142 "Truncate": 3.141
Cool, huh?
Okay guys and gals: I have added my desired functionality. And I gotta say, it works pretty cool! But here's the problem: I have accomplished my goal by hacking the core module. The changes (so far) are pretty minimal, so I don't know if this should be considered a viable contribution to core, or whether it should be presented in the form of a contrib module. The latter seems a lot more difficult since a) it's already done the way it is, and b) I have yet to put together a functional module, what with all the hooks and crannies required...
Anyway, how should I proceed?
Anyway, how should I proceed?
Personally, to me this looks like a cool candidate for a contrib module. Not sure how you accomplished things, but the available core alter hooks should make this possible out of the box.
Okay! I'm pretty stoked and excited about this... I have successfully finagled my desired functionality into a contrib module located at https://backdropcms.org/project/endo
Here's a screenshot:
So far, it seems to just work!
I will be addressing some needed tweaks:
- The "Use parentheses for negative value" and the Zero display override don't come up for integer fields (which they really should), so I'll be looking into that.
- I'm kinda brute-forcing some of the manipulation in there, and there's probably a more elegant way to accomplish the various goals, code-wise.
- Some validation is in order, such as checking that the Min. decimal places is not greater than the Max. decimal places. So that should be implemented...
etc.
No. 1 above has been fixed as of ver. 1.0.1 !
Follow the ongoing story here: https://github.com/backdrop-contrib/endo