Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
field_
... every Drupal person knows, this is a configurable field. I question now though this behaviour. An entity consists of fields, and each has a name. This is the message we should really communicate.
Do we want to drop it?
What does this mean from a community perspective?
Advantages
- This would automatically make our REST support better, as there is less Drupalism involved.
Disadvantages
- A decade of people with experience about it.
Proposed resolution
Remaining tasks
User interface changes
API changes
Data model changes
Comment | File | Size | Author |
---|---|---|---|
#2 | 2846872-2.patch | 351 bytes | dawehner |
Comments
Comment #2
dawehnerThis is set to active due to the required discussion.
Comment #3
cilefen CreditAttribution: cilefen commented+1 It is redundant.
Comment #4
dawehnerAdd expanded the issue summary a tiny little bit. I guess we could discuss that still way better.
Comment #5
BerdirHm. I often *change* it to something else, but I think having some sort of prefix makes sense.
We have protection against conflicts with base fields, but you never know what will be added in the future. Having a prefix limits the risk of ever running into a conflict when an entity type gets a new base field, either by itself or anotherm module.
I also use it in our install profile to set it to np8_, and then change it on projects to something project-specific, so I can add more np8_* fields later on without risk of conflict. That's still possible, but by not having a default, people might by less likely to think of that?
This will also break *tons* of tests that currently create fields with e.g. fieldUIAddNewField() and then somehow assert or access that field.
Counter-Proposal: Expose it in the UI, with a recommendation to have a prefix but sites that don't want one can remove it, on their own risk.
Comment #6
lslinnet CreditAttribution: lslinnet as a volunteer and at FFW commentedHow will "This would automatically make our REST support better, as there is less Drupalism involved." be achieved by removing the "field_" prefix? based on what standard will this name change make the REST support better?
Comment #7
yched CreditAttribution: yched commentedAs @Berdir points, the intent of the prefix is to allow a core / contrib entity type to add new base fields in any minor / major release without fearing it will clash somewhere with someone's configurable fields (and possibly with supporting business logic around $entity->field_foobar in custom code)
Granted, the specific 'field_‘ string litteral was picked at a time when the non-'field_' things in an entity were not fields...
Today that string is less relevant as a differenciator. But the point of having *some* prefix remains.
Comment #9
e0ipsoI think that that highlights that this protection is brittle, since it will still clash somewhere with someone's base fields. One could argue that to avoid collisions field IDs should not be human generated. I get that even if it was a good idea to have machine generated field IDs (there are several pretty obvious drawbacks) it's too late for that implementation.
However I think that this highlights how we are acknowledging some level of conflicts. I think that if we drop the prefix we'll only have potential conflicts when core is adding a base field, since field machine names are ensured to be unique when configuring a field.
My question is: is that marginal increase on risk worth keeping the prefix?
Comment #10
Wim LeersComment #11
e0ipsoNow that we have JSON API Extras and #2846488: Explore abstracting away "Drupalisms", we can close this. Thanks Wim for bringing it up.
Comment #12
gabesulliceI think it's worth considering this again, perhaps in the context of Drupal 9.
When Drupal is used in an API-first context, the distinction between a machine name and a field label gets blurred since the machine name might be exposed to 3rd party developers who are not the developers of the Drupal site itself.
The GraphQL and JSON:API modules both have workarounds for this, but the DX/site builder experience could be improved if the field name that is exposed via their public APIs were configurable in Drupal core (perhaps on the field config page).
Since it wasn't already mentioned explicitly, I'll call it out: Snake case is not at all the idiomatic style in JavaScript or JSON-based APIs. It's almost universally lower camel case.
Comment #13
ifrikAn example anecdote where it can break things.
I have a site that was migrated from D6 to D7 and then D8. This resulted in a field on the user account with the field name "homepage".
Anonymous users could no access node pages while logged-in users could.
template_preprocess_username accesses a property $account->homepage, not sure why something to do with comments maybe in the past. The content of $account->homepage is certainly not expected to be a field object.
Comment #15
xjmWe'd need to figure out a way to make this BC with an upgrade path. Since it's only the default for new things, it can be done in a minor, but we'd need to make sure we're not breaking existing assumptions.
Since 8.9.x and 9.0.x are now in beta, I'm moving this to 9.1.x. Thanks!