annotate core/modules/comment/src/CommentManager.php @ 2:5311817fb629

Theme updates
author Chris Cannam
date Tue, 10 Jul 2018 13:19:18 +0000
parents c75dbcec494b
children 12f9dff5fda9
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\comment;
Chris@0 4
Chris@0 5 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
Chris@0 6 use Drupal\Core\Config\ConfigFactoryInterface;
Chris@0 7 use Drupal\Core\Entity\EntityInterface;
Chris@0 8 use Drupal\Core\Entity\EntityManagerInterface;
Chris@0 9 use Drupal\Core\Entity\FieldableEntityInterface;
Chris@0 10 use Drupal\Core\Extension\ModuleHandlerInterface;
Chris@0 11 use Drupal\Core\Routing\UrlGeneratorInterface;
Chris@0 12 use Drupal\Core\Routing\UrlGeneratorTrait;
Chris@0 13 use Drupal\Core\Session\AccountInterface;
Chris@0 14 use Drupal\Core\StringTranslation\StringTranslationTrait;
Chris@0 15 use Drupal\Core\StringTranslation\TranslationInterface;
Chris@0 16 use Drupal\field\Entity\FieldStorageConfig;
Chris@0 17 use Drupal\field\Entity\FieldConfig;
Chris@0 18 use Drupal\user\RoleInterface;
Chris@0 19
Chris@0 20 /**
Chris@0 21 * Comment manager contains common functions to manage comment fields.
Chris@0 22 */
Chris@0 23 class CommentManager implements CommentManagerInterface {
Chris@0 24 use StringTranslationTrait;
Chris@0 25 use UrlGeneratorTrait;
Chris@0 26
Chris@0 27 /**
Chris@0 28 * The entity manager service.
Chris@0 29 *
Chris@0 30 * @var \Drupal\Core\Entity\EntityManagerInterface
Chris@0 31 */
Chris@0 32 protected $entityManager;
Chris@0 33
Chris@0 34 /**
Chris@0 35 * Whether the \Drupal\user\RoleInterface::AUTHENTICATED_ID can post comments.
Chris@0 36 *
Chris@0 37 * @var bool
Chris@0 38 */
Chris@0 39 protected $authenticatedCanPostComments;
Chris@0 40
Chris@0 41 /**
Chris@0 42 * The user settings config object.
Chris@0 43 *
Chris@0 44 * @var \Drupal\Core\Config\Config
Chris@0 45 */
Chris@0 46 protected $userConfig;
Chris@0 47
Chris@0 48 /**
Chris@0 49 * The module handler service.
Chris@0 50 *
Chris@0 51 * @var \Drupal\Core\Extension\ModuleHandlerInterface
Chris@0 52 */
Chris@0 53 protected $moduleHandler;
Chris@0 54
Chris@0 55 /**
Chris@0 56 * The current user.
Chris@0 57 *
Chris@0 58 * @var \Drupal\Core\Session\AccountInterface
Chris@0 59 */
Chris@0 60 protected $currentUser;
Chris@0 61
Chris@0 62 /**
Chris@0 63 * Construct the CommentManager object.
Chris@0 64 *
Chris@0 65 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
Chris@0 66 * The entity manager service.
Chris@0 67 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Chris@0 68 * The config factory.
Chris@0 69 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
Chris@0 70 * The string translation service.
Chris@0 71 * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
Chris@0 72 * The url generator service.
Chris@0 73 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
Chris@0 74 * The module handler service.
Chris@0 75 * @param \Drupal\Core\Session\AccountInterface $current_user
Chris@0 76 * The current user.
Chris@0 77 */
Chris@0 78 public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, UrlGeneratorInterface $url_generator, ModuleHandlerInterface $module_handler, AccountInterface $current_user) {
Chris@0 79 $this->entityManager = $entity_manager;
Chris@0 80 $this->userConfig = $config_factory->get('user.settings');
Chris@0 81 $this->stringTranslation = $string_translation;
Chris@0 82 $this->urlGenerator = $url_generator;
Chris@0 83 $this->moduleHandler = $module_handler;
Chris@0 84 $this->currentUser = $current_user;
Chris@0 85 }
Chris@0 86
Chris@0 87 /**
Chris@0 88 * {@inheritdoc}
Chris@0 89 */
Chris@0 90 public function getFields($entity_type_id) {
Chris@0 91 $entity_type = $this->entityManager->getDefinition($entity_type_id);
Chris@0 92 if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) {
Chris@0 93 return [];
Chris@0 94 }
Chris@0 95
Chris@0 96 $map = $this->entityManager->getFieldMapByFieldType('comment');
Chris@0 97 return isset($map[$entity_type_id]) ? $map[$entity_type_id] : [];
Chris@0 98 }
Chris@0 99
Chris@0 100 /**
Chris@0 101 * {@inheritdoc}
Chris@0 102 */
Chris@0 103 public function addBodyField($comment_type_id) {
Chris@0 104 if (!FieldConfig::loadByName('comment', $comment_type_id, 'comment_body')) {
Chris@0 105 // Attaches the body field by default.
Chris@0 106 $field = $this->entityManager->getStorage('field_config')->create([
Chris@0 107 'label' => 'Comment',
Chris@0 108 'bundle' => $comment_type_id,
Chris@0 109 'required' => TRUE,
Chris@0 110 'field_storage' => FieldStorageConfig::loadByName('comment', 'comment_body'),
Chris@0 111 ]);
Chris@0 112 $field->save();
Chris@0 113
Chris@0 114 // Assign widget settings for the 'default' form mode.
Chris@0 115 entity_get_form_display('comment', $comment_type_id, 'default')
Chris@0 116 ->setComponent('comment_body', [
Chris@0 117 'type' => 'text_textarea',
Chris@0 118 ])
Chris@0 119 ->save();
Chris@0 120
Chris@0 121 // Assign display settings for the 'default' view mode.
Chris@0 122 entity_get_display('comment', $comment_type_id, 'default')
Chris@0 123 ->setComponent('comment_body', [
Chris@0 124 'label' => 'hidden',
Chris@0 125 'type' => 'text_default',
Chris@0 126 'weight' => 0,
Chris@0 127 ])
Chris@0 128 ->save();
Chris@0 129 }
Chris@0 130 }
Chris@0 131
Chris@0 132 /**
Chris@0 133 * {@inheritdoc}
Chris@0 134 */
Chris@0 135 public function forbiddenMessage(EntityInterface $entity, $field_name) {
Chris@0 136 if (!isset($this->authenticatedCanPostComments)) {
Chris@0 137 // We only output a link if we are certain that users will get the
Chris@0 138 // permission to post comments by logging in.
Chris@0 139 $this->authenticatedCanPostComments = $this->entityManager
Chris@0 140 ->getStorage('user_role')
Chris@0 141 ->load(RoleInterface::AUTHENTICATED_ID)
Chris@0 142 ->hasPermission('post comments');
Chris@0 143 }
Chris@0 144
Chris@0 145 if ($this->authenticatedCanPostComments) {
Chris@0 146 // We cannot use the redirect.destination service here because these links
Chris@0 147 // sometimes appear on /node and taxonomy listing pages.
Chris@0 148 if ($entity->get($field_name)->getFieldDefinition()->getSetting('form_location') == CommentItemInterface::FORM_SEPARATE_PAGE) {
Chris@0 149 $comment_reply_parameters = [
Chris@0 150 'entity_type' => $entity->getEntityTypeId(),
Chris@0 151 'entity' => $entity->id(),
Chris@0 152 'field_name' => $field_name,
Chris@0 153 ];
Chris@0 154 $destination = ['destination' => $this->url('comment.reply', $comment_reply_parameters, ['fragment' => 'comment-form'])];
Chris@0 155 }
Chris@0 156 else {
Chris@0 157 $destination = ['destination' => $entity->url('canonical', ['fragment' => 'comment-form'])];
Chris@0 158 }
Chris@0 159
Chris@0 160 if ($this->userConfig->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) {
Chris@0 161 // Users can register themselves.
Chris@0 162 return $this->t('<a href=":login">Log in</a> or <a href=":register">register</a> to post comments', [
Chris@0 163 ':login' => $this->urlGenerator->generateFromRoute('user.login', [], ['query' => $destination]),
Chris@0 164 ':register' => $this->urlGenerator->generateFromRoute('user.register', [], ['query' => $destination]),
Chris@0 165 ]);
Chris@0 166 }
Chris@0 167 else {
Chris@0 168 // Only admins can add new users, no public registration.
Chris@0 169 return $this->t('<a href=":login">Log in</a> to post comments', [
Chris@0 170 ':login' => $this->urlGenerator->generateFromRoute('user.login', [], ['query' => $destination]),
Chris@0 171 ]);
Chris@0 172 }
Chris@0 173 }
Chris@0 174 return '';
Chris@0 175 }
Chris@0 176
Chris@0 177 /**
Chris@0 178 * {@inheritdoc}
Chris@0 179 */
Chris@0 180 public function getCountNewComments(EntityInterface $entity, $field_name = NULL, $timestamp = 0) {
Chris@0 181 // @todo Replace module handler with optional history service injection
Chris@0 182 // after https://www.drupal.org/node/2081585.
Chris@0 183 if ($this->currentUser->isAuthenticated() && $this->moduleHandler->moduleExists('history')) {
Chris@0 184 // Retrieve the timestamp at which the current user last viewed this entity.
Chris@0 185 if (!$timestamp) {
Chris@0 186 if ($entity->getEntityTypeId() == 'node') {
Chris@0 187 $timestamp = history_read($entity->id());
Chris@0 188 }
Chris@0 189 else {
Chris@0 190 $function = $entity->getEntityTypeId() . '_last_viewed';
Chris@0 191 if (function_exists($function)) {
Chris@0 192 $timestamp = $function($entity->id());
Chris@0 193 }
Chris@0 194 else {
Chris@0 195 // Default to 30 days ago.
Chris@0 196 // @todo Remove once https://www.drupal.org/node/1029708 lands.
Chris@0 197 $timestamp = COMMENT_NEW_LIMIT;
Chris@0 198 }
Chris@0 199 }
Chris@0 200 }
Chris@0 201 $timestamp = ($timestamp > HISTORY_READ_LIMIT ? $timestamp : HISTORY_READ_LIMIT);
Chris@0 202
Chris@0 203 // Use the timestamp to retrieve the number of new comments.
Chris@0 204 $query = $this->entityManager->getStorage('comment')->getQuery()
Chris@0 205 ->condition('entity_type', $entity->getEntityTypeId())
Chris@0 206 ->condition('entity_id', $entity->id())
Chris@0 207 ->condition('created', $timestamp, '>')
Chris@0 208 ->condition('status', CommentInterface::PUBLISHED);
Chris@0 209 if ($field_name) {
Chris@0 210 // Limit to a particular field.
Chris@0 211 $query->condition('field_name', $field_name);
Chris@0 212 }
Chris@0 213
Chris@0 214 return $query->count()->execute();
Chris@0 215 }
Chris@0 216 return FALSE;
Chris@0 217 }
Chris@0 218
Chris@0 219 }