2

I'm trying to create a module which creates a taxonomy vocabulary on installation of the module, and creates one text field on the taxonomy vocabulary/term. But I'm getting the following error when I enable my module:

[error] FieldException: Attempt to create an instance of field field_flickr_photoset on forbidden entity type taxonomy_term. in field_create_instance() (line 468 of /var/www/www.example.com/htdocs/modules/field/field.crud.inc).

First of al, why a Forbidden entity type? Is it not possible to create a field on a taxonomy vocabulary through code? Or am I missing some parameters?

I used very similar code to create a node type and attach fields to it, which currently works without any problems. I didn't think that changing the entity type would cause such problems.

I tried searching on google and here on how to do this, but all I get is people who are asking how to attach a taxonomy term reference field to a node type, which is not what I'm asking. It's attaching a text field to a taxonomy term/vocabulary.

3

If you look in taxonomy_entity_info() you'll see this code that defines the taxonomy_vocabulary entity type:

$return['taxonomy_vocabulary'] = array(
  'label' => t('Taxonomy vocabulary'),
  'controller class' => 'TaxonomyVocabularyController',
  'base table' => 'taxonomy_vocabulary',
  'entity keys' => array(
    'id' => 'vid',
    'label' => 'name',
  ),
  'fieldable' => FALSE,
);

The important bit to note is 'fieldable' => FALSE...that's why it's 'forbidden' to add fields to that type.

The taxonomy_term entity type is fieldable so you won't have any problem adding field instances to one of those.

If you really want to add a field to the vocabulary, you'll need to implement hook_entity_info_alter() to change the vocabulary entity to 'fieldable' => TRUE (I've done it before, it's perfectly safe):

function MYMODULE_entity_info_alter(&$info) {
  $info['taxonomy_vocabulary']['fieldable'] = TRUE;
}
| improve this answer | |
  • I actually didn't know that taxonomy_vocabulary and taxonomy_term were different entities. So thanks for this info. When I first written the code I set the entity_type for the field as taxonomy_vocabulary and enabled the module. And even after I disabled my module, it didn't remove the field from db, and it was still set as taxonomy_vocabulary entity type field in {field_config} table. So even after changing my code, and disabling re-enabling cc all-ing I was scratching my head. Now I deleted the record and re-enabled and now the field is created. Your info was very useful thanks. – Beebee Jul 6 '12 at 9:23
0

Drupal 7

If you check the code which causing that Exception, you'll see the following condition:

  // Check that the field can be attached to this entity type.
  if (!empty($field['entity_types']) && !in_array($instance['entity_type'], $field['entity_types'])) {
    throw new FieldException(t('Attempt to create an instance of field @field_name on forbidden entity type @entity_type.', array('@field_name' => $instance['field_name'], '@entity_type' => $instance['entity_type'])));
  }

That means the field which you're trying to define has restricted entity_types attribute which has list of entities to which the field can be attached.

So either you need to define your entity via the following property of your field:

  'entity_types' => array(0 => 'allowed_entity_type'),

leave it empty, or just try to remove entity_types attribute from the field definition at all.


Don't forget to clear the caches in Performance.

If the problem persist, you may try to remove the field to be able to recreate it again (e.g. drush eval "field_delete_field('field_which_is_broken');").

| improve this answer | |

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.