Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
While doing some theming I noticed that previewing comments completely break the layout. The problem is in comment_preview() here:
$comment_build = comment_view($comment, $node);
$comment_build += array(
'#weight' => -100,
'#prefix' => '<div class="preview">',
'#suffix' => '</div>',
);
The #prefix doesn't actually end up being used. There is a #prefix that is set in comment_view() that is used instead:
// Add anchor for each comment.
$prefix .= "<a id=\"comment-$comment->cid\"></a>\n";
$build['#prefix'] = $prefix;
I don't know the correct way to fix this, and it seems like it could be part of a larger issue, but removing the preview <div>
obviously fixes the markup problem. The reason I bring this up is because I'm not sure if we still need this <div>
since we have a comment-preview class being added during template_preprocess_comment().
Comment | File | Size | Author |
---|---|---|---|
#29 | comment-previews.patch | 2.54 KB | bleen |
#27 | comment-previews.patch | 2.59 KB | bleen |
#26 | comment-previews.patch | 854 bytes | bleen |
#24 | comment-previews.patch | 2.43 KB | bleen |
#21 | comment-previews.patch | 2.51 KB | bleen |
Comments
Comment #1
jensimmons CreditAttribution: jensimmons commentedThis is causing problems that were reported in the Bartik issue #748314: Sidebar drops on comment preview. It's a problem that needs to be fixed in core.
Ugly, right? Wanna fix it?
Comment #2
ChrisRL CreditAttribution: ChrisRL commentedThis still happens in alpha6, and indeed probably the latest checkout from CVS HEAD.
I'm marking this critical, as it is a core issue that affects all themes. Feel free to whack me over the head if this is not correct thinking.
I've attached a patch which I believe to be the intended behavior (that is, to overwrite the original prefix, which is a fairly useless anchor).
Comment #3
ChrisRL CreditAttribution: ChrisRL commentedChanging the status to needs review.
Comment #5
ChrisRL CreditAttribution: ChrisRL commentedRerolled without the path prefix... Ugh, this is a bad format to require patches in.
Comment #6
reglogge CreditAttribution: reglogge commentedHave you checked this issue against 7.0-dev? I don't have this with problem with the dropping sidebar there. Seems to have been fixed...
Comment #7
ChrisRL CreditAttribution: ChrisRL commentedI haven't directly checked it, but the code involving the comment preview (and, indeed, all of comment.module) is the same, so the bug is still there. It could be theme-dependent whether it's noticed or not, but it always breaks the structure of the page.
Comment #8
reglogge CreditAttribution: reglogge commentedMy dumb... I checked with only the first sidebar active :-(
The bug still exists, of course. The patch in #5 solves the problem in both Garland and Bartik.
Comment #9
rbayliss CreditAttribution: rbayliss commentedYup, #5 fixes the problem for me.
Comment #10
bleen CreditAttribution: bleen commentedEDIT: I was completely wrong when I wrote this ... removed my erroneous comment
Comment #11
bleen CreditAttribution: bleen commentedrestoring status
Comment #12
bleen CreditAttribution: bleen commentedI went back and fourth on this for a few minutes and I believe it is technically more correct to use array_merge in this case. Also a quick glance at core tells me that we generally use array_merge. So... see patch
Comment #13
ChrisRL CreditAttribution: ChrisRL commentedThere is no technical reason to use array_merge (or not), apart from it being more consistent with the core code - and that's certainly a good enough reason to use it.
It would be different if there were numeric indexes involved, but there are not. I only used + because I'm not familiar with the conventions used in core, and + was used originally.
Comment #14
ChrisRL CreditAttribution: ChrisRL commentedThe patch in #12 is not correct - array_merge does not alter the array that is passed, it returns a new array.
Comment #15
bleen CreditAttribution: bleen commentedoops ... forgot to fix that after I was testing some stuff
Comment #16
moshe weitzman CreditAttribution: moshe weitzman commenteda common mistake (for me, anyway).
Comment #17
sunA line of code documentation would justify the proper method to override these properties and also explain what is being done here and why for future readers. array_merge() is 100% overhead here.
Comment #18
moshe weitzman CreditAttribution: moshe weitzman commentedbetter. thx.
Comment #19
Dries CreditAttribution: Dries commentedIt might be cleaner to add a preview flag to comment_view()? At least that would keep all the code together?
Comment #20
bleen CreditAttribution: bleen commentedSomething like this?
Comment #21
bleen CreditAttribution: bleen commentedsame as #21 without whitespace probs
Comment #22
sunThe original post additionally stated that we do not need that div.preview container anymore, because http://api.drupal.org/api/function/template_preprocess_comment/7 already sets a .comment-preview class for http://api.drupal.org/api/drupal/themes--bartik--templates--comment.tpl....
Hence, the only remaining property would be the #weight. Personally, I'd say that all preview logic belongs into comment_preview(), and should not be mixed into the generic comment_view() function. Would be interesting to know what moshe thinks, as this also cries for a design pattern/standard regarding renderable $build arrays and where to build/prepare what.
Comment #23
moshe weitzman CreditAttribution: moshe weitzman commentedI'm not too keen on $comment->in_preview but thats consistent with nodes so lets go with it IMO.
Comment #24
bleen CreditAttribution: bleen commentedsun is correct about the redundant .preview container.
This patch removes it, but leaves the #weight property in comment_view() (as per Moshe's comment in #23)
Comment #25
sunThe $comment->in_preview property itself is consistent, yes. However, neither node_preview() nor theme_node_preview() (which looks odd on its own) nor node_view() is setting a special negative #weight.
To me, it looks not clean to have comment_view() account for a special #weight that may be useful when previewing. comment_view() should only care for building the requested view mode - setting a #weight would be a presumption or prediction on the surrounding elements on the page. Likewise, I believe that the already existing DIV containers for indentation actually belong into comment_view_multiple(), since they have nothing to do with the view mode or a rendering of a single comment at all, which of course is a different issue.
Comment #26
bleen CreditAttribution: bleen commentedEDIT: - Ignore this patch ... wrong one :(
Comment #27
bleen CreditAttribution: bleen commentedThis is the patch I meant to upload...
Comment #28
sun=== empty($comment->in_preview)
Trailing white-space.
Powered by Dreditor.
Comment #29
bleen CreditAttribution: bleen commentedhmph .. learned something new. I figured empty would throw an error.
Cool beans.
Comment #30
sunOverall, Comment module + markup + logic needs much more love, but this patch cuts it for now.
Comment #31
Dries CreditAttribution: Dries commentedCommitted to CVS HEAD. Thanks.