template_preprocess_comment(&$variables)
Prepares variables for comment templates.
Default template: comment.html.twig.
Parameters
array $variables: An associative array containing:
- elements: An associative array containing the comment and entity objects. Array keys: #comment, #commented_entity.
File
- core/modules/comment/comment.module, line 621
- Enables users to comment on published content.
Code
function template_preprocess_comment(&$variables) { /** @var \Drupal\comment\CommentInterface $comment */ $comment = $variables['elements']['#comment']; $commented_entity = $comment->getCommentedEntity(); $variables['comment'] = $comment; $variables['commented_entity'] = $commented_entity; $variables['threaded'] = $variables['elements']['#comment_threaded']; $account = $comment->getOwner(); $username = array( '#theme' => 'username', '#account' => $account, ); $variables['author'] = drupal_render($username); $variables['author_id'] = $comment->getOwnerId(); $variables['new_indicator_timestamp'] = $comment->getChangedTime(); $variables['created'] = format_date($comment->getCreatedTime()); // Avoid calling format_date() twice on the same timestamp. if ($comment->getChangedTime() == $comment->getCreatedTime()) { $variables['changed'] = $variables['created']; } else { $variables['changed'] = format_date($comment->getChangedTime()); } if (theme_get_setting('features.comment_user_picture')) { // To change user picture settings (for instance, image style), edit the // 'compact' view mode on the User entity. $variables['user_picture'] = user_view($account, 'compact'); } else { $variables['user_picture'] = array(); } if (isset($comment->in_preview)) { $variables['title'] = \Drupal::l($comment->getSubject(), new Url('<front>')); $variables['permalink'] = \Drupal::l(t('Permalink'), new Url('<front>')); } else { $uri = $comment->permalink(); $attributes = $uri->getOption('attributes') ? : array(); $attributes += array('class' => array('permalink'), 'rel' => 'bookmark'); $uri->setOption('attributes', $attributes); $variables['title'] = \Drupal::l($comment->getSubject(), $uri); $variables['permalink'] = \Drupal::l(t('Permalink'), $comment->permalink()); } $variables['submitted'] = t('Submitted by @username on @datetime', array('@username' => $variables['author'], '@datetime' => $variables['created'])); if ($comment->hasParentComment()) { // Fetch and store the parent comment information for use in templates. $comment_parent = $comment->getParentComment(); $account_parent = $comment_parent->getOwner(); $variables['parent_comment'] = $comment_parent; $username = array( '#theme' => 'username', '#account' => $account_parent, ); $variables['parent_author'] = drupal_render($username); $variables['parent_created'] = format_date($comment_parent->getCreatedTime()); // Avoid calling format_date() twice on the same timestamp. if ($comment_parent->getChangedTime() == $comment_parent->getCreatedTime()) { $variables['parent_changed'] = $variables['parent_created']; } else { $variables['parent_changed'] = format_date($comment_parent->getChangedTime()); } $permalink_uri_parent = $comment_parent->permalink(); $attributes = $permalink_uri_parent->getOption('attributes') ? : array(); $attributes += array('class' => array('permalink'), 'rel' => 'bookmark'); $permalink_uri_parent->setOption('attributes', $attributes); $variables['parent_title'] = \Drupal::l($comment_parent->getSubject(), $permalink_uri_parent); $variables['parent_permalink'] = \Drupal::l(t('Parent permalink'), $permalink_uri_parent); $variables['parent'] = t('In reply to @parent_title by @parent_username', array('@parent_username' => $variables['parent_author'], '@parent_title' => $variables['parent_title'])); } else { $variables['parent_comment'] = ''; $variables['parent_author'] = ''; $variables['parent_created'] = ''; $variables['parent_changed'] = ''; $variables['parent_title'] = ''; $variables['parent_permalink'] = ''; $variables['parent'] = ''; } // Helpful $content variable for templates. foreach (Element::children($variables['elements']) as $key) { $variables['content'][$key] = $variables['elements'][$key]; } // Set status to a string representation of comment->status. if (isset($comment->in_preview)) { $variables['status'] = 'preview'; } else { $variables['status'] = $comment->isPublished() ? 'published' : 'unpublished'; } // Add comment author user ID. Necessary for the comment-by-viewer library. $variables['attributes']['data-comment-user-id'] = $comment->getOwnerId(); }
Please login to continue.