1

I'd like to use bundle classes with taxonomy vocabularies. Vocabularies are bundleable config entities inheriting from EntityBase just like nodes, term and other content entities. The doc states bundle classes should work with all entities that are a subclass of the base entity class, but I can't get it to work with config entities.

works:

// in my module file
function foo_entity_bundle_info_alter(&$bundles) {
  if (isset($bundles['taxonomy_term']['foo'])) {
    $bundles['taxonomy_term']['foo']['class'] = TermFoo::class;
  }   
}

// ...somewhere else in my code...
// this correctly returns instanceof TermFoo
\Drupal::entityTypeManager()->getStorage('taxonomy_term')->load(123)

does not work:

// in my module file
function bar_entity_bundle_info_alter(&$bundles) {
  if (isset($bundles['taxonomy_vocabulary']['bar'])) {
    $bundles['taxonomy_vocabulary']['bar']['class'] = VocabularyBar::class;
  }   
}

// ...somewhere else in my code...
// this returns instanceof \Drupal\taxonomy\Entity\Vocabulary
// but I want instanceof \Drupal\bar\Entity\VocabularyBar
\Drupal::entityTypeManager()->getStorage('taxonomy_vocabulary')->load('bar')

During debugging I noticed that the hook's parameter $bundles['taxonomy_term'] had all bundles as sub-keys, matching the recommended if-statement.

But $bundles['taxonomy_vocabulary'] was missing all bundles, it only had a single sub-key $bundles['taxonomy_vocabulary']['taxonomy_vocabulary'], no bundles as keys anywhere, so the bundle class was never injected.

I then tried some dumb things in good faith like

function bar_entity_bundle_info_alter(&$bundles) {
  // remove guarding if clause
  //if (isset($bundles['taxonomy_vocabulary']['bar'])) {
    $bundles['taxonomy_vocabulary']['bar']['class'] = VocabularyBar::class;
  //
}   

or

function bar_entity_bundle_info_alter(&$bundles) {
  // add extra nesting
  if (isset($bundles['taxonomy_vocabulary']['bar'])) {
    $bundles['taxonomy_vocabulary']['taxonomy_vocabulary']['bar']['class'] = VocabularyBar::class;
  }   
}

but of course this did not work.

Is the doc simply incorrect or what am I missing to use bundle classes with config entities?

4
  • Each vocabulary name is the ID of a bundle config item. So what you are trying to do would be a separate class for each ID, like for a node 1,2,3, ... This is not possible. Vocabularies are not bundleable config entities, they are config entities storing bundle metadata for a content entity.
    – 4uk4
    Commented Jul 12, 2023 at 12:47
  • So the linked doc is inaccurate and should state "must be a subclass of the ContentEntityBase class" instead of "must be a subclass of the base entity class"?
    – Hudri
    Commented Jul 12, 2023 at 12:50
  • Quick testing: Using taxonomy_vocabulary as bundle name also doesn't work : In hook $bundles['taxonomy_vocabulary']['taxonomy_vocabulary']['class'] = VocabularyBar::class; will still return core Vocabulary class from entity storage, and not my custom class
    – Hudri
    Commented Jul 12, 2023 at 13:01
  • My comment is about this specific config entity which is for sure not a bundleable entity. Never seen one, would be interesting to see an example and the use case for this. Also, in future, there could be a third entity variant, which is not a content entity but is using bundles.
    – 4uk4
    Commented Jul 12, 2023 at 13:08

1 Answer 1

1

Is the doc simply incorrect or what am I missing to use bundle classes with config entities?

The issue you are experiencing is that the bundle Vocabulary is only a bundle in relation to the content entity Term. The bundle metadata is stored as config entity items. This doesn't mean that this config entity is now bundleable only because it stores bundle metadata of another entity.

If you want to test this you first need a bundleable config entity type. I don't know if there is a use case for this or if this is even possible, but for the doc this doesn't matter. You could construct an entity out of the entity base class which is not content/config but a new variant needing bundles but not the features of content/config entities.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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