Problem/Motivation
If you have an entity type that has bundles, using _entity_form: $entity_type.add
does not work because that ends up calling \Drupal\Core\Entity\EntityForm::getEntityFromRouteMatch()
which calls \Drupal\Core\Entity\EntityStorageInterface::create()
with an empty array, in particular without the value for the bundle.
Nodes, taxonomy terms, and basically all entity types in contrib (including those using Entity API or Drupal Console) work around this issue by using a dedicated controller instead of the magic _entity_form: $entity_type.add
.
However, the bundle value is readily available in the passed route match in getEntityFromRouteMatch()
we just have to fetch it before calling EntityStorageInterface::create()
.
Proposed resolution
Fetch the bundle from the route match in \Drupal\Core\Entity\EntityForm::getEntityFromRouteMatch()
. This makes it easier for all entities that have bundles. Also, it will make #2596095: Provide a route provider for add-form of entities a little easier.
Remaining tasks
Task | Novice task? | Contributor instructions | Complete? |
---|---|---|---|
Create a patch | Instructions | Yes | |
Add automated tests | Instructions | Yes | |
Review patch to ensure that it fixes the issue, stays within scope, is properly documented, and follows coding standards | Instructions |
User interface changes
None.
API changes
None. Everything that worked before will continue to work. Some things that threw an exception before because of a missing bundle (and thus were worked around, see above) will now magically work. That cannot be considered an API change.
Data model changes
None.
Comment | File | Size | Author |
---|---|---|---|
#17 | interdiff.txt | 1.47 KB | bojanz |
#17 | 2651716-17.patch | 13.4 KB | bojanz |
#13 | 2651716-13.patch | 13.4 KB | tstoeckler |
Comments
Comment #2
tstoecklerIn addition to the unit test I converted the test entity types to use this new pattern to remove some of the code, so I expect some fallout from that. Let's see.
Comment #4
tstoecklerComment #5
tstoeckler...and I'm an idiot. Sorry, testbot, I really didn't mean it!
Comment #7
dawehnerCan we split up the tests into several methods?
Comment #9
tstoecklerIf there's a prize for dumbest patch roller of the month, I think the decision should be clear by now...
Fixed #7, thanks for the review! I tried to reduce the code duplication between the now-separate methods, I hope that was the direction you were getting at.
Comment #11
tstoecklerWow, I still think the default value for the bundle is very, very strange, and it would have been nice automatic test coverage, but there are to many fails. Leaving the entity test routing changes as that still covers the new code and is also a nice cleanup.
Comment #13
tstoeckler@tstoeckler: FYI, #2596095: Provide a route provider for add-form of entities isn't in yet, in case you didn't notice.
……………………………………..________
………………………………,.-‘”……………….“~.,
………………………..,.-“……………………………..”-.,
…………………….,/………………………………………..”:,
…………………,?………………………………………………\,
………………./…………………………………………………..,}
……………../………………………………………………,:`^`..}
……………/……………………………………………,:”………/
…………..?…..__…………………………………..:`………../
…………./__.(…..”~-,_…………………………,:`………./
………../(_….”~,_……..”~,_………………..,:`…….._/
……….{.._$;_……”=,_…….”-,_…….,.-~-,},.~”;/….}
………..((…..*~_…….”=-._……”;,,./`…./”…………../
…,,,___.\`~,……”~.,………………..`…..}…………../
…………(….`=-,,…….`……………………(……;_,,-”
…………/.`~,……`-………………………….\……/\
………….\`~.*-,……………………………….|,./…..\,__
,,_……….}.>-._\……………………………..|…………..`=~-,
…..`=~-,_\_……`\,……………………………\
……………….`=~-,,.\,………………………….\
…………………………..`:,,………………………`\…………..__
……………………………….`=-,……………….,%`>–==“
…………………………………._\……….._,-%…….`\
……………………………..,<`.._|_,-&“…………….`
Comment #14
tstoecklerFinally, yay!
So the annotation changes are not actually necessary now to make this green. They are correct, though, and will become necessary in #2596095: Provide a route provider for add-form of entities so I personally wouldn't mind leaving them in. I will of course comply if anyone feels that should be another issue. Same goes for the super minor fix to declaring the test entity type IDs at the right place.
Comment #15
bojanz CreditAttribution: bojanz at Centarro commentedThis looks good to me.
Small nitpick:
The second if could be an elseif.
Otherwise looks RTBC ready.
Comment #16
dawehnerYeah, let's fix #15 and call it a day.
Comment #17
bojanz CreditAttribution: bojanz at Centarro commentedHere we go.
Comment #18
tstoecklerYay, thanks! That's a very good point.
Comment #19
catchLooks great and I couldn't find anything to complain about. Committed/pushed to 8.1.x, thanks!
Comment #21
kristiaanvandeneyndeVery nice addition to Entity API, well done!
Comment #23
fgmAdded followup issue about route defaults at #2743303: EntityForm::getEntityFromRouteMatch() does not support route-level parameters