View source
<?php
namespace Drupal\comment;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\user\RoleInterface;
use Drupal\user\UserInterface;
class CommentManager implements CommentManagerInterface {
use StringTranslationTrait;
use DeprecatedServicePropertyTrait;
protected $deprecatedProperties = [
'entityManager' => 'entity.manager',
];
protected $entityFieldManager;
protected $entityDisplayRepository;
protected $entityTypeManager;
protected $authenticatedCanPostComments;
protected $userConfig;
protected $moduleHandler;
protected $currentUser;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler, AccountInterface $current_user, EntityFieldManagerInterface $entity_field_manager = NULL, EntityDisplayRepositoryInterface $entity_display_repository = NULL) {
$this->entityTypeManager = $entity_type_manager;
$this->userConfig = $config_factory
->get('user.settings');
$this->stringTranslation = $string_translation;
$this->moduleHandler = $module_handler;
$this->currentUser = $current_user;
if (!$entity_field_manager) {
@trigger_error('The entity_field.manager service must be passed to CommentManager::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
$entity_field_manager = \Drupal::service('entity_field.manager');
}
$this->entityFieldManager = $entity_field_manager;
if (!$entity_display_repository) {
@trigger_error('The entity_display.repository service must be passed to CommentManager::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2835616.', E_USER_DEPRECATED);
$entity_display_repository = \Drupal::service('entity_display.repository');
}
$this->entityDisplayRepository = $entity_display_repository;
}
public function getFields($entity_type_id) {
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
if (!$entity_type
->entityClassImplements(FieldableEntityInterface::class)) {
return [];
}
$map = $this->entityFieldManager
->getFieldMapByFieldType('comment');
return isset($map[$entity_type_id]) ? $map[$entity_type_id] : [];
}
public function addBodyField($comment_type_id) {
if (!FieldConfig::loadByName('comment', $comment_type_id, 'comment_body')) {
$field = $this->entityTypeManager
->getStorage('field_config')
->create([
'label' => 'Comment',
'bundle' => $comment_type_id,
'required' => TRUE,
'field_storage' => FieldStorageConfig::loadByName('comment', 'comment_body'),
]);
$field
->save();
$this->entityDisplayRepository
->getFormDisplay('comment', $comment_type_id)
->setComponent('comment_body', [
'type' => 'text_textarea',
])
->save();
$this->entityDisplayRepository
->getViewDisplay('comment', $comment_type_id)
->setComponent('comment_body', [
'label' => 'hidden',
'type' => 'text_default',
'weight' => 0,
])
->save();
}
}
public function forbiddenMessage(EntityInterface $entity, $field_name) {
if (!isset($this->authenticatedCanPostComments)) {
$this->authenticatedCanPostComments = $this->entityTypeManager
->getStorage('user_role')
->load(RoleInterface::AUTHENTICATED_ID)
->hasPermission('post comments');
}
if ($this->authenticatedCanPostComments) {
if ($entity
->get($field_name)
->getFieldDefinition()
->getSetting('form_location') == CommentItemInterface::FORM_SEPARATE_PAGE) {
$comment_reply_parameters = [
'entity_type' => $entity
->getEntityTypeId(),
'entity' => $entity
->id(),
'field_name' => $field_name,
];
$destination = [
'destination' => Url::fromRoute('comment.reply', $comment_reply_parameters, [
'fragment' => 'comment-form',
])
->toString(),
];
}
else {
$destination = [
'destination' => $entity
->toUrl('canonical', [
'fragment' => 'comment-form',
])
->toString(),
];
}
if ($this->userConfig
->get('register') != UserInterface::REGISTER_ADMINISTRATORS_ONLY) {
return $this
->t('<a href=":login">Log in</a> or <a href=":register">register</a> to post comments', [
':login' => Url::fromRoute('user.login', [], [
'query' => $destination,
])
->toString(),
':register' => Url::fromRoute('user.register', [], [
'query' => $destination,
])
->toString(),
]);
}
else {
return $this
->t('<a href=":login">Log in</a> to post comments', [
':login' => Url::fromRoute('user.login', [], [
'query' => $destination,
])
->toString(),
]);
}
}
return '';
}
public function getCountNewComments(EntityInterface $entity, $field_name = NULL, $timestamp = 0) {
if ($this->currentUser
->isAuthenticated() && $this->moduleHandler
->moduleExists('history')) {
if (!$timestamp) {
if ($entity
->getEntityTypeId() == 'node') {
$timestamp = history_read($entity
->id());
}
else {
$function = $entity
->getEntityTypeId() . '_last_viewed';
if (function_exists($function)) {
$timestamp = $function($entity
->id());
}
else {
$timestamp = COMMENT_NEW_LIMIT;
}
}
}
$timestamp = $timestamp > HISTORY_READ_LIMIT ? $timestamp : HISTORY_READ_LIMIT;
$query = $this->entityTypeManager
->getStorage('comment')
->getQuery()
->condition('entity_type', $entity
->getEntityTypeId())
->condition('entity_id', $entity
->id())
->condition('created', $timestamp, '>')
->condition('status', CommentInterface::PUBLISHED);
if ($field_name) {
$query
->condition('field_name', $field_name);
}
return $query
->count()
->execute();
}
return FALSE;
}
}