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