[4.0] Custom fields prefix and suffix#24232
Conversation
J4 version of joomla#23499 It is very common to need to display a unit of measurement/value with a field. Currently there is no way to do this without creating custom layouts for every field and every unit when needed. This PR adds two new param to every custom field "Suffix" and "Prefix" which allows you to add a unit of measurement (or any other text) to be rendered after/before the value of the field. The new field is disabled by default so three is no b/c issue ### To test Apply the pr and then create a new custom field and you will see the new Prefix Suffix param in the render options.
|
Nice :) Can you make it so that there are no spaces between the field value and the prefix/suffix? Also, as mentioned in the other issue, is having the "Show suffix" and "Show prefix" yes/no toggles necessary? Wouldn't be simpler to just have two text fields that would display only if not empty? |
There aren't any |
I guess the spaces come from either the tab or linebreak in the PHP code (depending if suffix or prefix). There is whitespace between the
I'd second this. Just the remove the yes/no toggle and show the prefix/suffix if something is entered. |
# Conflicts: # components/com_fields/layouts/field/render.php
|
Updated to remove conflicts and simplify as per @Bakual suggestion |
| <?php if ($showLabel == 1) : ?> | ||
| <span class="field-label <?php echo $labelClass; ?>"><?php echo htmlentities($label, ENT_QUOTES | ENT_IGNORE, 'UTF-8'); ?>: </span> | ||
| <?php endif; ?> | ||
| <?php if ($prefix) : ?> |
There was a problem hiding this comment.
Just from a code review perspective. I think this needs to be <?php if (!empty($prefix)) : ?> otherwise you're going to get the span with an empty string. But if you test and tell me this works then I'm happy with that too
There was a problem hiding this comment.
would work without empty but would raise a warning if $prefix is not defined.
There was a problem hiding this comment.
But would not work if $prefix is "0" (zero)
There was a problem hiding this comment.
The field params is a registry object, so it will default to an empty string if existing fields haven't been saved yet
There was a problem hiding this comment.
Tested and you do not get a span with empty string
Also tested what happens if you have a prefix and then delete the prefix - no span then either
There was a problem hiding this comment.
There is a point in what Harald wrote. It doesn't work if you have a prefix (eg "0") which evaluates to false. I'm not sure if you ever get such a prefix or suffix. If you want to avoid that, one could do a check if ($prefix !== '')
There was a problem hiding this comment.
if prefix is 0 then you dont get a prefix (including the spans) not sure that would ever happen
There was a problem hiding this comment.
That's what I mean. I don't think "0" is a valid prefix or suffix. But I learned people do expect crazy things and maybe someone expects "0" to appear.
Personally, I don't mind 😄
|
I see another problem with this approach: $prefix = Text::plural($field->params->get('prefix'), $value);
$suffix = Text::plural($field->params->get('suffix'), $value);if the prefix language string for plural needs a suffix like 1st 2nd... this maybe and with a space between prefix and suffix. |
|
Sorry I dont understand your plural comment |
|
I'm not sure if a "decoration" attribute with a place holder wouldn't be better something like: DECORATION_1="The %dst floor" $decoration = Text::plural($field->params->get('decoration'), $value); And replaces the echo $value completely |
|
How is that not covered by passing this through the |
|
1st, 2nd, 3rd is impossible to do with Text::plural. Because it's not plural forms were talking here. Plurals forms are defined per language and eg in english you only have two forms. It's either plural or singular. So afaik you can't do a "3rd". |
what a mess... |
|
Thanks |
|
I think this is good enough for now. As Harald has pointed out it doesn't cover every case. But we can work on that if there's demand. I think it's useful enough as it is now. Thanks! |
J4 version of joomla#23499 It is very common to need to display a unit of measurement/value with a field. Currently there is no way to do this without creating custom layouts for every field and every unit when needed. This PR adds two new param to every custom field "Suffix" and "Prefix" which allows you to add a unit of measurement (or any other text) to be rendered after/before the value of the field.

J4 version of #23499
It is very common to need to display a unit of measurement/value with a field. Currently there is no way to do this without creating custom layouts for every field and every unit when needed.
This PR adds two new param to every custom field "Suffix" and "Prefix" which allows you to add a unit of measurement (or any other text) to be rendered after/before the value of the field.
The new field is disabled by default so there is no b/c issue