Field API should refuse to create field names that override other property names identified by fieldable info (e.g. for node: nid, vid, type). We may want to allow fieldable info to specify additional reserved names as well.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bjaspan’s picture

Priority: Normal » Critical
scor’s picture

would you hardcode that blacklist or generate it from looking at the fieldable info of a node?

KarenS’s picture

Wouldn't it be more flexible to use a hook or callback for this so any module can specify names that should not be used? Maybe on a bundle by bundle basis (the name is OK on a page bundle but not OK on my custom bundle).

fgm’s picture

Assigned: Unassigned » fgm

I'm gonna try this one (see http://drupal.org/community-initiatives/drupal-core/fields)

I think some historical info may still need to be hardcoded, but the rest should be available from the fieldable info. Adding a hook or callback would likely add unneeded load for little benefit, as field creation is a statistically rare occurrence.

bjaspan’s picture

Thanks, Frederic!

IIRC the motivation behind this issue was #367595: Translatable fields and the possible "language" property.

I'm not sure why a separate hook should be needed since hook_fieldable_info() can return a list. Do we want to call a hook for non-fieldable modules to return data about fieldable entity types? Seems odd.

We have to figure out what to do if a "reserved name" shows up in the banned list after a field with that name already exists.

Currently neither node nor user makes any attempt to prevent this kind of problem. If this turns into an impossible rathole, then we don't really have to deal with it either.

KarenS’s picture

Field creation is a statistically rare occurrence, so doing a bit of extra work to make sure it is set up in a way that will create other problems has no big cost. And a field that uses a name that some other module expected to be able to use could create potentially significant problems, so it seems like it has a good cost-to-benefit ratio.

Sure it hasn't come up so far, but we're going down a path we've never been on before adding fields to everything and making all kinds of things that were not fields before into fields. I have no specific use case, but was thinking about something like a modules that work together to do complex processing (i.e. ecommerce or finance) that need to know that any fields with specific names are 'their' fields. And the Node module could protect 'body', 'teaser', and 'title' and the User module could protect 'name', 'mail', and 'password' as well using the same method.

We don't have to do this of course, but there is very little cost to creating a flexible way to let modules tells us what names are not 'allowable' instead of using a hard-coded list that later breaks something.

yched’s picture

I'd tend to think the safest way to prevent those clashes is to promote naming conventions, like : 'prefix your fields with your module's name - if you don't do this, don't come crying'. Core-defined fields could possibly be a safe exception (body, teaser, title), for legacy's sake.

KarenS’s picture

OK, I'll back down on this idea. Just thought it was worth exploring.

bjaspan’s picture

I'm also thinking that per-module protected names only need to be protected (if they do at all) within an object type. e.g. suppose that node module protects title. Why should I not be able to create a field named title and just not attach it to nodes?

For object properties (such as 'language', if we end up using that) that apply to *every* object with fields attached, the Field API would flat out prohibit it as a field name. That was the original point of this issue.

Maybe we should hold off until we actually have such object properties used globally by field api.

aaron’s picture

also need to think about what to do when adding a new module that reserves a name already in use. for instance, one creates a ->location field, then later installs the location module.

yched’s picture

Re arron #10 : The fields added through the UI will all have a forced prefix (probably 'field_' to stay consistent with CCK D6 names)
Modules need to follow good practice and prefix the fields they create by the module name to avoid name clashes.

Note that we just made an exception with 'body' field (instead of 'node_body'). That's probably OK here (really more straightforward for this very common field), but that doesn't set contrib up for good practice...

fgm’s picture

Status: Active » Needs work
FileSize
1.06 KB

OK, sorry it took me so long for so little, but here is a first attempt, just to make sure this is really what this request is about.

fgm’s picture

Status: Needs work » Needs review
FileSize
2.07 KB

Rerolled with added simpletest.

Status: Needs review » Needs work

The last submitted patch failed testing.

fgm’s picture

Status: Needs work » Needs review
FileSize
2.08 KB

Rerolled against today's HEAD.

bjaspan’s picture

I'm wondering if this whole issues counts as over-engineering, and we should just go with the plan in #7.

Status: Needs review » Needs work

The last submitted patch failed testing.

sun.core’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch failed testing.

fgm’s picture

Status: Needs work » Needs review

Why did it suddently stop passing ?

Status: Needs review » Needs work

The last submitted patch failed testing.

fgm’s picture

Status: Needs work » Needs review
FileSize
2.1 KB

Revised version for today's head.

Status: Needs review » Needs work

The last submitted patch failed testing.

fgm’s picture

Status: Needs work » Needs review
FileSize
2.04 KB

Rerolled from root..

Status: Needs review » Needs work

The last submitted patch failed testing.

fgm’s picture

Status: Needs work » Needs review
FileSize
2.1 KB

Patch format problem. Grrr.

fgm’s picture

Issue tags: +Fields in Core

I *think* the "Fields in core" tag needs to be added

bjaspan’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
2.47 KB

Code style cleanup, improved comment (we do not reference d.o issues in code), and slight test improvement (use test_entity, not node, as part of the test case). Otherwise, works.

fgm’s picture

Minor nitpicking, but shouldn't we say ""...from the "test_entity" bundle" instead of "....from the "test_entity" type" ?

bjaspan’s picture

No. The revision id key is defined by the entity type. A bundle is just a name for a collection of fields to load at once.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Cool, I committed this to HEAD.

It's hard to tell from the replies above if we need to expand this out to cover more instances or not, but for now marking fixed.

Status: Fixed » Closed (fixed)
Issue tags: -Fields in Core

Automatically closed -- issue fixed for 2 weeks with no activity.