Problem/Motivation
Making a custom group creation form, which includes a group preview step before saving. To do this using a view mode, and using the view builder to render the unsaved entity. All good except https://git.drupalcode.org/project/group/-/blob/3.0.x/group.module#L186 throws a exception as it can't build a URL for an entity without an ID.
Proposed resolution
Test if there's a id before running and return a blank string if there isn't one. If for some reason your template for a group that isn't saved does use the {{ url }} it will just point to the page its on.
diff --git a/group.module b/group.module
index 20979c6..57d0ec3 100644
--- a/group.module
+++ b/group.module
@@ -183,7 +183,7 @@ function template_preprocess_group(&$variables) {
$variables['group'] = $group;
$variables['view_mode'] = $variables['elements']['#view_mode'];
$variables['label'] = $group->label();
- $variables['url'] = $group->toUrl('canonical', ['language' => $group->language()]);
+ $variables['url'] = $group->id() ? $group->toUrl('canonical', ['language' => $group->language()]) : '';
$variables['attributes']['class'][] = 'group';
$variables['attributes']['class'][] = Html::cleanCssIdentifier('group--' . $variables['view_mode']);
Comments
Comment #2
kristiaanvandeneyndeNormally I side with "whatever passed in data makes sense, goes". But in this case I can see people wanting to render an incomplete Group entity, sure. Maybe we want to check isNew(), though and group the vars that a new Group would not have access to in that if-statement.
Comment #3
ekes commentedSomething like: attached patch (seems easier than making a branch for this, but can do that if preferred).
Comment #4
ekes commentedComment #5
kristiaanvandeneyndeYeah that would be exactly what I had in mind.
Comment #6
kristiaanvandeneyndeWill commit as such, just changing the issue category and name
Comment #9
kristiaanvandeneynde