When trying to create a new entity module using entity operations, I encountered a situation. My entity had only one bundle / entity type, but it was named differently to the core entity. When trying to add a new instance of my entity (on the add form), I would encounter a fatal error from common.inc and the entity_extract_ids function.
As the code stands at the moment, if there is only one bundle, the module automatically assumes the name of the bundle is the same as that of the entity and does not check the name of the bundle and goes straight to to the entity_ui_add_form. We need to add some code that will check the name of the bundle if the bundle entity key value is set. If it is not then the standard behaviour can be kept.
I have discussed this issue with Joachim at some length on IRC so filing this issue as suggested by him.
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | entity_operations-add-form-single-bundle-2233507-6.patch | 1.56 KB | jeebsuk |
Comments
Comment #1
joachim commentedThanks for filing :)
The problem is in EntityOperationsOperationAdd::build().
I've made the stupid assumption that with only one bundle, there's no bundle key on the entity. Hence the proto-entity is broken in your case.
What we need here is something like:
Then you need to use array_pop() to get the machine name of the only bundle, and put that into a $values array for entity_create().
Comment #2
jeebsuk commentedWhat I've got which seems to work is the following:
See what you think to that - if you think that looks OK I'll build a patch.
Comment #3
joachim commentedThat's the right approach. What I'd to to aid legibility though is have less stuff going on in a conditional block, so:
So rather than 2 separate cases, it's 'do something special first, then do something common'.
Comment #4
jeebsuk commentedSecond go (which works):
Comment #5
joachim commentedLooks good. A few small things:
- you need to initialize $values to an empty array() before the if(){}, otherwise it'll be undeclared if you don't go into the if()
- let's say $bundle_key = $entity_info['entity keys']['bundle']; as that's the variable name used elsewhere in the module code. Consistency helps readability.
- comments need to be full sentences with capital and full stop and wrapped to 80 characters
Can you make a patch?
Comment #6
jeebsuk commentedThis is the first time I've created a patch so apologies if it's not right - but it seems to be based on various ones I've had to apply.
Comment #7
jeebsuk commentedComment #9
joachim commentedArgh, the tests were failing on their own.
I've fixed the generic form tests. Not sure why the Views plugin test is failing, as for me it passes locally. Let's re-test and see what happens...
Comment #10
joachim commented6: entity_operations-add-form-single-bundle-2233507-6.patch queued for re-testing.
Comment #12
joachim commentedTests all pass for me locally, so committing.
Thanks for the patch!