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']);
CommentFileSizeAuthor
#3 3346752-preprocess-is-new.patch1.68 KBekes

Comments

ekes created an issue. See original summary.

kristiaanvandeneynde’s picture

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

ekes’s picture

StatusFileSize
new1.68 KB

Something like: attached patch (seems easier than making a branch for this, but can do that if preferred).

ekes’s picture

Status: Active » Needs review
kristiaanvandeneynde’s picture

Yeah that would be exactly what I had in mind.

kristiaanvandeneynde’s picture

Title: Group preview » Allow previewing of unsaved Group entity
Category: Bug report » Feature request
Status: Needs review » Reviewed & tested by the community

Will commit as such, just changing the issue category and name

kristiaanvandeneynde’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.