X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fcomment%2Fsrc%2FCommentManager.php;fp=web%2Fcore%2Fmodules%2Fcomment%2Fsrc%2FCommentManager.php;h=80cc427e34c3fad28e4cbee443d74ef4ef9bee4e;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/comment/src/CommentManager.php b/web/core/modules/comment/src/CommentManager.php new file mode 100644 index 000000000..80cc427e3 --- /dev/null +++ b/web/core/modules/comment/src/CommentManager.php @@ -0,0 +1,219 @@ +entityManager = $entity_manager; + $this->userConfig = $config_factory->get('user.settings'); + $this->stringTranslation = $string_translation; + $this->urlGenerator = $url_generator; + $this->moduleHandler = $module_handler; + $this->currentUser = $current_user; + } + + /** + * {@inheritdoc} + */ + public function getFields($entity_type_id) { + $entity_type = $this->entityManager->getDefinition($entity_type_id); + if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) { + return []; + } + + $map = $this->entityManager->getFieldMapByFieldType('comment'); + return isset($map[$entity_type_id]) ? $map[$entity_type_id] : []; + } + + /** + * {@inheritdoc} + */ + public function addBodyField($comment_type_id) { + if (!FieldConfig::loadByName('comment', $comment_type_id, 'comment_body')) { + // Attaches the body field by default. + $field = $this->entityManager->getStorage('field_config')->create([ + 'label' => 'Comment', + 'bundle' => $comment_type_id, + 'required' => TRUE, + 'field_storage' => FieldStorageConfig::loadByName('comment', 'comment_body'), + ]); + $field->save(); + + // Assign widget settings for the 'default' form mode. + entity_get_form_display('comment', $comment_type_id, 'default') + ->setComponent('comment_body', [ + 'type' => 'text_textarea', + ]) + ->save(); + + // Assign display settings for the 'default' view mode. + entity_get_display('comment', $comment_type_id, 'default') + ->setComponent('comment_body', [ + 'label' => 'hidden', + 'type' => 'text_default', + 'weight' => 0, + ]) + ->save(); + } + } + + /** + * {@inheritdoc} + */ + public function forbiddenMessage(EntityInterface $entity, $field_name) { + 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(RoleInterface::AUTHENTICATED_ID) + ->hasPermission('post comments'); + } + + if ($this->authenticatedCanPostComments) { + // We cannot use the redirect.destination service here because these links + // sometimes appear on /node and taxonomy listing pages. + 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' => $this->url('comment.reply', $comment_reply_parameters, ['fragment' => 'comment-form'])]; + } + else { + $destination = ['destination' => $entity->url('canonical', ['fragment' => 'comment-form'])]; + } + + if ($this->userConfig->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) { + // Users can register themselves. + return $this->t('Log in or register to post comments', [ + ':login' => $this->urlGenerator->generateFromRoute('user.login', [], ['query' => $destination]), + ':register' => $this->urlGenerator->generateFromRoute('user.register', [], ['query' => $destination]), + ]); + } + else { + // Only admins can add new users, no public registration. + return $this->t('Log in to post comments', [ + ':login' => $this->urlGenerator->generateFromRoute('user.login', [], ['query' => $destination]), + ]); + } + } + return ''; + } + + /** + * {@inheritdoc} + */ + public function getCountNewComments(EntityInterface $entity, $field_name = NULL, $timestamp = 0) { + // @todo Replace module handler with optional history service injection + // after https://www.drupal.org/node/2081585. + if ($this->currentUser->isAuthenticated() && $this->moduleHandler->moduleExists('history')) { + // Retrieve the timestamp at which the current user last viewed this entity. + 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 { + // Default to 30 days ago. + // @todo Remove once https://www.drupal.org/node/1029708 lands. + $timestamp = COMMENT_NEW_LIMIT; + } + } + } + $timestamp = ($timestamp > HISTORY_READ_LIMIT ? $timestamp : HISTORY_READ_LIMIT); + + // Use the timestamp to retrieve the number of new comments. + $query = $this->entityManager->getStorage('comment')->getQuery() + ->condition('entity_type', $entity->getEntityTypeId()) + ->condition('entity_id', $entity->id()) + ->condition('created', $timestamp, '>') + ->condition('status', CommentInterface::PUBLISHED); + if ($field_name) { + // Limit to a particular field. + $query->condition('field_name', $field_name); + } + + return $query->count()->execute(); + } + return FALSE; + } + +}