Create a field of type radio with the class switcher:
For example:
<field
name="show_title"
type="radio"
label="JGLOBAL_SHOW_TITLE_LABEL"
class="switcher btn-group btn-group-yesno "
default="1"
>
<option value="0">JHIDE</option>
<option value="1">JSHOW</option>
</field>
The switcher should behave in Joomla 3 and Joomla 4 like expected. In Joomla 3 you need the classes btn-group btn-group-yesno and for Joomla 4 the class switcher is important.
In Joomla 4 you do not see the green color if active.
I think the problem is this line:
$type = str_replace('switcher switcher-', '', trim($class));
You can find this line here:
The type of the field is set with the class attribute.
I changed this line in
if (strpos($type, 'switcher') !== false){
$type = 'switcher';
}
and everything works fine. My changes make, that if the word switcher is in the values of the class attribute, the type is set to switcher. But I am sure that this is not that easy ....
Labels |
Added:
?
|
Category | ⇒ | Fields |
Status | New | ⇒ | Discussion |
Actually, the type attribute would be wrong. At least the way the field currently works (with two different JLayouts). Using the type attribute would mean we create a completely new field for that switcher.
We can do that of course, but then we have two fields which both are radios just with different appearance.
The current approach of switching the layout based on a CSS class is bad design.
The most correct way would be to set the layout in the XML to the switcher one. But that means adding that layout to each XML definition.
An alternative may be to add a new attribute like switcher="true"
which then sets the layout accordingly. But that as well means to add a line to each switcher radio.
@Bakual It should be a new field ideally. Reason being is the radio
type can have more than 2 options, whereas the switcher must have 2 only.
I agree that using a class to call the switcher is bad, however using the following is also bad:
<field
type="radio"
switcher="true"
/>
Much better to separate it and use:
<field
type="switcher"
class="primary" << // (success, danger, primary) (defaults to 'success')
/>
Happy to do the leg work of patching the xml when a decision is made
With current code, the correct way to set the layout would be
<field
type="radio"
layout="joomla/form/field/radio/switcher"
/>
That does work without any changes in the PHP side.
However it doesn't solve the issue here (an own type doesn't solve it as well). The problem we have is that in J3 core uses button groups and in J4 we use switchers. If an extension now wants to work both in J3 and J4 and use the same UI which core uses, it isn't possible currently.
There are two issues there: One is that the XML changed (for now class attribute and you can't have both variants active) and the second is that the order of the options got changed. I don't see how that can both be fixed and I'm not sure if it even should be fixed.
As it is, the only option for now is to keep using button groups. Those are supposed to work both in J3 and J4. An extension can switch to using switchers only when they drop support for J3.
Status | Discussion | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2018-03-20 19:50:55 |
Closed_By | ⇒ | joomla-cms-bot |
Closed_By | joomla-cms-bot | ⇒ | franz-wohlkoenig |
Set to "closed" on behalf of @franz-wohlkoenig by The JTracker Application at issues.joomla.org/joomla-cms/19943
@astridx there was some discussion about this in the other PRs. I think someone very brave needs to change the field to use the type attribute from the XML and also patch all the XMLs that have a switcher (that's the tricky part😜 )