With 'comment body as field', you now get the option to add the 'Comment' field to nodes or taxo terms in Field UI's 'add existing field' dropdown selector.

Similarly, you can add an instance of the (node) 'Body' field to taxo terms or comments (well, except comments don't have a UI for their fields so far for lack of an obvious place in our IA, but we need to fix that and I intend to post a patch asap)

This will lead to major confusion, and IMO qualifies as critical

We need an optional $field['entity_types'] = array('node', 'user', 'foo'); property, allowing module-defined fields to be restricted to a given set of entity types.
- field_create_instance() needs to raise an exception if an instance is being created on a forbidden entity type.
- Field UI needs to take it into account when building the list of options in the 'Add existing field' select.

Then, node body should be restricted to nodes, and comment body to comments.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

yched’s picture

Doh, I borked my formatting and there's no editing of OPs.

The end was:

We need an optional $field['entity_types'] = array('node', 'user', 'foo'); property, allowing module-defined fields to be restricted to a given set of entity types.
- field_create_instance() needs to raise an exception if an instance is being created on a forbidden entity type.
- Field UI needs to take it into account when building the list of options in the 'Add existing field' select.

Then, node body should be restricted to nodes, and comment body to comments.

moshe weitzman’s picture

Makes sense. We have similar feature which restrictions actions to certain triggers.

yched’s picture

Status: Active » Needs review
FileSize
7.98 KB
12.54 KB

Patch, with tests (I'm being lazy and will let the bot test the tests).

+ screenshot of the bug (before the fix...)

Status: Needs review » Needs work

The last submitted patch, field_restrict_types-680910-3.patch.patch, failed testing.

yched’s picture

Status: Needs work » Needs review
FileSize
8 KB

Doh, test wasn't good indeed.

Should be better.

yched’s picture

Same patch, but without the empty test method (changed my mind before rolling #5 and decided to include the test in the existing addExistingField() function, but forgot to remove the stub test method).

moshe weitzman’s picture

is object_types alterable? the trigger asociation is alterable and thats good; it lets is provide a simplified UI for the majority use case but lets modules get fancy if dev knows what he is doing.

yched’s picture

re @moshe: "is object_types alterable? ?"
Nope. We don't have alter() on $field and $instances definitions. Fried my brain everytime I tried to approach it.
Also, field_update_field() forbids changing the 'object_types' property for an existing field, because if the list of "allowed object types for instances of the field" gets further restricted dynamically, what about existing instances on object types that have just been forbidden for the field ?

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

Well, in the case of actions/triggers the association is just a GUI hint. The app does not enforce restrictions. Since nothing about the field definition is alterable today, we have no time to get into that now. Lets RTBC as is.

bjaspan’s picture

I see a test that an instance cannot be added to an object type forbidden by the field. Shouldn't there also be a test that an instance can be added to an object type allowed by the field? I added one.

yched’s picture

Many other tests would fail if 'create an instance on an allowed obj type' was broken (e.g no node body), but sure, better to have that as its own dedicated test.

Dries’s picture

The testbot should be fixed, so let's wait for the test results.

yched’s picture

The bot doesn't seem to want to see this. Re-uploading.

aspilicious’s picture

webchick’s picture

Priority: Critical » Normal
Status: Reviewed & tested by the community » Fixed

Hm. I can see why this is needed. The lack of an alter() hook though does strike me as potentially problematic. For example, Node Comment module probably wants to also have a comment body field, and will have to duplicate a bunch of code to do it.

However, this is pretty edge-casey, and this will smooth out the UI for the 99.999% of people who aren't doing things like that. So, committed to HEAD.

This does not cause Drupal to cease to function, and is therefore not critical. Downgrading.

chx’s picture

Status: Fixed » Needs review
FileSize
1.51 KB

I have started an email thread. Very productive: Barry pointed me to this issue as the place where that piece of code was born (I simply assumed it was there since the beginning so I did not run annotate, sorry) while Damien and yched fixed it up nicely which I am posting here.

Reopening because the restriction is way too restrictive: it does not allow adding any new entities to an existing field if the restriction is already set. Ick! I am close to call this critical.

Status: Needs review » Needs work

The last submitted patch, noadd.patch, failed testing.

scroogie’s picture

So what does #16 mean exactly? That you can't modify the allowed entities subsequently?

chx’s picture

Yeah, that's the problem.

Alan D.’s picture

sweet, new drupal wtfs. Always wanted to know why we couldn't replicate the body in other entities, but I can see the reason from an end-users UI perspective. Is there any negative to removing the lock if we alter this manually? There doesn't appear to be any negatives in the code.

For interested parties, this was the update that we used to alter the field "field_featured_image", followed by a devel cache flush:

$field = db_select('field_config', 'c')->fields('c')->condition('field_name', 'field_featured_image')->execute()->fetchObject();
$field->data = unserialize($field->data);
$field->data['entity_types'] = array();
drupal_write_record('field_config', $field, array('field_name'));

Big plus one for making this a configurable option in the field settings. Happy if you could not reduce only increase the allowed range of entities to keep it as simple as possible.

[edit]
Note that this thread refers to object_xx, this was renamed to entity_xx
Also you can not use field_update_field($field) as this throws an exception if you change the entity_types value.

fgm’s picture

Version: 7.x-dev » 8.x-dev

Just noticed this trying to build a field type that would only be allowed on a specific entity type (node) because it has side-effects on the carrying entity (publish/unpublish, in that case). In that specific case, I guess, this means one will have to just emit a warning in the UI.

Still, even without an alter hook, it would be nicer if there was a way to define entity_types in hook_field_info() as a default instead of hardcoding them to array() in field_create_field(). That will have to be bumped to 8.x, I'm afraid.

tterranigma’s picture

Issue summary: View changes
Status: Needs work » Fixed

In Drupal 8.0.x, fields seem to be restricted per entity type. I could not find a relevant change record or issue, but viewing the db of a Drupal 8 installation and playing with the admin interface, I feel confident that the issue here is fixed.

Status: Fixed » Closed (fixed)

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