Score:1

How to use bundle classes with config entities (vocabularies)?

ru flag

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?

4uk4 avatar
cn flag
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.
ru flag
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"*?
ru flag
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
4uk4 avatar
cn flag
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.
Score:1
cn flag

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.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.