3 namespace Drupal\comment;
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;
15 * Defines a service for comment #lazy_builder callbacks.
17 class CommentLazyBuilders {
20 * The entity manager service.
22 * @var \Drupal\Core\Entity\EntityManagerInterface
24 protected $entityManager;
27 * The entity form builder service.
29 * @var \Drupal\Core\Entity\EntityFormBuilderInterface
31 protected $entityFormBuilder;
34 * Comment manager service.
36 * @var \Drupal\comment\CommentManagerInterface
38 protected $commentManager;
41 * Current logged in user.
43 * @var \Drupal\Core\Session\AccountInterface
45 protected $currentUser;
48 * The module handler service.
50 * @var \Drupal\Core\Extension\ModuleHandlerInterface
52 protected $moduleHandler;
55 * The renderer service.
57 * @var \Drupal\Core\Render\RendererInterface
62 * Constructs a new CommentLazyBuilders object.
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.
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;
87 * #lazy_builder callback; builds the comment form.
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.
99 * A renderable array containing the comment form.
101 public function renderForm($commented_entity_type_id, $commented_entity_id, $field_name, $comment_type_id) {
103 'entity_type' => $commented_entity_type_id,
104 'entity_id' => $commented_entity_id,
105 'field_name' => $field_name,
106 'comment_type' => $comment_type_id,
109 $comment = $this->entityManager->getStorage('comment')->create($values);
110 return $this->entityFormBuilder->getForm($comment);
114 * #lazy_builder callback; builds a comment's links.
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.
126 * A renderable array representing the comment links.
128 public function renderLinks($comment_entity_id, $view_mode, $langcode, $is_in_preview) {
130 '#theme' => 'links__comment',
131 '#pre_render' => ['drupal_pre_render_links'],
132 '#attributes' => ['class' => ['links', 'inline']],
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();
140 $links['comment'] = $this->buildLinks($entity, $commented_entity);
142 // Allow other modules to alter the comment links.
144 'view_mode' => $view_mode,
145 'langcode' => $langcode,
146 'commented_entity' => $commented_entity,
148 $this->moduleHandler->alter('comment_links', $links, $entity, $hook_context);
154 * Build the default links (reply, edit, delete …) for a comment.
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.
162 * An array that can be processed by drupal_pre_render_links().
164 protected function buildLinks(CommentInterface $entity, EntityInterface $commented_entity) {
166 $status = $commented_entity->get($entity->getFieldName())->status;
168 if ($status == CommentItemInterface::OPEN) {
169 if ($entity->access('delete')) {
170 $links['comment-delete'] = [
171 'title' => t('Delete'),
172 'url' => $entity->urlInfo('delete-form'),
176 if ($entity->access('update')) {
177 $links['comment-edit'] = [
178 'title' => t('Edit'),
179 'url' => $entity->urlInfo('edit-form'),
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(),
193 if (!$entity->isPublished() && $entity->access('approve')) {
194 $links['comment-approve'] = [
195 'title' => t('Approve'),
196 'url' => Url::fromRoute('comment.approve', ['comment' => $entity->id()]),
199 if (empty($links) && $this->currentUser->isAnonymous()) {
200 $links['comment-forbidden']['title'] = $this->commentManager->forbiddenMessage($commented_entity, $entity->getFieldName());
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'),
213 '#theme' => 'links__comment__comment',
214 // The "entity" property is specified to be present, so no need to check.
216 '#attributes' => ['class' => ['links', 'inline']],
221 * Wraps content_translation_translate_access.
223 protected function access(EntityInterface $entity) {
224 return content_translation_translate_access($entity);