Skip to content

Instantly share code, notes, and snippets.

@leymannx
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leymannx/6e78d817f96e5b5dddb9 to your computer and use it in GitHub Desktop.
Save leymannx/6e78d817f96e5b5dddb9 to your computer and use it in GitHub Desktop.
Drupal comment approval messages with Akamai
<?php
// When Drupal is cached by Akamai it happens sometimes that $messages ain't displayed when the page is done.
// Instead the $message get rendered only after the user visits another page, on that another page.
// So I disabled Drupal messages for anonymous users completely (theme_status_messages)
// and built the following workaround for comment approval messages.
<script id="message-queued" type="text/template">
// Inside an inline script I parked the markup plus text that normally is displayed.
// By setting type=text/template it is ignored by browsers but will be available for later display.
<div id="messages">
<div class="section clearfix">
<div class="messages status">
<h2 class="element-invisible">Status message</h2>
<?php print t('Your comment has been queued for review by site administrators and will be published after approval.'); ?>
</div>
</div>
</div>
</script>
<script type="text/javascript">
// Since Drupal always appends #comment-1234 to the URL when a comments is submitted
// I let the following function check if the URL has a certain substring
// and let it display the markup accordingly.
// This script doesn't have to be in the template. Place it where you want.
jQuery( document ).ready(function($) {
if (window.location.hash.substring(1,8) == 'comment') {
$('#messages-placeholder').before($('#message-queued').html());
}
});
</script>
<?php
// copy pasted from Drupal core, only added two lines each with a comment above
function YOURTHEME_status_messages($vars) {
$display = $vars ['display'];
$output = '';
$status_heading = array(
'status' => t('Status message'),
'error' => t('Error message'),
'warning' => t('Warning message'),
);
foreach (drupal_get_messages($display) as $type => $messages) {
// skip all messages for anonymous users
if (!user_is_logged_in() && ($type == "error" || $type == "warning" || $type == "status")) { continue; }
// skip all messages for users who don't have the 'administer nodes' permission
else if (!user_access('administer nodes') && ($type == "error" || $type == "warning" || $type == "status")) { continue; }
$output .= "<div class=\"messages $type\">\n";
if (!empty($status_heading [$type])) {
$output .= '<h2 class="element-invisible">' . $status_heading [$type] . "</h2>\n";
}
if (count($messages) > 1) {
$output .= " <ul>\n";
foreach ($messages as $message) {
$output .= ' <li>' . $message . "</li>\n";
}
$output .= " </ul>\n";
}
else {
$output .= reset($messages);
}
$output .= "</div>\n";
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment