diff --git a/core/modules/comment/comment.admin.inc b/core/modules/comment/comment.admin.inc index 488c2f9..2d5814e 100644 --- a/core/modules/comment/comment.admin.inc +++ b/core/modules/comment/comment.admin.inc @@ -39,7 +39,6 @@ function comment_admin($type = 'new') { * @see comment_admin() * @see comment_admin_overview_validate() * @see comment_admin_overview_submit() - * @see theme_comment_admin_overview() */ function comment_admin_overview($form, &$form_state, $arg) { // Build an 'Update options' form. @@ -119,7 +118,12 @@ 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( + 'data' => array( + '#theme' => 'username', + '#account' => comment_prepare_author($comment) + ), + ), 'posted_in' => array( 'data' => array( '#type' => 'link', diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index b2d0190..22df7d9 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -176,18 +176,13 @@ function comment_field_extra_fields() { function comment_theme() { return array( 'comment_block' => array( + 'template' => 'comment-block', 'variables' => array('number' => NULL), ), - 'comment_preview' => array( - 'variables' => array('comment' => NULL), - ), 'comment' => array( 'template' => 'comment', 'render element' => 'elements', ), - 'comment_post_forbidden' => array( - 'variables' => array('node' => NULL), - ), 'comment_wrapper' => array( 'template' => 'comment-wrapper', 'render element' => 'content', @@ -539,26 +534,6 @@ function comment_new_page_count($num_comments, $new_replies, EntityInterface $no } /** - * Returns HTML for a list of recent comments. - * - * @ingroup themeable - */ -function theme_comment_block($variables) { - $items = array(); - $number = $variables['number']; - foreach (comment_get_recent($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))) . ''; - } - - if ($items) { - return theme('item_list', array('items' => $items)); - } - else { - return t('No comments available.'); - } -} - -/** * Implements hook_node_view(). */ function comment_node_view(EntityInterface $node, EntityDisplay $display, $view_mode) { @@ -613,7 +588,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, ); } @@ -643,7 +618,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, ); } @@ -945,7 +920,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; } } @@ -1553,7 +1528,29 @@ function comment_prepare_author(Comment $comment) { } /** - * Prepares variables for comment templates. + * Prepares variables for comment block templates. + * + * Default template: comment-block.html.twig. + * + * @param array $variables + * An associative array containing: + * - number: The number of recent comments to display. + */ +function template_preprocess_comment_block(&$variables) { + $items = array(); + 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))) . ''; + } + if (!empty($items)) { + $variables['comments'] = array( + '#theme' => 'item_list', + '#items' => $items, + ); + } +} + +/** + * Preprocesses variables for comment.tpl.php. * * Default template: comment.html.twig. * @@ -1679,50 +1676,48 @@ function template_preprocess_comment(&$variables) { } /** - * Returns HTML for a "you can't post comments" notice. + * Provides a message if posting comments is forbidden. * - * @param $variables - * An associative array containing: - * - node: The comment node. + * 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. * - * @ingroup themeable + * @param \Drupal\Core\Entity\EntityInterface $node + * The node the comment is attached to. */ -function theme_comment_post_forbidden($variables) { - $node = $variables['node']; - global $user; +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. - $authenticated_post_comments = &drupal_static(__FUNCTION__, NULL); - - if (!$user->uid) { - if (!isset($authenticated_post_comments)) { - // We only output a link if we are certain that users will get permission - // to post comments by logging in. - $comment_roles = user_roles(TRUE, 'post comments'); - $authenticated_post_comments = isset($comment_roles[DRUPAL_AUTHENTICATED_RID]); - } - - if ($authenticated_post_comments) { - // 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"); - } + $auth_post_comments = &drupal_static(__FUNCTION__, NULL); + if (!isset($auth_post_comments)) { + $comment_roles = user_roles(TRUE, 'post comments'); + $auth_post_comments = isset($comment_roles[DRUPAL_AUTHENTICATED_RID]); + } - if (config('user.settings')->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) { - // Users can register themselves. - return t('Log in or register to post comments', array('@login' => url('user/login', array('query' => $destination)), '@register' => url('user/register', array('query' => $destination)))); - } - else { - // Only admins can add new users, no public registration. - return t('Log in to post comments', array('@login' => url('user/login', array('query' => $destination)))); - } + 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 { + $message = ''; + } + + return $message; } /** diff --git a/core/modules/comment/lib/Drupal/comment/CommentFormController.php b/core/modules/comment/lib/Drupal/comment/CommentFormController.php index 179410c..1ce51be 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentFormController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentFormController.php @@ -91,7 +91,8 @@ public function form(array $form, array &$form_state) { 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']['#theme'] = 'username'; + $form['author']['name']['#account'] = $user; } // Add author e-mail and homepage fields depending on the current user. diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php index 5585650..d3eba8a 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php @@ -260,7 +260,7 @@ function assertCommentLinks(array $info) { // Anonymous users should see a note to log in or register in case // authenticated users are allowed to post comments. - // @see theme_comment_post_forbidden() + // @see comment-post-forbidden.html.twig if (!$this->loggedInUser) { if (user_access('post comments', $this->web_user)) { // The note depends on whether users are actually able to register. diff --git a/core/modules/comment/templates/comment-block.html.twig b/core/modules/comment/templates/comment-block.html.twig new file mode 100644 index 0000000..2ec3d00 --- /dev/null +++ b/core/modules/comment/templates/comment-block.html.twig @@ -0,0 +1,19 @@ +{# +/** + * @file + * Default theme implementation for a list of recent comments. + * + * Available variables: + * - comments: A list of recent comments. + * + * @see template_preprocess() + * @see template_preprocess_comment_block() + * + * @ingroup themeable + */ +#} +{% if not comments %} + {{ 'No comments available.'|t }} +{% else %} + {{ comments }} +{% endif %}