5

For a current project our taxonomy terms need a text field with a summary; naturally as there's already a body field available on nodes, I thought I'd just use that.

Confusingly, 'body' doesn't appear in the list of existing fields when managing fields for the taxonomy term. No problem thought I, I'll just use field_create_instance() and add it manually. Drupal objected:

Attempt to create an instance of field body on forbidden entity type taxonomy_term.

So I thought I'd get clever. I loaded up the field config, added taxonomy_term to the array of entity_types, and tried to update with field_update_field(). Nope:

Cannot change an existing field's entity_types property.

It turns out node_add_body_field() does this unconditionally:

$field = array(
  'field_name' => 'body',
  'type' => 'text_with_summary',
  'entity_types' => array('node'),
);
$field = field_create_field($field);

I (perhaps naively, not sure yet) disagreed with Drupal's various assessments, so I went into the database and manually added taxonomy_term to the list of allowed entity types for the body field. Works great, I haven't noticed any ill-effects so far, and the body field is where I expect it to be.

So I guess my question is two-fold:

  • What reason could there be for the node module locking down this field to a single entity type?
  • Can I expect to see any bad effects from this further down the line?
3

I would expect it is for historical reasons and is a place to migrate data from the old Drupal 6 body field.

Since the old Drupal 6 body field was just for nodes it seems in some ways logical that they would then also restrict this to just nodes so it is as it was in Drupal 6.

A couple of quick (not so thorough) greps didn't turn up any code that looks like it would cause you trouble using it for other entities but I could have missed something.

I would not think that this would cause you much trouble in future but I'm not sure of that.

A possible downside though is that it is in some ways like hacking core. If someone else inherits your site they may find it unusual that your site behaves differently than usual.

Personally sometimes I remove the drupal core body field and add my own field_body and use that throughout the site.

  • I never even considered it might be a D6 remnant, really good point. I think that makes as much sense as anything, thanks :) – Clive Nov 18 '14 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.