Commit f3732c68 authored by alexpott's avatar alexpott
Browse files

Issue #2028025 by David Hernández, Berdir, larowlan, ceng, YesCT, a_thakur:...

Issue #2028025 by David Hernández, Berdir, larowlan, ceng, YesCT, a_thakur: Expand CommentInterface to provide methods.
parent 0b2c622b
......@@ -24,7 +24,7 @@
*/
function hook_comment_presave(Drupal\comment\Comment $comment) {
// Remove leading & trailing spaces from the comment subject.
$comment->subject->value = trim($comment->subject->value);
$comment->setSubject(trim($comment->getSubject()));
}
/**
......@@ -35,8 +35,8 @@ function hook_comment_presave(Drupal\comment\Comment $comment) {
*/
function hook_comment_insert(Drupal\comment\Comment $comment) {
// Reindex the node when comments are added.
if ($comment->entity_type->value == 'node') {
node_reindex_node_search($comment->entity_id->value);
if ($comment->getCommentedEntityTypeId() == 'node') {
node_reindex_node_search($comment->getCommentedEntityId());
}
}
......@@ -48,8 +48,8 @@ function hook_comment_insert(Drupal\comment\Comment $comment) {
*/
function hook_comment_update(Drupal\comment\Comment $comment) {
// Reindex the node when comments are updated.
if ($comment->entity_type->value == 'node') {
node_reindex_node_search($comment->entity_id->value);
if ($comment->getCommentedEntityTypeId() == 'node') {
node_reindex_node_search($comment->getCommentedEntityId());
}
}
......@@ -149,7 +149,7 @@ function hook_comment_view_alter(&$build, \Drupal\comment\Entity\Comment $commen
* The comment the action is being performed on.
*/
function hook_comment_publish(Drupal\comment\Comment $comment) {
drupal_set_message(t('Comment: @subject has been published', array('@subject' => $comment->subject->value)));
drupal_set_message(t('Comment: @subject has been published', array('@subject' => $comment->getSubject())));
}
/**
......@@ -159,7 +159,7 @@ function hook_comment_publish(Drupal\comment\Comment $comment) {
* The comment the action is being performed on.
*/
function hook_comment_unpublish(Drupal\comment\Comment $comment) {
drupal_set_message(t('Comment: @subject has been unpublished', array('@subject' => $comment->subject->value)));
drupal_set_message(t('Comment: @subject has been unpublished', array('@subject' => $comment->getSubject())));
}
/**
......@@ -194,7 +194,7 @@ function hook_comment_predelete(Drupal\comment\Comment $comment) {
* @see entity_delete_multiple()
*/
function hook_comment_delete(Drupal\comment\Comment $comment) {
drupal_set_message(t('Comment: @subject has been deleted', array('@subject' => $comment->subject->value)));
drupal_set_message(t('Comment: @subject has been deleted', array('@subject' => $comment->getSubject())));
}
/**
......
......@@ -749,7 +749,7 @@ function comment_prepare_thread(&$comments) {
foreach ($comments as $key => &$comment) {
// The $divs element instructs #prefix whether to add an indent div or
// close existing divs (a negative value).
$comment->depth = count(explode('.', $comment->thread->value)) - 1;
$comment->depth = count(explode('.', $comment->getThread())) - 1;
if ($comment->depth > $divs) {
$comment->divs = 1;
$divs++;
......@@ -935,7 +935,7 @@ function comment_entity_insert(EntityInterface $entity) {
'last_comment_timestamp',
'last_comment_name',
'last_comment_uid',
'comment_count'
'comment_count',
));
$execute_query = FALSE;
foreach ($fields as $field_name => $detail) {
......@@ -1094,7 +1094,7 @@ function comment_user_cancel($edit, $account, $method) {
case 'user_cancel_block_unpublish':
$comments = entity_load_multiple_by_properties('comment', array('uid' => $account->id()));
foreach ($comments as $comment) {
$comment->status->value = 0;
$comment->setPublished(COMMENT_NOT_PUBLISHED);
$comment->save();
}
break;
......@@ -1285,12 +1285,13 @@ function comment_get_display_page($cid, $instance) {
*/
function comment_preview(CommentInterface $comment, array &$form_state) {
$preview_build = array();
$entity = entity_load($comment->entity_type->value, $comment->entity_id->value);
$entity = $comment->getCommentedEntity();
if (!form_get_errors($form_state)) {
// Attach the user and time information.
if (!empty($comment->name->value)) {
$account = user_load_by_name($comment->name->value);
$author_name = $comment->getAuthorName();
if (!empty($author_name)) {
$account = user_load_by_name($author_name);
}
elseif (\Drupal::currentUser()->isAuthenticated() && empty($comment->is_anonymous)) {
$account = \Drupal::currentUser();
......@@ -1298,13 +1299,14 @@ function comment_preview(CommentInterface $comment, array &$form_state) {
if (!empty($account) && $account->isAuthenticated()) {
$comment->setOwner($account);
$comment->name->value = check_plain($account->getUsername());
$comment->setAuthorName(check_plain($account->getUsername()));
}
else {
$comment->name->value = \Drupal::config('user.settings')->get('anonymous');
elseif (empty($author_name)) {
$comment->setAuthorName(\Drupal::config('user.settings')->get('anonymous'));
}
$comment->created->value = !empty($comment->created->value) ? $comment->created->value : REQUEST_TIME;
$created_time = !is_null($comment->getCreatedTime()) ? $comment->getCreatedTime() : REQUEST_TIME;
$comment->setCreatedTime($created_time);
$comment->changed->value = REQUEST_TIME;
$comment->in_preview = TRUE;
$comment_build = comment_view($comment);
......@@ -1313,10 +1315,10 @@ function comment_preview(CommentInterface $comment, array &$form_state) {
$preview_build['comment_preview'] = $comment_build;
}
if ($comment->pid->target_id) {
if ($comment->hasParentComment()) {
$build = array();
$parent = $comment->pid->entity;
if ($parent && $parent->status->value == CommentInterface::PUBLISHED) {
$parent = $comment->getParentComment();
if ($parent && $parent->isPublished()) {
$build = comment_view($parent);
}
}
......@@ -1332,7 +1334,7 @@ function comment_preview(CommentInterface $comment, array &$form_state) {
// entity is rendered, it excludes the comment field output. As objects are
// always addressed by reference we ensure changes are not lost by setting
// the value back to its original state after the call to entity_view().
$field_name = $comment->field_name->value;
$field_name = $comment->getFieldName();
$original_value = $entity->get($field_name);
$entity->set($field_name, COMMENT_HIDDEN);
$build = entity_view($entity, 'full');
......@@ -1370,7 +1372,7 @@ function comment_prepare_author(CommentInterface $comment) {
// The account has been pre-loaded by CommentViewBuilder::buildContent().
$account = $comment->getOwner();
if (empty($account->uid->value)) {
$account = entity_create('user', array('uid' => 0, 'name' => $comment->name->value, 'homepage' => $comment->homepage->value));
$account = entity_create('user', array('uid' => 0, 'name' => $comment->getAuthorName(), 'homepage' => $comment->getHomepage()));
}
return $account;
}
......@@ -1388,7 +1390,7 @@ function comment_prepare_author(CommentInterface $comment) {
function template_preprocess_comment(&$variables) {
/** @var \Drupal\comment\CommentInterface $comment */
$comment = $variables['elements']['#comment'];
$commented_entity = entity_load($comment->entity_type->value, $comment->entity_id->value);
$commented_entity = $comment->getCommentedEntity();
$variables['comment'] = $comment;
$variables['commented_entity'] = $commented_entity;
......@@ -1399,14 +1401,14 @@ function template_preprocess_comment(&$variables) {
'#account' => $account,
);
$variables['author'] = drupal_render($username);
$variables['new_indicator_timestamp'] = $comment->changed->value;
$variables['created'] = format_date($comment->created->value);
$variables['new_indicator_timestamp'] = $comment->getChangedTime();
$variables['created'] = format_date($comment->getCreatedTime());
// Avoid calling format_date() twice on the same timestamp.
if ($comment->changed->value == $comment->created->value) {
if ($comment->getChangedTime() == $comment->getCreatedTime()) {
$variables['changed'] = $variables['created'];
}
else {
$variables['changed'] = format_date($comment->changed->value);
$variables['changed'] = format_date($comment->getChangedTime());
}
if (theme_get_setting('features.comment_user_picture')) {
......@@ -1425,13 +1427,13 @@ function template_preprocess_comment(&$variables) {
$variables['signature'] = '';
}
if (isset($comment->in_preview)) {
$variables['title'] = l($comment->subject->value, '');
$variables['title'] = l($comment->getSubject(), '');
$variables['permalink'] = l(t('Permalink'), '');
}
else {
$uri = $comment->urlInfo();
$uri['options'] += array('attributes' => array('class' => array('permalink'), 'rel' => 'bookmark'));
$variables['title'] = \Drupal::l($comment->subject->value, $uri['route_name'], $uri['route_parameters'], $uri['options']);
$variables['title'] = \Drupal::l($comment->getSubject(), $uri['route_name'], $uri['route_parameters'], $uri['options']);
$permalink_uri = $comment->permalink();
$variables['permalink'] = \Drupal::l(t('Permalink'), $permalink_uri['route_name'], $permalink_uri['route_parameters'], $permalink_uri['options']);
......@@ -1439,9 +1441,9 @@ function template_preprocess_comment(&$variables) {
$variables['submitted'] = t('Submitted by !username on !datetime', array('!username' => $variables['author'], '!datetime' => $variables['created']));
if ($comment->pid->target_id) {
if ($comment->hasParentComment()) {
// Fetch and store the parent comment information for use in templates.
$comment_parent = $comment->pid->entity;
$comment_parent = $comment->getParentComment();
$account_parent = comment_prepare_author($comment_parent);
$variables['parent_comment'] = $comment_parent;
// @todo Do not call theme() here. We do this for purposes of t().
......@@ -1450,17 +1452,17 @@ function template_preprocess_comment(&$variables) {
'#account' => $account_parent,
);
$variables['parent_author'] = drupal_render($username);
$variables['parent_created'] = format_date($comment_parent->created->value);
$variables['parent_created'] = format_date($comment_parent->getCreatedTime());
// Avoid calling format_date() twice on the same timestamp.
if ($comment_parent->changed->value == $comment_parent->created->value) {
if ($comment_parent->getChangedTime() == $comment_parent->getCreatedTime()) {
$variables['parent_changed'] = $variables['parent_created'];
}
else {
$variables['parent_changed'] = format_date($comment_parent->changed->value);
$variables['parent_changed'] = format_date($comment_parent->getChangedTime());
}
$permalink_uri_parent = $comment_parent->permalink();
$permalink_uri_parent['options'] += array('attributes' => array('class' => array('permalink'), 'rel' => 'bookmark'));
$variables['parent_title'] = \Drupal::l($comment_parent->subject->value, $permalink_uri_parent['route_name'], $permalink_uri_parent['route_parameters'], $permalink_uri_parent['options']);
$variables['parent_title'] = \Drupal::l($comment_parent->getSubject(), $permalink_uri_parent['route_name'], $permalink_uri_parent['route_parameters'], $permalink_uri_parent['options']);
$variables['parent_permalink'] = \Drupal::l(t('Parent permalink'), $permalink_uri_parent['route_name'], $permalink_uri_parent['route_parameters'], $permalink_uri_parent['options']);
$variables['parent'] = t('In reply to !parent_title by !parent_username',
array('!parent_username' => $variables['parent_author'], '!parent_title' => $variables['parent_title']));
......@@ -1485,7 +1487,7 @@ function template_preprocess_comment(&$variables) {
$variables['status'] = 'preview';
}
else {
$variables['status'] = ($comment->status->value == CommentInterface::NOT_PUBLISHED) ? 'unpublished' : 'published';
$variables['status'] = $comment->isPublished() ? 'published' : 'unpublished';
}
// Gather comment classes.
......@@ -1573,9 +1575,9 @@ function comment_ranking() {
* Implements hook_file_download_access().
*/
function comment_file_download_access($field, EntityInterface $entity, FileInterface $file) {
if ($entity->getEntityTypeId() == 'comment') {
if (user_access('access comments') && $entity->status->value == CommentInterface::PUBLISHED || user_access('administer comments')) {
$commented_entity = entity_load($entity->entity_type->value, $entity->entity_id->value);
if ($entity instanceof CommentInterface) {
if (user_access('access comments') && $entity->isPublished() || user_access('administer comments')) {
$commented_entity = $entity->getCommentedEntity();
// Check access to parent entity.
return $commented_entity->access('view');
}
......
......@@ -137,32 +137,27 @@ function comment_tokens($type, $tokens, array $data = array(), array $options =
$replacements[$original] = $comment->id();
break;
// Poster identity information for comments
// Poster identity information for comments.
case 'hostname':
$replacements[$original] = $sanitize ? check_plain($comment->hostname->value) : $comment->hostname->value;
$replacements[$original] = $sanitize ? check_plain($comment->getHostname()) : $comment->getHostname();
break;
case 'name':
$name = ($comment->getOwnerId() == 0) ? \Drupal::config('user.settings')->get('anonymous') : $comment->name->value;
$name = ($comment->getOwnerId() == 0) ? \Drupal::config('user.settings')->get('anonymous') : $comment->getAuthorName();
$replacements[$original] = $sanitize ? filter_xss($name) : $name;
break;
case 'mail':
if ($comment->getOwnerId()) {
$mail = $comment->getOwner()->getEmail();
}
else {
$mail = $comment->mail->value;
}
$mail = $comment->getAuthorEmail();
$replacements[$original] = $sanitize ? check_plain($mail) : $mail;
break;
case 'homepage':
$replacements[$original] = $sanitize ? check_url($comment->homepage->value) : $comment->homepage->value;
$replacements[$original] = $sanitize ? check_url($comment->getHomepage()) : $comment->getHomepage();
break;
case 'title':
$replacements[$original] = $sanitize ? filter_xss($comment->subject->value) : $comment->subject->value;
$replacements[$original] = $sanitize ? filter_xss($comment->getSubject()) : $comment->getSubject();
break;
case 'body':
......@@ -180,28 +175,31 @@ function comment_tokens($type, $tokens, array $data = array(), array $options =
$replacements[$original] = url('comment/' . $comment->id() . '/edit', $url_options);
break;
// Default values for the chained tokens handled below.
// @todo Remove 'name' token in favour of 'author'. See
// https://drupal.org/node/920056.
case 'name':
case 'author':
$replacements[$original] = $sanitize ? filter_xss($comment->name->value) : $comment->name->value;
$name = $comment->getAuthorName();
$replacements[$original] = $sanitize ? filter_xss($name) : $name;
break;
case 'parent':
if (!empty($comment->pid->target_id)) {
$parent = entity_load('comment', $comment->pid->target_id);
$replacements[$original] = $sanitize ? filter_xss($parent->subject->value) : $parent->subject->value;
if ($comment->hasParentComment()) {
$parent = $comment->getParentComment();
$replacements[$original] = $sanitize ? filter_xss($parent->getSubject()) : $parent->getSubject();
}
break;
case 'created':
$replacements[$original] = format_date($comment->created->value, 'medium', '', NULL, $langcode);
$replacements[$original] = format_date($comment->getCreatedTime(), 'medium', '', NULL, $langcode);
break;
case 'changed':
$replacements[$original] = format_date($comment->changed->value, 'medium', '', NULL, $langcode);
$replacements[$original] = format_date($comment->getChangedTime(), 'medium', '', NULL, $langcode);
break;
case 'entity':
$entity = entity_load($comment->entity_type->value, $comment->entity_id->value);
$entity = $comment->getCommentedEntity();
$title = $entity->label();
$replacements[$original] = $sanitize ? filter_xss($title) : $title;
break;
......@@ -210,8 +208,8 @@ function comment_tokens($type, $tokens, array $data = array(), array $options =
// Support legacy comment node tokens, since tokes are embedded in
// user data and can't be upgraded directly.
// @todo Remove in Drupal 9, see https://drupal.org/node/2031901.
if ($comment->entity_type->value == 'node') {
$entity = entity_load($comment->entity_type->value, $comment->entity_id->value);
if ($comment->getCommentedEntityTypeId() == 'node') {
$entity = $comment->getCommentedEntity();
$title = $entity->label();
$replacements[$original] = $sanitize ? filter_xss($title) : $title;
}
......@@ -224,24 +222,24 @@ function comment_tokens($type, $tokens, array $data = array(), array $options =
// Chained token relationships.
if ($entity_tokens = $token_service->findwithPrefix($tokens, 'entity')) {
$entity = entity_load($comment->entity_type->value, $comment->entity_id->value);
$replacements += $token_service->generate($comment->entity_type->value, $entity_tokens, array($comment->entity_type->value => $entity), $options);
$entity = $comment->getCommentedEntity();
$replacements += $token_service->generate($comment->getCommentedEntityTypeId(), $entity_tokens, array($comment->getCommentedEntityTypeId() => $entity), $options);
}
if (($node_tokens = $token_service->findwithPrefix($tokens, 'node')) && $comment->entity_type->value == 'node') {
$node = entity_load($comment->entity_type->value, $comment->entity_id->value);
if (($node_tokens = $token_service->findwithPrefix($tokens, 'node')) && $comment->getCommentedEntityTypeId() == 'node') {
$node = $comment->getCommentedEntity();
$replacements += $token_service->generate('node', $node_tokens, array('node' => $node), $options);
}
if ($date_tokens = $token_service->findwithPrefix($tokens, 'created')) {
$replacements += $token_service->generate('date', $date_tokens, array('date' => $comment->created->value), $options);
$replacements += $token_service->generate('date', $date_tokens, array('date' => $comment->getCreatedTime()), $options);
}
if ($date_tokens = $token_service->findwithPrefix($tokens, 'changed')) {
$replacements += $token_service->generate('date', $date_tokens, array('date' => $comment->changed->value), $options);
$replacements += $token_service->generate('date', $date_tokens, array('date' => $comment->getChangedTime()), $options);
}
if (($parent_tokens = $token_service->findwithPrefix($tokens, 'parent')) && $parent = $comment->pid->entity) {
if (($parent_tokens = $token_service->findwithPrefix($tokens, 'parent')) && $parent = $comment->getParentComment()) {
$replacements += $token_service->generate('comment', $parent_tokens, array('comment' => $parent), $options);
}
......@@ -255,7 +253,7 @@ function comment_tokens($type, $tokens, array $data = array(), array $options =
$entity = !empty($data['entity']) ? $data['entity'] : $data['node'];
foreach ($tokens as $name => $original) {
switch($name) {
switch ($name) {
case 'comment-count':
$count = 0;
$fields = array_keys(\Drupal::service('comment.manager')->getFields($entity->getEntityTypeId()));
......
......@@ -29,7 +29,7 @@ public function getValue() {
// Field id is of the form {entity_type}__{field_name}. We set the
// optional limit param to explode() in case the user adds a field with __
// in the name.
$parts = explode('__', $entity->field_id->value, 2);
$parts = explode('__', $entity->getFieldId(), 2);
if ($parts && count($parts) == 2) {
$this->value = end($parts);
}
......@@ -46,7 +46,7 @@ public function setValue($value, $notify = TRUE) {
// Also set the field id.
$field = $this->parent->getParent();
$entity = $field->getParent();
$entity->field_id = $entity->entity_type->value . '__' . $value;
$entity->field_id = $entity->getCommentedEntityTypeId() . '__' . $value;
}
}
......
......@@ -86,8 +86,8 @@ protected function init(array &$form_state) {
public function form(array $form, array &$form_state) {
/** @var \Drupal\comment\CommentInterface $comment */
$comment = $this->entity;
$entity = $this->entityManager->getStorageController($comment->entity_type->value)->load($comment->entity_id->value);
$field_name = $comment->field_name->value;
$entity = $this->entityManager->getStorageController($comment->getCommentedEntityTypeId())->load($comment->getCommentedEntityId());
$field_name = $comment->getFieldName();
$instance = $this->fieldInfo->getInstance($entity->getEntityTypeId(), $entity->bundle(), $field_name);
// Use #comment-form as unique jump target, regardless of entity type.
......@@ -104,7 +104,7 @@ public function form(array $form, array &$form_state) {
// If not replying to a comment, use our dedicated page callback for new
// Comments on entities.
if (!$comment->id() && empty($comment->pid->target_id)) {
if (!$comment->id() && !$comment->hasParentComment()) {
$form['#action'] = url('comment/reply/' . $entity->getEntityTypeId() . '/' . $entity->id() . '/' . $field_name);
}
......@@ -124,11 +124,11 @@ public function form(array $form, array &$form_state) {
// Prepare default values for form elements.
if ($is_admin) {
$author = $comment->name->value;
$status = (isset($comment->status->value) ? $comment->status->value : CommentInterface::NOT_PUBLISHED);
$author = $comment->getAuthorName();
$status = $comment->isPublished();
if (empty($form_state['comment_preview'])) {
$form['#title'] = $this->t('Edit comment %title', array(
'%title' => $comment->subject->value,
'%title' => $comment->getSubject(),
));
}
}
......@@ -137,14 +137,14 @@ public function form(array $form, array &$form_state) {
$author = $this->currentUser->getUsername();
}
else {
$author = ($comment->name->value ? $comment->name->value : '');
$author = ($comment->getAuthorName() ? $comment->getAuthorName() : '');
}
$status = ($this->currentUser->hasPermission('skip comment approval') ? CommentInterface::PUBLISHED : CommentInterface::NOT_PUBLISHED);
}
$date = '';
if ($comment->id()) {
$date = !empty($comment->date) ? $comment->date : DrupalDateTime::createFromTimestamp($comment->created->value);
$date = !empty($comment->date) ? $comment->date : DrupalDateTime::createFromTimestamp($comment->getCreatedTime());
}
// Add the author name field depending on the current user.
......@@ -172,7 +172,7 @@ public function form(array $form, array &$form_state) {
$form['author']['mail'] = array(
'#type' => 'email',
'#title' => $this->t('E-mail'),
'#default_value' => $comment->mail->value,
'#default_value' => $comment->getAuthorEmail(),
'#required' => ($this->currentUser->isAnonymous() && $anonymous_contact == COMMENT_ANONYMOUS_MUST_CONTACT),
'#maxlength' => 64,
'#size' => 30,
......@@ -183,7 +183,7 @@ public function form(array $form, array &$form_state) {
$form['author']['homepage'] = array(
'#type' => 'url',
'#title' => $this->t('Homepage'),
'#default_value' => $comment->homepage->value,
'#default_value' => $comment->getHomepage(),
'#maxlength' => 255,
'#size' => 30,
'#access' => $is_admin || ($this->currentUser->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT),
......@@ -213,7 +213,7 @@ public function form(array $form, array &$form_state) {
'#type' => 'textfield',
'#title' => $this->t('Subject'),
'#maxlength' => 64,
'#default_value' => $comment->subject->value,
'#default_value' => $comment->getSubject(),
'#access' => $instance->getSetting('subject'),
);
......@@ -238,9 +238,10 @@ public function form(array $form, array &$form_state) {
*/
protected function actions(array $form, array &$form_state) {
$element = parent::actions($form, $form_state);
/* @var \Drupal\comment\CommentInterface $comment */
$comment = $this->entity;
$entity = $this->entityManager->getStorageController($comment->entity_type->value)->load($comment->entity_id->value);
$instance = $this->fieldInfo->getInstance($comment->entity_type->value, $entity->bundle(), $comment->field_name->value);
$entity = $comment->getCommentedEntity();
$instance = $this->fieldInfo->getInstance($comment->getCommentedEntityTypeId(), $entity->bundle(), $comment->getFieldName());
$preview_mode = $instance->getSetting('preview');
// No delete action on the comment form.
......@@ -308,10 +309,10 @@ public function validate(array $form, array &$form_state) {
public function buildEntity(array $form, array &$form_state) {
$comment = parent::buildEntity($form, $form_state);
if (!empty($form_state['values']['date']) && $form_state['values']['date'] instanceOf DrupalDateTime) {
$comment->created->value = $form_state['values']['date']->getTimestamp();
$comment->setCreatedTime($form_state['values']['date']->getTimestamp());
}
else {
$comment->created->value = REQUEST_TIME;
$comment->setCreatedTime(REQUEST_TIME);
}
$comment->changed->value = REQUEST_TIME;
return $comment;
......@@ -327,28 +328,29 @@ public function submit(array $form, array &$form_state) {
// If the comment was posted by a registered user, assign the author's ID.
// @todo Too fragile. Should be prepared and stored in comment_form()
// already.
if (!$comment->is_anonymous && !empty($comment->name->value) && ($account = user_load_by_name($comment->name->value))) {
$author_name = $comment->getAuthorName();
if (!$comment->is_anonymous && !empty($author_name) && ($account = user_load_by_name($author_name))) {
$comment->setOwner($account);
}
// If the comment was posted by an anonymous user and no author name was
// required, use "Anonymous" by default.
if ($comment->is_anonymous && (!isset($comment->name->value) || $comment->name->value === '')) {
$comment->name->value = $this->config('user.settings')->get('anonymous');
if ($comment->is_anonymous && (!isset($author_name) || $author_name === '')) {
$comment->setAuthorName($this->config('user.settings')->get('anonymous'));
}
// Validate the comment's subject. If not specified, extract from comment
// body.
if (trim($comment->subject->value) == '') {
if (trim($comment->getSubject()) == '') {
// The body may be in any format, so:
// 1) Filter it into HTML
// 2) Strip out all HTML tags
// 3) Convert entities back to plain-text.
$comment_text = $comment->comment_body->processed;
$comment->subject = Unicode::truncate(trim(String::decodeEntities(strip_tags($comment_text))), 29, TRUE);
$comment->setSubject(Unicode::truncate(trim(String::decodeEntities(strip_tags($comment_text))), 29, TRUE));
// Edge cases where the comment body is populated only by HTML tags will
// require a default subject.
if ($comment->subject->value == '') {
$comment->subject->value = $this->t('(No subject)');
if ($comment->getSubject() == '') {
$comment->setSubject($this->t('(No subject)'));
}
}
......@@ -376,7 +378,7 @@ public function preview(array &$form, array &$form_state) {
public function save(array $form, array &$form_state) {
$entity = entity_load($form_state['values']['entity_type'], $form_state['values']['entity_id']);
$comment = $this->entity;
$field_name = $comment->field_name->value;
$field_name = $comment->getFieldName();
$uri = $entity->urlInfo();
if ($this->currentUser->hasPermission('post comments') && ($this->currentUser->hasPermission('administer comments') || $entity->{$field_name}->status == COMMENT_OPEN)) {
......@@ -389,10 +391,10 @@ public function save(array $form, array &$form_state) {
$form_state['values']['cid'] = $comment->id();
// Add an entry to the watchdog log.
watchdog('content', 'Comment posted: %subject.', array('%subject' => $comment->subject->value), WATCHDOG_NOTICE, l(t('view'), 'comment/' . $comment->id(), array('fragment' => 'comment-' . $comment->id())));
watchdog('content', 'Comment posted: %subject.', array('%subject' => $comment->getSubject()), WATCHDOG_NOTICE, l(t('view'), 'comment/' . $comment->id(), array('fragment' => 'comment-' . $comment->id())));
// Explain the approval queue if necessary.
if ($comment->status->value == CommentInterface::NOT_PUBLISHED) {
if (!$comment->isPublished()) {
if (!$this->currentUser->hasPermission('administer comments')) {
drupal_set_message($this->t('Your comment has been queued for review by site administrators and will be published after approval.'));
}
......@@ -411,8 +413,8 @@ public function save(array $form, array &$form_state) {
$uri['options'] += array('query' => $query, 'fragment' => 'comment-' . $comment->id());
}
else {
watchdog('content', 'Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' => $comment->subject->value), WATCHDOG_WARNING);
drupal_set_message($this->t('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' => $comment->subject->value)), 'error');
watchdog('content', 'Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' => $comment->getSubject()), WATCHDOG_WARNING);
drupal_set_message($this->t('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' => $comment->getSubject())), 'error');