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