joomla / joomla-cms

Home of the Joomla! Content Management System
https://www.joomla.org
GNU General Public License v2.0
4.73k stars 3.64k forks source link

Custom fields: Subform fieldname ==id #30638

Open angieradtke opened 4 years ago

angieradtke commented 4 years ago

Just playing around with the subfom custom field and the template overrides, I noticed that the $field->name is not its name but its id. Is that the expected behavior or a bug?

{ ["myfield"]=> object(stdClass)#1211 (34) { ["id"]=> int(6) ["title"]=> string(4) "myfield" ["name"]=> string(6) "field6"

Bye Angie

SharkyKZ commented 4 years ago

Looks intentional:

https://github.com/joomla/joomla-cms/blob/4574ff9bcece5743b19c52dc08c923e83850f1ee/plugins/fields/subfields/subfields.php#L423-L430

Use $field->fieldname to get what you need.

jiweigert commented 4 years ago

Ok, it's implemented like this, so yes intentional by the look of it. But from developer view, it is misleading and unexpected to access a value through $field->name and get the id as result back, instead of the requested name (as the $field->name suggests).


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/30638.

AndySDH commented 4 years ago

Yeah it's implemented like this because it needs to be stored in the database based on the ID.

The name of a field can be changed. The ID cannot. If it was stored based on name, all data would be lost if the name changed.

jiweigert commented 4 years ago

The question here is not to save something or change the the unique key from fieldid to fieldname.

The question (I think) is, that the naming of the getter (function) $field->name is incorrect and irritating, when you get the field-id as result and not the field-name you expect by using that getter (function).

angieradtke commented 4 years ago

Yeah it's implemented like this because it needs to be stored in the database based on the ID.

uiuiui that sounds as it is hastily cobbled together ?

crystalenka commented 3 years ago

I came across this today and agree it doesn't make sense as a template developer. It's also a departure for how subforms work in Joomla 3, making it harder to port template overrides when everyone updates.

The name of a field can be changed. The ID cannot. If it was stored based on name, all data would be lost if the name changed.

No one is saying it should not be stored by ID, but not allowing us to access the data by name is unintuitive and frustrating, and makes it harder to write readable (and therefore easily maintainable) code. If the ID must be included in the JSON, then it should also include the name as a column so those of us who need it can use it.

AndySDH commented 3 years ago

I think the best solution is to fix this and actually have the name of the field in $field->name.

But still making sure to save the value in the database by the "field25" method (eg. where ID is 25). But not sure if that's possible (are the two things even correlated? the object and how the db stores them?)

If not, even just using the ID in the database would work, so eg. even simply "25".