There are some tools in Drupal that allow you to change the bundle of an entity (e.g. the node type):
- The Node Convert module
- The migrate module, if you update an entity and change the bundle.
- Any custom programmatical bundle change, see Change entity bundle on update?
(In my case it is about commerce_product entities where the bundle changes from "product" to something else)
Entity revisions, if enabled, can remember all the field data from the old entity, even if these fields do not exist in the newly assigned bundle.
However, when loading the old revision with e.g. entity_load()
, it has the new bundle, not the old one. And field_attach_load()
only loads field data for fields of the new bundle.
A place to look is DrupalDefaultEntityController::load()
. This queries the database for the entity / revision, and then calls $this->attachLoad()
to add the field data. Then e.g. CommerceProductEntityController::attachLoad()
calls the parent DrupalDefaultEntityController::attachLoad()
, which calls field_attach_load_revision()
, which calls field_attach_load()
, which determines the entity bundle and loads the field instances for the determined bundle. The bundle is already on the entity since it was loaded from the database.
It seems the main problem is that (most or all of) the revision tables do not have a column for the bundle. I assume this could be changed with hook_schema_alter()
to add a type
column to commerce_product_revision
(or node_revision
). But will this cause other problems?
Has anyone tried this so far, or knows an existing solution? Anything I can think of right now seems hackish and fragile.