Commit 5d05703a authored by catch's avatar catch

Issue #2182055 by dawehner, andypost, juampy, linclark: Comment module causes...

Issue #2182055 by dawehner, andypost, juampy, linclark: Comment module causes Circular reference error on a REST request.
parent 41902d3d
......@@ -465,7 +465,7 @@ function comment_node_links_alter(array &$node_links, NodeInterface $node, array
$links['comment-add'] += $node->urlInfo()->toArray();
}
}
else {
elseif (\Drupal::currentUser()->isAnonymous()) {
$links['comment-forbidden'] = array(
'title' => \Drupal::service('comment.manager')->forbiddenMessage($node, $field_name),
'html' => TRUE,
......@@ -500,7 +500,7 @@ function comment_node_links_alter(array &$node_links, NodeInterface $node, array
}
}
}
else {
elseif (\Drupal::currentUser()->isAnonymous()) {
$links['comment-forbidden'] = array(
'title' => \Drupal::service('comment.manager')->forbiddenMessage($node, $field_name),
'html' => TRUE,
......@@ -844,6 +844,10 @@ function comment_translation_configuration_element_submit($form, &$form_state) {
* @see \Drupal\comment\Plugin\Field\FieldType\CommentItem::propertyDefinitions()
*/
function comment_entity_load($entities, $entity_type) {
// Comments can only be attached to content entities, so skip others.
if (!\Drupal::entityManager()->getDefinition($entity_type)->isSubclassOf('Drupal\Core\Entity\ContentEntityInterface')) {
return;
}
if (!\Drupal::service('comment.manager')->getFields($entity_type)) {
// Do not query database when entity has no comment fields.
return;
......
......@@ -7,7 +7,7 @@ services:
comment.manager:
class: Drupal\comment\CommentManager
arguments: ['@field.info', '@entity.manager', '@current_user', '@config.factory', '@string_translation', '@url_generator']
arguments: ['@field.info', '@entity.manager', '@config.factory', '@string_translation', '@url_generator']
comment.statistics:
class: Drupal\comment\CommentStatistics
......
......@@ -36,13 +36,6 @@ class CommentManager implements CommentManagerInterface {
*/
protected $entityManager;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface $current_user
*/
protected $currentUser;
/**
* Whether the DRUPAL_AUTHENTICATED_RID can post comments.
*
......@@ -78,8 +71,6 @@ class CommentManager implements CommentManagerInterface {
* The field info service.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
......@@ -87,10 +78,9 @@ class CommentManager implements CommentManagerInterface {
* @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
* The url generator service.
*/
public function __construct(FieldInfo $field_info, EntityManagerInterface $entity_manager, AccountInterface $current_user, ConfigFactoryInterface $config_factory, TranslationInterface $translation_manager, UrlGeneratorInterface $url_generator) {
public function __construct(FieldInfo $field_info, EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, TranslationInterface $translation_manager, UrlGeneratorInterface $url_generator) {
$this->fieldInfo = $field_info;
$this->entityManager = $entity_manager;
$this->currentUser = $current_user;
$this->userConfig = $config_factory->get('user.settings');
$this->translationManager = $translation_manager;
$this->urlGenerator = $url_generator;
......@@ -277,39 +267,37 @@ public function getFieldUIPageTitle($commented_entity_type, $field_name) {
* {@inheritdoc}
*/
public function forbiddenMessage(EntityInterface $entity, $field_name) {
if ($this->currentUser->isAnonymous()) {
if (!isset($this->authenticatedCanPostComments)) {
// We only output a link if we are certain that users will get the
// permission to post comments by logging in.
$this->authenticatedCanPostComments = $this->entityManager
->getStorage('user_role')
->load(DRUPAL_AUTHENTICATED_RID)
->hasPermission('post comments');
}
if (!isset($this->authenticatedCanPostComments)) {
// We only output a link if we are certain that users will get the
// permission to post comments by logging in.
$this->authenticatedCanPostComments = $this->entityManager
->getStorage('user_role')
->load(DRUPAL_AUTHENTICATED_RID)
->hasPermission('post comments');
}
if ($this->authenticatedCanPostComments) {
// We cannot use drupal_get_destination() because these links
// sometimes appear on /node and taxonomy listing pages.
if ($entity->get($field_name)->getFieldDefinition()->getSetting('form_location') == COMMENT_FORM_SEPARATE_PAGE) {
$destination = array('destination' => 'comment/reply/' . $entity->getEntityTypeId() . '/' . $entity->id() . '/' . $field_name . '#comment-form');
}
else {
$destination = array('destination' => $entity->getSystemPath() . '#comment-form');
}
if ($this->authenticatedCanPostComments) {
// We cannot use drupal_get_destination() because these links
// sometimes appear on /node and taxonomy listing pages.
if ($entity->get($field_name)->getFieldDefinition()->getSetting('form_location') == COMMENT_FORM_SEPARATE_PAGE) {
$destination = array('destination' => 'comment/reply/' . $entity->getEntityTypeId() . '/' . $entity->id() . '/' . $field_name . '#comment-form');
}
else {
$destination = array('destination' => $entity->getSystemPath() . '#comment-form');
}
if ($this->userConfig->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) {
// Users can register themselves.
return $this->t('<a href="@login">Log in</a> or <a href="@register">register</a> to post comments', array(
'@login' => $this->urlGenerator->generateFromRoute('user.login', array(), array('query' => $destination)),
'@register' => $this->urlGenerator->generateFromRoute('user.register', array(), array('query' => $destination)),
));
}
else {
// Only admins can add new users, no public registration.
return $this->t('<a href="@login">Log in</a> to post comments', array(
'@login' => $this->urlGenerator->generateFromRoute('user.login', array(), array('query' => $destination)),
));
}
if ($this->userConfig->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) {
// Users can register themselves.
return $this->t('<a href="@login">Log in</a> or <a href="@register">register</a> to post comments', array(
'@login' => $this->urlGenerator->generateFromRoute('user.login', array(), array('query' => $destination)),
'@register' => $this->urlGenerator->generateFromRoute('user.register', array(), array('query' => $destination)),
));
}
else {
// Only admins can add new users, no public registration.
return $this->t('<a href="@login">Log in</a> to post comments', array(
'@login' => $this->urlGenerator->generateFromRoute('user.login', array(), array('query' => $destination)),
));
}
}
return '';
......
......@@ -241,14 +241,14 @@ protected static function buildLinks(CommentInterface $entity, EntityInterface $
'html' => TRUE,
);
}
if (empty($links)) {
if (empty($links) && \Drupal::currentUser()->isAnonymous()) {
$links['comment-forbidden']['title'] = \Drupal::service('comment.manager')->forbiddenMessage($commented_entity, $entity->getFieldName());
$links['comment-forbidden']['html'] = TRUE;
}
}
// Add translations link for translation-enabled comment bundles.
if ($container->get('module_handler')->moduleExists('content_translation') && content_translation_translate_access($entity)) {
if (\Drupal::moduleHandler()->moduleExists('content_translation') && content_translation_translate_access($entity)) {
$links['comment-translations'] = array(
'title' => t('Translate'),
'href' => 'comment/' . $entity->id() . '/translations',
......
......@@ -19,7 +19,7 @@ class AuthTest extends RESTTestBase {
*
* @var array
*/
public static $modules = array('basic_auth', 'hal', 'rest', 'entity_test');
public static $modules = array('basic_auth', 'hal', 'rest', 'entity_test', 'comment');
/**
* {@inheritdoc}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment