Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/comment/src/CommentManager.php @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | c75dbcec494b |
children |
comparison
equal
deleted
inserted
replaced
4:a9cd425dd02b | 5:12f9dff5fda9 |
---|---|
2 | 2 |
3 namespace Drupal\comment; | 3 namespace Drupal\comment; |
4 | 4 |
5 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; | 5 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; |
6 use Drupal\Core\Config\ConfigFactoryInterface; | 6 use Drupal\Core\Config\ConfigFactoryInterface; |
7 use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait; | |
8 use Drupal\Core\Entity\EntityFieldManagerInterface; | |
7 use Drupal\Core\Entity\EntityInterface; | 9 use Drupal\Core\Entity\EntityInterface; |
8 use Drupal\Core\Entity\EntityManagerInterface; | 10 use Drupal\Core\Entity\EntityTypeManagerInterface; |
9 use Drupal\Core\Entity\FieldableEntityInterface; | 11 use Drupal\Core\Entity\FieldableEntityInterface; |
10 use Drupal\Core\Extension\ModuleHandlerInterface; | 12 use Drupal\Core\Extension\ModuleHandlerInterface; |
11 use Drupal\Core\Routing\UrlGeneratorInterface; | |
12 use Drupal\Core\Routing\UrlGeneratorTrait; | |
13 use Drupal\Core\Session\AccountInterface; | 13 use Drupal\Core\Session\AccountInterface; |
14 use Drupal\Core\StringTranslation\StringTranslationTrait; | 14 use Drupal\Core\StringTranslation\StringTranslationTrait; |
15 use Drupal\Core\StringTranslation\TranslationInterface; | 15 use Drupal\Core\StringTranslation\TranslationInterface; |
16 use Drupal\Core\Url; | |
16 use Drupal\field\Entity\FieldStorageConfig; | 17 use Drupal\field\Entity\FieldStorageConfig; |
17 use Drupal\field\Entity\FieldConfig; | 18 use Drupal\field\Entity\FieldConfig; |
18 use Drupal\user\RoleInterface; | 19 use Drupal\user\RoleInterface; |
20 use Drupal\user\UserInterface; | |
19 | 21 |
20 /** | 22 /** |
21 * Comment manager contains common functions to manage comment fields. | 23 * Comment manager contains common functions to manage comment fields. |
22 */ | 24 */ |
23 class CommentManager implements CommentManagerInterface { | 25 class CommentManager implements CommentManagerInterface { |
24 use StringTranslationTrait; | 26 use StringTranslationTrait; |
25 use UrlGeneratorTrait; | 27 use DeprecatedServicePropertyTrait; |
26 | 28 |
27 /** | 29 /** |
28 * The entity manager service. | 30 * {@inheritdoc} |
29 * | 31 */ |
30 * @var \Drupal\Core\Entity\EntityManagerInterface | 32 protected $deprecatedProperties = ['entityManager' => 'entity.manager']; |
31 */ | 33 |
32 protected $entityManager; | 34 /** |
35 * The entity field manager. | |
36 * | |
37 * @var \Drupal\Core\Entity\EntityFieldManagerInterface | |
38 */ | |
39 protected $entityFieldManager; | |
40 | |
41 /** | |
42 * The entity type manager. | |
43 * | |
44 * @var \Drupal\Core\Entity\EntityTypeManagerInterface | |
45 */ | |
46 protected $entityTypeManager; | |
33 | 47 |
34 /** | 48 /** |
35 * Whether the \Drupal\user\RoleInterface::AUTHENTICATED_ID can post comments. | 49 * Whether the \Drupal\user\RoleInterface::AUTHENTICATED_ID can post comments. |
36 * | 50 * |
37 * @var bool | 51 * @var bool |
60 protected $currentUser; | 74 protected $currentUser; |
61 | 75 |
62 /** | 76 /** |
63 * Construct the CommentManager object. | 77 * Construct the CommentManager object. |
64 * | 78 * |
65 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager | 79 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
66 * The entity manager service. | 80 * The entity type manager service. |
67 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory | 81 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory |
68 * The config factory. | 82 * The config factory. |
69 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation | 83 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation |
70 * The string translation service. | 84 * 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 | 85 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler |
74 * The module handler service. | 86 * The module handler service. |
75 * @param \Drupal\Core\Session\AccountInterface $current_user | 87 * @param \Drupal\Core\Session\AccountInterface $current_user |
76 * The current user. | 88 * The current user. |
77 */ | 89 * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager |
78 public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, UrlGeneratorInterface $url_generator, ModuleHandlerInterface $module_handler, AccountInterface $current_user) { | 90 * The entity field manager service. |
79 $this->entityManager = $entity_manager; | 91 */ |
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) { | |
93 $this->entityTypeManager = $entity_type_manager; | |
80 $this->userConfig = $config_factory->get('user.settings'); | 94 $this->userConfig = $config_factory->get('user.settings'); |
81 $this->stringTranslation = $string_translation; | 95 $this->stringTranslation = $string_translation; |
82 $this->urlGenerator = $url_generator; | |
83 $this->moduleHandler = $module_handler; | 96 $this->moduleHandler = $module_handler; |
84 $this->currentUser = $current_user; | 97 $this->currentUser = $current_user; |
98 if (!$entity_field_manager) { | |
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); | |
100 $entity_field_manager = \Drupal::service('entity_field.manager'); | |
101 } | |
102 $this->entityFieldManager = $entity_field_manager; | |
85 } | 103 } |
86 | 104 |
87 /** | 105 /** |
88 * {@inheritdoc} | 106 * {@inheritdoc} |
89 */ | 107 */ |
90 public function getFields($entity_type_id) { | 108 public function getFields($entity_type_id) { |
91 $entity_type = $this->entityManager->getDefinition($entity_type_id); | 109 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); |
92 if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) { | 110 if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) { |
93 return []; | 111 return []; |
94 } | 112 } |
95 | 113 |
96 $map = $this->entityManager->getFieldMapByFieldType('comment'); | 114 $map = $this->entityFieldManager->getFieldMapByFieldType('comment'); |
97 return isset($map[$entity_type_id]) ? $map[$entity_type_id] : []; | 115 return isset($map[$entity_type_id]) ? $map[$entity_type_id] : []; |
98 } | 116 } |
99 | 117 |
100 /** | 118 /** |
101 * {@inheritdoc} | 119 * {@inheritdoc} |
102 */ | 120 */ |
103 public function addBodyField($comment_type_id) { | 121 public function addBodyField($comment_type_id) { |
104 if (!FieldConfig::loadByName('comment', $comment_type_id, 'comment_body')) { | 122 if (!FieldConfig::loadByName('comment', $comment_type_id, 'comment_body')) { |
105 // Attaches the body field by default. | 123 // Attaches the body field by default. |
106 $field = $this->entityManager->getStorage('field_config')->create([ | 124 $field = $this->entityTypeManager->getStorage('field_config')->create([ |
107 'label' => 'Comment', | 125 'label' => 'Comment', |
108 'bundle' => $comment_type_id, | 126 'bundle' => $comment_type_id, |
109 'required' => TRUE, | 127 'required' => TRUE, |
110 'field_storage' => FieldStorageConfig::loadByName('comment', 'comment_body'), | 128 'field_storage' => FieldStorageConfig::loadByName('comment', 'comment_body'), |
111 ]); | 129 ]); |
134 */ | 152 */ |
135 public function forbiddenMessage(EntityInterface $entity, $field_name) { | 153 public function forbiddenMessage(EntityInterface $entity, $field_name) { |
136 if (!isset($this->authenticatedCanPostComments)) { | 154 if (!isset($this->authenticatedCanPostComments)) { |
137 // We only output a link if we are certain that users will get the | 155 // We only output a link if we are certain that users will get the |
138 // permission to post comments by logging in. | 156 // permission to post comments by logging in. |
139 $this->authenticatedCanPostComments = $this->entityManager | 157 $this->authenticatedCanPostComments = $this->entityTypeManager |
140 ->getStorage('user_role') | 158 ->getStorage('user_role') |
141 ->load(RoleInterface::AUTHENTICATED_ID) | 159 ->load(RoleInterface::AUTHENTICATED_ID) |
142 ->hasPermission('post comments'); | 160 ->hasPermission('post comments'); |
143 } | 161 } |
144 | 162 |
149 $comment_reply_parameters = [ | 167 $comment_reply_parameters = [ |
150 'entity_type' => $entity->getEntityTypeId(), | 168 'entity_type' => $entity->getEntityTypeId(), |
151 'entity' => $entity->id(), | 169 'entity' => $entity->id(), |
152 'field_name' => $field_name, | 170 'field_name' => $field_name, |
153 ]; | 171 ]; |
154 $destination = ['destination' => $this->url('comment.reply', $comment_reply_parameters, ['fragment' => 'comment-form'])]; | 172 $destination = ['destination' => Url::fromRoute('comment.reply', $comment_reply_parameters, ['fragment' => 'comment-form'])->toString()]; |
155 } | 173 } |
156 else { | 174 else { |
157 $destination = ['destination' => $entity->url('canonical', ['fragment' => 'comment-form'])]; | 175 $destination = ['destination' => $entity->toUrl('canonical', ['fragment' => 'comment-form'])->toString()]; |
158 } | 176 } |
159 | 177 |
160 if ($this->userConfig->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) { | 178 if ($this->userConfig->get('register') != UserInterface::REGISTER_ADMINISTRATORS_ONLY) { |
161 // Users can register themselves. | 179 // Users can register themselves. |
162 return $this->t('<a href=":login">Log in</a> or <a href=":register">register</a> to post comments', [ | 180 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]), | 181 ':login' => Url::fromRoute('user.login', [], ['query' => $destination])->toString(), |
164 ':register' => $this->urlGenerator->generateFromRoute('user.register', [], ['query' => $destination]), | 182 ':register' => Url::fromRoute('user.register', [], ['query' => $destination])->toString(), |
165 ]); | 183 ]); |
166 } | 184 } |
167 else { | 185 else { |
168 // Only admins can add new users, no public registration. | 186 // Only admins can add new users, no public registration. |
169 return $this->t('<a href=":login">Log in</a> to post comments', [ | 187 return $this->t('<a href=":login">Log in</a> to post comments', [ |
170 ':login' => $this->urlGenerator->generateFromRoute('user.login', [], ['query' => $destination]), | 188 ':login' => Url::fromRoute('user.login', [], ['query' => $destination])->toString(), |
171 ]); | 189 ]); |
172 } | 190 } |
173 } | 191 } |
174 return ''; | 192 return ''; |
175 } | 193 } |
199 } | 217 } |
200 } | 218 } |
201 $timestamp = ($timestamp > HISTORY_READ_LIMIT ? $timestamp : HISTORY_READ_LIMIT); | 219 $timestamp = ($timestamp > HISTORY_READ_LIMIT ? $timestamp : HISTORY_READ_LIMIT); |
202 | 220 |
203 // Use the timestamp to retrieve the number of new comments. | 221 // Use the timestamp to retrieve the number of new comments. |
204 $query = $this->entityManager->getStorage('comment')->getQuery() | 222 $query = $this->entityTypeManager->getStorage('comment')->getQuery() |
205 ->condition('entity_type', $entity->getEntityTypeId()) | 223 ->condition('entity_type', $entity->getEntityTypeId()) |
206 ->condition('entity_id', $entity->id()) | 224 ->condition('entity_id', $entity->id()) |
207 ->condition('created', $timestamp, '>') | 225 ->condition('created', $timestamp, '>') |
208 ->condition('status', CommentInterface::PUBLISHED); | 226 ->condition('status', CommentInterface::PUBLISHED); |
209 if ($field_name) { | 227 if ($field_name) { |