diff --git a/core/modules/comment/comment.admin.inc b/core/modules/comment/comment.admin.inc index 6d60e90..7b00a3c 100644 --- a/core/modules/comment/comment.admin.inc +++ b/core/modules/comment/comment.admin.inc @@ -118,7 +118,10 @@ function comment_admin_overview($form, &$form_state, $arg) { '#options' => array('attributes' => array('title' => truncate_utf8($comment->comment_body->value, 128)), 'fragment' => 'comment-' . $comment->id()), ), ), - 'author' => theme('username', array('account' => comment_prepare_author($comment))), + 'author' => array( + '#theme' => 'username', + '#account' => comment_prepare_author($comment) + ), 'posted_in' => array( 'data' => array( '#type' => 'link', diff --git a/core/modules/comment/comment.api.php b/core/modules/comment/comment.api.php index c678e87..a27899e 100644 --- a/core/modules/comment/comment.api.php +++ b/core/modules/comment/comment.api.php @@ -113,8 +113,7 @@ function hook_comment_view(\Drupal\comment\Plugin\Core\Entity\Comment $comment, * If the module wishes to act on the rendered HTML of the comment rather than * the structured content array, it may use this hook to add a #post_render * callback. Alternatively, it could also implement hook_preprocess_HOOK() for - * comment.html.twig. See drupal_render() and theme() documentation respectively - * for details. + * comment.html.twig. See drupal_render() documentation for details. * * @param $build * A renderable array representing the comment. diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 143523c..d6ab57f 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -201,16 +201,12 @@ function comment_theme() { 'variables' => array('number' => NULL), ), 'comment' => array( - 'template' => 'comment', 'render element' => 'elements', - ), - 'comment_post_forbidden' => array( - 'template' => 'comment-post-forbidden', - 'variables' => array('node' => NULL), + 'template' => 'comment', ), 'comment_wrapper' => array( - 'template' => 'comment-wrapper', 'render element' => 'content', + 'template' => 'comment-wrapper', ), ); } @@ -607,7 +603,7 @@ function comment_node_view(EntityInterface $node, EntityDisplay $display, $view_ } else { $links['comment-forbidden'] = array( - 'title' => theme('comment_post_forbidden', array('node' => $node)), + 'title' => comment_forbidden_message($node), 'html' => TRUE, ); } @@ -637,7 +633,7 @@ function comment_node_view(EntityInterface $node, EntityDisplay $display, $view_ } else { $links['comment-forbidden'] = array( - 'title' => theme('comment_post_forbidden', array('node' => $node)), + 'title' => comment_forbidden_message($node), 'html' => TRUE, ); } @@ -939,7 +935,7 @@ function comment_links(Comment $comment, EntityInterface $node) { ); } if (empty($links)) { - $links['comment-forbidden']['title'] = theme('comment_post_forbidden', array('node' => $node)); + $links['comment-forbidden']['title'] = comment_forbidden_message($node); $links['comment-forbidden']['html'] = TRUE; } } @@ -1575,11 +1571,12 @@ function template_preprocess_comment_block(&$variables) { foreach (comment_get_recent($variables['number']) as $comment) { $items[] = l($comment->subject, 'comment/' . $comment->cid, array('fragment' => 'comment-' . $comment->cid)) . ' ' . t('@time ago', array('@time' => format_interval(REQUEST_TIME - $comment->changed))) . ''; } - - $variables['comments'] = array( - '#theme' => 'item_list', - '#items' => $items, - ); + if (!empty($items)) { + $variables['comments'] = array( + '#theme' => 'item_list', + '#items' => $items, + ); + } } /** @@ -1599,7 +1596,10 @@ function template_preprocess_comment(&$variables) { $variables['node'] = $node; $account = comment_prepare_author($comment); - $variables['author'] = theme('username', array('account' => $account)); + $variables['author'] = array( + '#theme' => 'username', + '#account' => $account + ); $variables['new'] = $comment->new->value ? t('new') : ''; $variables['created'] = format_date($comment->created->value); // Avoid calling format_date() twice on the same timestamp. @@ -1636,7 +1636,10 @@ function template_preprocess_comment(&$variables) { $comment_parent = $comment->pid->entity; $account_parent = comment_prepare_author($comment); $variables['parent_comment'] = $comment_parent; - $variables['parent_author'] = theme('username', array('account' => $account_parent)); + $variables['parent_author'] = = array( + '#theme' => 'username', + '#account' => $account_parent + ); $variables['parent_created'] = format_date($comment_parent->created->value); // Avoid calling format_date() twice on the same timestamp. if ($comment_parent->changed->value == $comment_parent->created->value) { @@ -1697,50 +1700,53 @@ function template_preprocess_comment(&$variables) { $variables['attributes']['class'][] = 'by-viewer'; } } + + $variables['content_attributes']['class'][] = 'content'; } /** - * Prepares variables for comment forbidden notice templates. + * Provides a message if posting comments is forbidden. * - * Default template: comment-post-forbidden.html.twig. + * If authenticated users can post comments, a message is returned that prompts + * the anonymous user to log in (or register, if applicable) that redirects to + * node comment form. Otherwise, no message is returned. * - * @param array $variables - * An associative array containing: - * - node: The comment node. + * @param \Drupal\Core\Entity\EntityInterface $node + * The node the comment is attached to. */ -function template_preprocess_comment_post_forbidden(&$variables) { - global $user; - $node = $variables['node']; - - // Whether the user viewing the site is logged out. - $variables['logged_out'] = (bool) (!$user->uid); +function comment_forbidden_message(EntityInterface $node) { // Whether the user will be able to post comments once logged in. // Since this is expensive to compute, we cache it so that a page with many // comments only has to query the database once for all the links. - $variables['can_post_comments'] = &drupal_static(__FUNCTION__, NULL); - if (!isset($variables['can_post_comments'])) { + $auth_post_comments = &drupal_static(__FUNCTION__, NULL); + if (!isset($auth_post_comments)) { $comment_roles = user_roles(TRUE, 'post comments'); - $variables['can_post_comments'] = isset($comment_roles[DRUPAL_AUTHENTICATED_RID]); + $auth_post_comments = isset($comment_roles[DRUPAL_AUTHENTICATED_RID]); } - // Whether a user is allowed to register for the site. - $variables['user_register'] = (config('user.settings')->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY); - - // Build login and register links. - // We cannot use drupal_get_destination() because these links - // sometimes appear on /node and taxonomy listing pages. - if (variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BELOW) == COMMENT_FORM_SEPARATE_PAGE) { - $destination = array('destination' => "comment/reply/$node->nid#comment-form"); + if (!user_is_logged_in() && $auth_post_comments) { + // Build login and register links. We cannot use drupal_get_destination() + // because these links sometimes appear on /node and taxonomy listing pages. + if (variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BELOW) == COMMENT_FORM_SEPARATE_PAGE) { + $destination = array('destination' => "comment/reply/$node->nid#comment-form"); + } + else { + $destination = array('destination' => "node/$node->nid#comment-form"); + } + // Message depends on whether users may register accounts. + if (config('user.settings')->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) { + $message = t('Log in or register to post comments', array('@login' => url('user/login', array('query' => $destination)), '@register' => url('user/register', array('query' => $destination)))); + } + else { + $message = t('Log in to post comments', array('@login' => url('user/login', array('query' => $destination)))); + } } else { - $destination = array('destination' => "node/$node->nid#comment-form"); + $message = ''; } - // The URL where a user may log in. - $variables['user_login_url'] = url('user/login', array('query' => $destination)); - // The URL where a user may register. - $variables['user_register_url'] = url('user/register', array('query' => $destination)); + return $message; } /** diff --git a/core/modules/comment/lib/Drupal/comment/CommentFormController.php b/core/modules/comment/lib/Drupal/comment/CommentFormController.php index a5e5f15..fbb47cb 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentFormController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentFormController.php @@ -91,7 +91,10 @@ public function form(array $form, array &$form_state, EntityInterface $comment) elseif ($user->uid) { $form['author']['name']['#type'] = 'item'; $form['author']['name']['#value'] = $form['author']['name']['#default_value']; - $form['author']['name']['#markup'] = theme('username', array('account' => $user)); + $form['author']['name']['#markup'] = array( + '#theme' => 'username', + '#account' => $user + ); } // Add author e-mail and homepage fields depending on the current user. diff --git a/core/modules/comment/templates/comment-block.html.twig b/core/modules/comment/templates/comment-block.html.twig index a5d0307..2ec3d00 100644 --- a/core/modules/comment/templates/comment-block.html.twig +++ b/core/modules/comment/templates/comment-block.html.twig @@ -12,8 +12,8 @@ * @ingroup themeable */ #} -{{ comments }} - {% if not comments %} {{ 'No comments available.'|t }} +{% else %} + {{ comments }} {% endif %} diff --git a/core/modules/comment/templates/comment-post-forbidden.html.twig b/core/modules/comment/templates/comment-post-forbidden.html.twig deleted file mode 100644 index 468290b..0000000 --- a/core/modules/comment/templates/comment-post-forbidden.html.twig +++ /dev/null @@ -1,32 +0,0 @@ -{# -/** - * @file - * Default theme implementation for comment forbidden notice templates. - * - * Available variables: - * - logged_out: Whether the user viewing the site is logged out. - * - can_post_comments: Whether authenticated users can post comments once - * logged in. - * - user_register: Whether a user is allowed to register for the site. - * - user_login_url: The URL where a user may log in. - * - user_register_url: The URL where a user may register. - * - * @see template_preprocess() - * @see template_preprocess_comment_post_forbidden() - * - * @ingroup themable - */ -#} -{# - We only output a link if the current user is logged out and we are certain - that users will get permission to post comments by logging in. - - @todo Consider moving this decision out of the template. -#} -{% if logged_out and can_post_comments %} - {% if user_register %} - {{ 'Log in or register to post comments'|t({'@login': user_login_url, '@register': user_register_url}) }} - {% else %} - {{ 'Log in to post comments'|t({'@login': user_login_url}) }} - {% endif %} -{% endif %} diff --git a/core/modules/comment/templates/comment.html.twig b/core/modules/comment/templates/comment.html.twig index 838a0e4..a9c6b7d 100644 --- a/core/modules/comment/templates/comment.html.twig +++ b/core/modules/comment/templates/comment.html.twig @@ -93,7 +93,7 @@ {{ permalink }} -