View source
<?php
namespace Drupal\comment_perm;
use Drupal\comment\CommentInterface;
use Drupal\comment\CommentManagerInterface;
use Drupal\comment\CommentStorage as CommentStorageBase;
use Drupal\Core\Entity\EntityInterface;
class CommentStorage extends CommentStorageBase {
use CommentAccessTrait;
public function getDisplayOrdinal(CommentInterface $comment, $comment_mode, $divisor = 1) {
$data_table = $this
->getDataTable();
$query = $this->database
->select($data_table, 'c1');
$query
->innerJoin($data_table, 'c2', 'c2.entity_id = c1.entity_id AND c2.entity_type = c1.entity_type AND c2.field_name = c1.field_name');
$query
->addExpression('COUNT(*)', 'count');
$query
->condition('c2.cid', $comment
->id());
if ($this
->noAccessAdministerComment($this->currentUser, $comment
->bundle())) {
$query
->condition('c1.status', CommentInterface::PUBLISHED);
}
if ($comment_mode == CommentManagerInterface::COMMENT_MODE_FLAT) {
$query
->condition('c1.cid', $comment
->id(), '<');
}
else {
$query
->where('SUBSTRING(c1.thread, 1, (LENGTH(c1.thread) - 1)) < SUBSTRING(c2.thread, 1, (LENGTH(c2.thread) - 1))');
}
$query
->condition('c1.default_langcode', 1);
$query
->condition('c2.default_langcode', 1);
$ordinal = $query
->execute()
->fetchField();
return $divisor > 1 ? floor($ordinal / $divisor) : $ordinal;
}
public function loadThread(EntityInterface $entity, $field_name, $mode, $comments_per_page = 0, $pager_id = 0) {
$data_table = $this
->getDataTable();
$query = $this->database
->select($data_table, 'c');
$query
->addField('c', 'cid');
$query
->condition('c.entity_id', $entity
->id())
->condition('c.entity_type', $entity
->getEntityTypeId())
->condition('c.field_name', $field_name)
->condition('c.default_langcode', 1)
->addTag('entity_access')
->addTag('comment_filter')
->addMetaData('base_table', 'comment')
->addMetaData('entity', $entity)
->addMetaData('field_name', $field_name);
if ($comments_per_page) {
$query = $query
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->limit($comments_per_page);
if ($pager_id) {
$query
->element($pager_id);
}
$count_query = $this->database
->select($data_table, 'c');
$count_query
->addExpression('COUNT(*)');
$count_query
->condition('c.entity_id', $entity
->id())
->condition('c.entity_type', $entity
->getEntityTypeId())
->condition('c.field_name', $field_name)
->condition('c.default_langcode', 1)
->addTag('entity_access')
->addTag('comment_filter')
->addMetaData('base_table', 'comment')
->addMetaData('entity', $entity)
->addMetaData('field_name', $field_name);
$query
->setCountQuery($count_query);
}
$comment_type = $entity
->get($field_name)
->getSettings()['comment_type'];
if ($this
->noAccessAdministerComment($this->currentUser, $comment_type)) {
$query
->condition('c.status', CommentInterface::PUBLISHED);
if ($comments_per_page) {
$count_query
->condition('c.status', CommentInterface::PUBLISHED);
}
}
if ($mode == CommentManagerInterface::COMMENT_MODE_FLAT) {
$query
->orderBy('c.cid', 'ASC');
}
else {
$query
->addExpression('SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))', 'torder');
$query
->orderBy('torder', 'ASC');
}
$cids = $query
->execute()
->fetchCol();
$comments = [];
if ($cids) {
$comments = $this
->loadMultiple($cids);
}
return $comments;
}
public function currentUser() {
return $this->currentUser;
}
}