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@18
|
6 use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
|
Chris@0
|
7 use Drupal\Core\Entity\EntityFormBuilderInterface;
|
Chris@0
|
8 use Drupal\Core\Entity\EntityInterface;
|
Chris@18
|
9 use Drupal\Core\Entity\EntityTypeManagerInterface;
|
Chris@0
|
10 use Drupal\Core\Extension\ModuleHandlerInterface;
|
Chris@0
|
11 use Drupal\Core\Render\RendererInterface;
|
Chris@0
|
12 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
13 use Drupal\Core\Url;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Defines a service for comment #lazy_builder callbacks.
|
Chris@0
|
17 */
|
Chris@0
|
18 class CommentLazyBuilders {
|
Chris@18
|
19 use DeprecatedServicePropertyTrait;
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@18
|
22 * {@inheritdoc}
|
Chris@18
|
23 */
|
Chris@18
|
24 protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
|
Chris@18
|
25
|
Chris@18
|
26 /**
|
Chris@18
|
27 * The entity type manager service.
|
Chris@0
|
28 *
|
Chris@18
|
29 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
Chris@0
|
30 */
|
Chris@18
|
31 protected $entityTypeManager;
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * The entity form builder service.
|
Chris@0
|
35 *
|
Chris@0
|
36 * @var \Drupal\Core\Entity\EntityFormBuilderInterface
|
Chris@0
|
37 */
|
Chris@0
|
38 protected $entityFormBuilder;
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * Comment manager service.
|
Chris@0
|
42 *
|
Chris@0
|
43 * @var \Drupal\comment\CommentManagerInterface
|
Chris@0
|
44 */
|
Chris@0
|
45 protected $commentManager;
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * Current logged in user.
|
Chris@0
|
49 *
|
Chris@0
|
50 * @var \Drupal\Core\Session\AccountInterface
|
Chris@0
|
51 */
|
Chris@0
|
52 protected $currentUser;
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * The module handler service.
|
Chris@0
|
56 *
|
Chris@0
|
57 * @var \Drupal\Core\Extension\ModuleHandlerInterface
|
Chris@0
|
58 */
|
Chris@0
|
59 protected $moduleHandler;
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * The renderer service.
|
Chris@0
|
63 *
|
Chris@0
|
64 * @var \Drupal\Core\Render\RendererInterface
|
Chris@0
|
65 */
|
Chris@0
|
66 protected $renderer;
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * Constructs a new CommentLazyBuilders object.
|
Chris@0
|
70 *
|
Chris@18
|
71 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
Chris@18
|
72 * The entity type manager service.
|
Chris@0
|
73 * @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder
|
Chris@0
|
74 * The entity form builder service.
|
Chris@0
|
75 * @param \Drupal\Core\Session\AccountInterface $current_user
|
Chris@0
|
76 * The current logged in user.
|
Chris@0
|
77 * @param \Drupal\comment\CommentManagerInterface $comment_manager
|
Chris@0
|
78 * The comment manager service.
|
Chris@0
|
79 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
Chris@0
|
80 * The module handler service.
|
Chris@0
|
81 * @param \Drupal\Core\Render\RendererInterface $renderer
|
Chris@0
|
82 * The renderer service.
|
Chris@0
|
83 */
|
Chris@18
|
84 public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFormBuilderInterface $entity_form_builder, AccountInterface $current_user, CommentManagerInterface $comment_manager, ModuleHandlerInterface $module_handler, RendererInterface $renderer) {
|
Chris@18
|
85 $this->entityTypeManager = $entity_type_manager;
|
Chris@0
|
86 $this->entityFormBuilder = $entity_form_builder;
|
Chris@0
|
87 $this->currentUser = $current_user;
|
Chris@0
|
88 $this->commentManager = $comment_manager;
|
Chris@0
|
89 $this->moduleHandler = $module_handler;
|
Chris@0
|
90 $this->renderer = $renderer;
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 /**
|
Chris@0
|
94 * #lazy_builder callback; builds the comment form.
|
Chris@0
|
95 *
|
Chris@0
|
96 * @param string $commented_entity_type_id
|
Chris@0
|
97 * The commented entity type ID.
|
Chris@0
|
98 * @param string $commented_entity_id
|
Chris@0
|
99 * The commented entity ID.
|
Chris@0
|
100 * @param string $field_name
|
Chris@0
|
101 * The comment field name.
|
Chris@0
|
102 * @param string $comment_type_id
|
Chris@0
|
103 * The comment type ID.
|
Chris@0
|
104 *
|
Chris@0
|
105 * @return array
|
Chris@0
|
106 * A renderable array containing the comment form.
|
Chris@0
|
107 */
|
Chris@0
|
108 public function renderForm($commented_entity_type_id, $commented_entity_id, $field_name, $comment_type_id) {
|
Chris@0
|
109 $values = [
|
Chris@0
|
110 'entity_type' => $commented_entity_type_id,
|
Chris@0
|
111 'entity_id' => $commented_entity_id,
|
Chris@0
|
112 'field_name' => $field_name,
|
Chris@0
|
113 'comment_type' => $comment_type_id,
|
Chris@0
|
114 'pid' => NULL,
|
Chris@0
|
115 ];
|
Chris@18
|
116 $comment = $this->entityTypeManager->getStorage('comment')->create($values);
|
Chris@0
|
117 return $this->entityFormBuilder->getForm($comment);
|
Chris@0
|
118 }
|
Chris@0
|
119
|
Chris@0
|
120 /**
|
Chris@0
|
121 * #lazy_builder callback; builds a comment's links.
|
Chris@0
|
122 *
|
Chris@0
|
123 * @param string $comment_entity_id
|
Chris@0
|
124 * The comment entity ID.
|
Chris@0
|
125 * @param string $view_mode
|
Chris@0
|
126 * The view mode in which the comment entity is being viewed.
|
Chris@0
|
127 * @param string $langcode
|
Chris@0
|
128 * The language in which the comment entity is being viewed.
|
Chris@0
|
129 * @param bool $is_in_preview
|
Chris@0
|
130 * Whether the comment is currently being previewed.
|
Chris@0
|
131 *
|
Chris@0
|
132 * @return array
|
Chris@0
|
133 * A renderable array representing the comment links.
|
Chris@0
|
134 */
|
Chris@0
|
135 public function renderLinks($comment_entity_id, $view_mode, $langcode, $is_in_preview) {
|
Chris@0
|
136 $links = [
|
Chris@0
|
137 '#theme' => 'links__comment',
|
Chris@0
|
138 '#pre_render' => ['drupal_pre_render_links'],
|
Chris@0
|
139 '#attributes' => ['class' => ['links', 'inline']],
|
Chris@0
|
140 ];
|
Chris@0
|
141
|
Chris@0
|
142 if (!$is_in_preview) {
|
Chris@0
|
143 /** @var \Drupal\comment\CommentInterface $entity */
|
Chris@18
|
144 $entity = $this->entityTypeManager->getStorage('comment')->load($comment_entity_id);
|
Chris@0
|
145 $commented_entity = $entity->getCommentedEntity();
|
Chris@0
|
146
|
Chris@0
|
147 $links['comment'] = $this->buildLinks($entity, $commented_entity);
|
Chris@0
|
148
|
Chris@0
|
149 // Allow other modules to alter the comment links.
|
Chris@0
|
150 $hook_context = [
|
Chris@0
|
151 'view_mode' => $view_mode,
|
Chris@0
|
152 'langcode' => $langcode,
|
Chris@0
|
153 'commented_entity' => $commented_entity,
|
Chris@0
|
154 ];
|
Chris@0
|
155 $this->moduleHandler->alter('comment_links', $links, $entity, $hook_context);
|
Chris@0
|
156 }
|
Chris@0
|
157 return $links;
|
Chris@0
|
158 }
|
Chris@0
|
159
|
Chris@0
|
160 /**
|
Chris@0
|
161 * Build the default links (reply, edit, delete …) for a comment.
|
Chris@0
|
162 *
|
Chris@0
|
163 * @param \Drupal\comment\CommentInterface $entity
|
Chris@0
|
164 * The comment object.
|
Chris@0
|
165 * @param \Drupal\Core\Entity\EntityInterface $commented_entity
|
Chris@0
|
166 * The entity to which the comment is attached.
|
Chris@0
|
167 *
|
Chris@0
|
168 * @return array
|
Chris@0
|
169 * An array that can be processed by drupal_pre_render_links().
|
Chris@0
|
170 */
|
Chris@0
|
171 protected function buildLinks(CommentInterface $entity, EntityInterface $commented_entity) {
|
Chris@0
|
172 $links = [];
|
Chris@0
|
173 $status = $commented_entity->get($entity->getFieldName())->status;
|
Chris@0
|
174
|
Chris@0
|
175 if ($status == CommentItemInterface::OPEN) {
|
Chris@0
|
176 if ($entity->access('delete')) {
|
Chris@0
|
177 $links['comment-delete'] = [
|
Chris@0
|
178 'title' => t('Delete'),
|
Chris@18
|
179 'url' => $entity->toUrl('delete-form'),
|
Chris@0
|
180 ];
|
Chris@0
|
181 }
|
Chris@0
|
182
|
Chris@0
|
183 if ($entity->access('update')) {
|
Chris@0
|
184 $links['comment-edit'] = [
|
Chris@0
|
185 'title' => t('Edit'),
|
Chris@18
|
186 'url' => $entity->toUrl('edit-form'),
|
Chris@0
|
187 ];
|
Chris@0
|
188 }
|
Chris@0
|
189 if ($entity->access('create')) {
|
Chris@0
|
190 $links['comment-reply'] = [
|
Chris@0
|
191 'title' => t('Reply'),
|
Chris@0
|
192 'url' => Url::fromRoute('comment.reply', [
|
Chris@0
|
193 'entity_type' => $entity->getCommentedEntityTypeId(),
|
Chris@0
|
194 'entity' => $entity->getCommentedEntityId(),
|
Chris@0
|
195 'field_name' => $entity->getFieldName(),
|
Chris@0
|
196 'pid' => $entity->id(),
|
Chris@0
|
197 ]),
|
Chris@0
|
198 ];
|
Chris@0
|
199 }
|
Chris@0
|
200 if (!$entity->isPublished() && $entity->access('approve')) {
|
Chris@0
|
201 $links['comment-approve'] = [
|
Chris@0
|
202 'title' => t('Approve'),
|
Chris@0
|
203 'url' => Url::fromRoute('comment.approve', ['comment' => $entity->id()]),
|
Chris@0
|
204 ];
|
Chris@0
|
205 }
|
Chris@0
|
206 if (empty($links) && $this->currentUser->isAnonymous()) {
|
Chris@0
|
207 $links['comment-forbidden']['title'] = $this->commentManager->forbiddenMessage($commented_entity, $entity->getFieldName());
|
Chris@0
|
208 }
|
Chris@0
|
209 }
|
Chris@0
|
210
|
Chris@0
|
211 // Add translations link for translation-enabled comment bundles.
|
Chris@0
|
212 if ($this->moduleHandler->moduleExists('content_translation') && $this->access($entity)->isAllowed()) {
|
Chris@0
|
213 $links['comment-translations'] = [
|
Chris@0
|
214 'title' => t('Translate'),
|
Chris@18
|
215 'url' => $entity->toUrl('drupal:content-translation-overview'),
|
Chris@0
|
216 ];
|
Chris@0
|
217 }
|
Chris@0
|
218
|
Chris@0
|
219 return [
|
Chris@0
|
220 '#theme' => 'links__comment__comment',
|
Chris@0
|
221 // The "entity" property is specified to be present, so no need to check.
|
Chris@0
|
222 '#links' => $links,
|
Chris@0
|
223 '#attributes' => ['class' => ['links', 'inline']],
|
Chris@0
|
224 ];
|
Chris@0
|
225 }
|
Chris@0
|
226
|
Chris@0
|
227 /**
|
Chris@0
|
228 * Wraps content_translation_translate_access.
|
Chris@0
|
229 */
|
Chris@0
|
230 protected function access(EntityInterface $entity) {
|
Chris@0
|
231 return content_translation_translate_access($entity);
|
Chris@0
|
232 }
|
Chris@0
|
233
|
Chris@0
|
234 }
|