I'd like to point out that there are several cases in GroupContentStorage where exception is thrown a bit unnecessary in my opinion.
For example in loadByEntity() there is following comment and code:
public function loadByGroup(GroupInterface $group, $plugin_id = NULL, $filters = []) {
// An unsaved group cannot have any content.
if ($group->id() === NULL) {
throw new EntityStorageException("Cannot load GroupContent entities for an unsaved group.");
}
But as the comment says, there can not be any group content, so it could just silently return empty array, no harm done.
The problem with these exceptions is that developer just has to know not to request content out of unsaved group, since IDE will have hard time to figure out that the call might return exception.
So if the exception actually is required, I'd like it to be added in the documentation, there's undocumented exceptions in at least following functions:
\Drupal\group\Entity\Storage\GroupContentStorage::createForEntityInGroup
\Drupal\group\Entity\Storage\GroupContentStorage::loadByGroup
\Drupal\group\Entity\Storage\GroupContentStorage::loadByEntity
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | group-groupcontentstorage_exceptions-3040478.patch | 1.06 KB | bisupr |
Comments
Comment #2
geek-merlinLooks like i filed the same as a bug.
Comment #3
maxwellkeeble commentedI agree that this should return an empty array - all the functions I've found that use it seem to be expecting that. I'm getting a hard-to-debug error on my production site relating to this (with plugin_id "group_membership"), and returning an empty array sorts it.
Comment #4
kristiaanvandeneyndeI might consider returning an empty array. Still on the fence, but it might not do as much harm as I first envisioned.
Comment #5
bisupr commentedI have been getting similar error quite randomly now and I agree with the idea of returning empty array, not just for `loadByGroup` but also for `loadByEntity`. So far, in our test scenarios, I haven't seen any issues with it.
In my case, the exception was pretty easily reproducible if layout_builder is enabled and you are trying manage default layout in Content view display. In such case, there is no entity id at all and layout builder tries to preview the output and also, in other preview mode for unsaved node.
So, I am adding a related patch for this here.
Comment #6
scotwith1tSame here. Patch works as advertised. Love to see this one get in, can't imagine how loading an empty array instead of throwing the exception will hurt anything. Thanks!
Comment #7
kristiaanvandeneyndeI'll pick this one up for 2.0.x. I agree it probably can't do any harm, but I'd rather wait for a major update because that usually has people on their toes more about possible changes. And to be fair, it is changing the existing behavior.
Comment #8
kristiaanvandeneyndeClosing for 8.x-1.x and fixing for 2.0.x here: #3258942: Optimize GroupContentStorage and calls to it. Will give credit here as it was a support request, so Fixed is an acceptable status.