Chris@0: entityManager = $entity_manager; Chris@0: $this->userConfig = $config_factory->get('user.settings'); Chris@0: $this->stringTranslation = $string_translation; Chris@0: $this->urlGenerator = $url_generator; Chris@0: $this->moduleHandler = $module_handler; Chris@0: $this->currentUser = $current_user; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getFields($entity_type_id) { Chris@0: $entity_type = $this->entityManager->getDefinition($entity_type_id); Chris@0: if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) { Chris@0: return []; Chris@0: } Chris@0: Chris@0: $map = $this->entityManager->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@0: $field = $this->entityManager->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@0: $this->authenticatedCanPostComments = $this->entityManager 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@0: $destination = ['destination' => $this->url('comment.reply', $comment_reply_parameters, ['fragment' => 'comment-form'])]; Chris@0: } Chris@0: else { Chris@0: $destination = ['destination' => $entity->url('canonical', ['fragment' => 'comment-form'])]; Chris@0: } Chris@0: Chris@0: if ($this->userConfig->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) { Chris@0: // Users can register themselves. Chris@0: return $this->t('Log in or register to post comments', [ Chris@0: ':login' => $this->urlGenerator->generateFromRoute('user.login', [], ['query' => $destination]), Chris@0: ':register' => $this->urlGenerator->generateFromRoute('user.register', [], ['query' => $destination]), 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@0: ':login' => $this->urlGenerator->generateFromRoute('user.login', [], ['query' => $destination]), 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@0: $query = $this->entityManager->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: }