comparison core/modules/comment/src/CommentManager.php @ 0:c75dbcec494b

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