There are some tools in Drupal that allow you to change the bundle of an entity (e.g. the node type):

(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.

  • Would it be a choice if you use a field to store the bundle type?
    – Jimmy Ko
    Commented Jul 22, 2016 at 22:58
  • @JimmyKo If this solves the problem, why not. But does it?
    – donquixote
    Commented Jul 23, 2016 at 19:39
  • Also I could "cheat" and just say that revisions before a certain date are the old bundle, and revisions past that date are the new bundle. But this still means I need to somehow tell the entity controller. Maybe just register my own overridden controller for commerce products?
    – donquixote
    Commented Jul 23, 2016 at 19:41
  • Adding a new field may cause less problem because I suspect if core and modules assume the structure of revision table is same as the entity one. But I just come up an idea. Maybe you can add the bundle column on both entity and revision table. IMPO, it won't cause too much problem because most module only handle the entity table and ignore the revision one.
    – Jimmy Ko
    Commented Jul 23, 2016 at 22:23
  • So I store the bundle in a field. Then what? Drupal still thinks that it is the other bundle.
    – donquixote
    Commented Jul 23, 2016 at 23:22


Your Answer

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

Browse other questions tagged or ask your own question.