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