Chris@0: 'entity.manager']; Chris@18: Chris@18: /** Chris@18: * The entity field manager. Chris@0: * Chris@18: * @var \Drupal\Core\Entity\EntityFieldManagerInterface Chris@0: */ Chris@18: protected $entityFieldManager; Chris@18: Chris@18: /** Chris@18: * The entity type manager. Chris@18: * Chris@18: * @var \Drupal\Core\Entity\EntityTypeManagerInterface Chris@18: */ Chris@18: protected $entityTypeManager; Chris@0: Chris@0: /** Chris@0: * Whether the \Drupal\user\RoleInterface::AUTHENTICATED_ID can post comments. Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: protected $authenticatedCanPostComments; Chris@0: Chris@0: /** Chris@0: * The user settings config object. Chris@0: * Chris@0: * @var \Drupal\Core\Config\Config Chris@0: */ Chris@0: protected $userConfig; Chris@0: Chris@0: /** Chris@0: * The module handler service. Chris@0: * Chris@0: * @var \Drupal\Core\Extension\ModuleHandlerInterface Chris@0: */ Chris@0: protected $moduleHandler; Chris@0: Chris@0: /** Chris@0: * The current user. Chris@0: * Chris@0: * @var \Drupal\Core\Session\AccountInterface Chris@0: */ Chris@0: protected $currentUser; Chris@0: Chris@0: /** Chris@0: * Construct the CommentManager object. Chris@0: * Chris@18: * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager Chris@18: * The entity type manager service. Chris@0: * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory Chris@0: * The config factory. Chris@0: * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation Chris@0: * The string translation service. Chris@0: * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler Chris@0: * The module handler service. Chris@0: * @param \Drupal\Core\Session\AccountInterface $current_user Chris@0: * The current user. Chris@18: * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager Chris@18: * The entity field manager service. Chris@0: */ Chris@18: public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler, AccountInterface $current_user, EntityFieldManagerInterface $entity_field_manager = NULL) { Chris@18: $this->entityTypeManager = $entity_type_manager; Chris@0: $this->userConfig = $config_factory->get('user.settings'); Chris@0: $this->stringTranslation = $string_translation; Chris@0: $this->moduleHandler = $module_handler; Chris@0: $this->currentUser = $current_user; Chris@18: if (!$entity_field_manager) { Chris@18: @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); Chris@18: $entity_field_manager = \Drupal::service('entity_field.manager'); Chris@18: } Chris@18: $this->entityFieldManager = $entity_field_manager; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getFields($entity_type_id) { Chris@18: $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); Chris@0: if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) { Chris@0: return []; Chris@0: } Chris@0: Chris@18: $map = $this->entityFieldManager->getFieldMapByFieldType('comment'); Chris@0: return isset($map[$entity_type_id]) ? $map[$entity_type_id] : []; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function addBodyField($comment_type_id) { Chris@0: if (!FieldConfig::loadByName('comment', $comment_type_id, 'comment_body')) { Chris@0: // Attaches the body field by default. Chris@18: $field = $this->entityTypeManager->getStorage('field_config')->create([ Chris@0: 'label' => 'Comment', Chris@0: 'bundle' => $comment_type_id, Chris@0: 'required' => TRUE, Chris@0: 'field_storage' => FieldStorageConfig::loadByName('comment', 'comment_body'), Chris@0: ]); Chris@0: $field->save(); Chris@0: Chris@0: // Assign widget settings for the 'default' form mode. Chris@0: entity_get_form_display('comment', $comment_type_id, 'default') Chris@0: ->setComponent('comment_body', [ Chris@0: 'type' => 'text_textarea', Chris@0: ]) Chris@0: ->save(); Chris@0: Chris@0: // Assign display settings for the 'default' view mode. Chris@0: entity_get_display('comment', $comment_type_id, 'default') Chris@0: ->setComponent('comment_body', [ Chris@0: 'label' => 'hidden', Chris@0: 'type' => 'text_default', Chris@0: 'weight' => 0, Chris@0: ]) Chris@0: ->save(); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function forbiddenMessage(EntityInterface $entity, $field_name) { Chris@0: if (!isset($this->authenticatedCanPostComments)) { Chris@0: // We only output a link if we are certain that users will get the Chris@0: // permission to post comments by logging in. Chris@18: $this->authenticatedCanPostComments = $this->entityTypeManager Chris@0: ->getStorage('user_role') Chris@0: ->load(RoleInterface::AUTHENTICATED_ID) Chris@0: ->hasPermission('post comments'); Chris@0: } Chris@0: Chris@0: if ($this->authenticatedCanPostComments) { Chris@0: // We cannot use the redirect.destination service here because these links Chris@0: // sometimes appear on /node and taxonomy listing pages. Chris@0: if ($entity->get($field_name)->getFieldDefinition()->getSetting('form_location') == CommentItemInterface::FORM_SEPARATE_PAGE) { Chris@0: $comment_reply_parameters = [ Chris@0: 'entity_type' => $entity->getEntityTypeId(), Chris@0: 'entity' => $entity->id(), Chris@0: 'field_name' => $field_name, Chris@0: ]; Chris@18: $destination = ['destination' => Url::fromRoute('comment.reply', $comment_reply_parameters, ['fragment' => 'comment-form'])->toString()]; Chris@0: } Chris@0: else { Chris@18: $destination = ['destination' => $entity->toUrl('canonical', ['fragment' => 'comment-form'])->toString()]; Chris@0: } Chris@0: Chris@18: if ($this->userConfig->get('register') != UserInterface::REGISTER_ADMINISTRATORS_ONLY) { Chris@0: // Users can register themselves. Chris@0: return $this->t('Log in or register to post comments', [ Chris@18: ':login' => Url::fromRoute('user.login', [], ['query' => $destination])->toString(), Chris@18: ':register' => Url::fromRoute('user.register', [], ['query' => $destination])->toString(), Chris@0: ]); Chris@0: } Chris@0: else { Chris@0: // Only admins can add new users, no public registration. Chris@0: return $this->t('Log in to post comments', [ Chris@18: ':login' => Url::fromRoute('user.login', [], ['query' => $destination])->toString(), Chris@0: ]); Chris@0: } Chris@0: } Chris@0: return ''; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getCountNewComments(EntityInterface $entity, $field_name = NULL, $timestamp = 0) { Chris@0: // @todo Replace module handler with optional history service injection Chris@0: // after https://www.drupal.org/node/2081585. Chris@0: if ($this->currentUser->isAuthenticated() && $this->moduleHandler->moduleExists('history')) { Chris@0: // Retrieve the timestamp at which the current user last viewed this entity. Chris@0: if (!$timestamp) { Chris@0: if ($entity->getEntityTypeId() == 'node') { Chris@0: $timestamp = history_read($entity->id()); Chris@0: } Chris@0: else { Chris@0: $function = $entity->getEntityTypeId() . '_last_viewed'; Chris@0: if (function_exists($function)) { Chris@0: $timestamp = $function($entity->id()); Chris@0: } Chris@0: else { Chris@0: // Default to 30 days ago. Chris@0: // @todo Remove once https://www.drupal.org/node/1029708 lands. Chris@0: $timestamp = COMMENT_NEW_LIMIT; Chris@0: } Chris@0: } Chris@0: } Chris@0: $timestamp = ($timestamp > HISTORY_READ_LIMIT ? $timestamp : HISTORY_READ_LIMIT); Chris@0: Chris@0: // Use the timestamp to retrieve the number of new comments. Chris@18: $query = $this->entityTypeManager->getStorage('comment')->getQuery() Chris@0: ->condition('entity_type', $entity->getEntityTypeId()) Chris@0: ->condition('entity_id', $entity->id()) Chris@0: ->condition('created', $timestamp, '>') Chris@0: ->condition('status', CommentInterface::PUBLISHED); Chris@0: if ($field_name) { Chris@0: // Limit to a particular field. Chris@0: $query->condition('field_name', $field_name); Chris@0: } Chris@0: Chris@0: return $query->count()->execute(); Chris@0: } Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: }