Chris@0: 'entity.manager']; Chris@18: Chris@18: /** Chris@18: * The entity type manager service. Chris@0: * Chris@18: * @var \Drupal\Core\Entity\EntityTypeManagerInterface Chris@0: */ Chris@18: protected $entityTypeManager; Chris@0: Chris@0: /** Chris@0: * The entity form builder service. Chris@0: * Chris@0: * @var \Drupal\Core\Entity\EntityFormBuilderInterface Chris@0: */ Chris@0: protected $entityFormBuilder; Chris@0: Chris@0: /** Chris@0: * Comment manager service. Chris@0: * Chris@0: * @var \Drupal\comment\CommentManagerInterface Chris@0: */ Chris@0: protected $commentManager; Chris@0: Chris@0: /** Chris@0: * Current logged in user. Chris@0: * Chris@0: * @var \Drupal\Core\Session\AccountInterface Chris@0: */ Chris@0: protected $currentUser; Chris@0: Chris@0: /** Chris@0: * The module handler service. Chris@0: * Chris@0: * @var \Drupal\Core\Extension\ModuleHandlerInterface Chris@0: */ Chris@0: protected $moduleHandler; Chris@0: Chris@0: /** Chris@0: * The renderer service. Chris@0: * Chris@0: * @var \Drupal\Core\Render\RendererInterface Chris@0: */ Chris@0: protected $renderer; Chris@0: Chris@0: /** Chris@0: * Constructs a new CommentLazyBuilders object. Chris@0: * Chris@18: * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager Chris@18: * The entity type manager service. Chris@0: * @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder Chris@0: * The entity form builder service. Chris@0: * @param \Drupal\Core\Session\AccountInterface $current_user Chris@0: * The current logged in user. Chris@0: * @param \Drupal\comment\CommentManagerInterface $comment_manager Chris@0: * The comment manager service. Chris@0: * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler Chris@0: * The module handler service. Chris@0: * @param \Drupal\Core\Render\RendererInterface $renderer Chris@0: * The renderer service. Chris@0: */ Chris@18: public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFormBuilderInterface $entity_form_builder, AccountInterface $current_user, CommentManagerInterface $comment_manager, ModuleHandlerInterface $module_handler, RendererInterface $renderer) { Chris@18: $this->entityTypeManager = $entity_type_manager; Chris@0: $this->entityFormBuilder = $entity_form_builder; Chris@0: $this->currentUser = $current_user; Chris@0: $this->commentManager = $comment_manager; Chris@0: $this->moduleHandler = $module_handler; Chris@0: $this->renderer = $renderer; Chris@0: } Chris@0: Chris@0: /** Chris@0: * #lazy_builder callback; builds the comment form. Chris@0: * Chris@0: * @param string $commented_entity_type_id Chris@0: * The commented entity type ID. Chris@0: * @param string $commented_entity_id Chris@0: * The commented entity ID. Chris@0: * @param string $field_name Chris@0: * The comment field name. Chris@0: * @param string $comment_type_id Chris@0: * The comment type ID. Chris@0: * Chris@0: * @return array Chris@0: * A renderable array containing the comment form. Chris@0: */ Chris@0: public function renderForm($commented_entity_type_id, $commented_entity_id, $field_name, $comment_type_id) { Chris@0: $values = [ Chris@0: 'entity_type' => $commented_entity_type_id, Chris@0: 'entity_id' => $commented_entity_id, Chris@0: 'field_name' => $field_name, Chris@0: 'comment_type' => $comment_type_id, Chris@0: 'pid' => NULL, Chris@0: ]; Chris@18: $comment = $this->entityTypeManager->getStorage('comment')->create($values); Chris@0: return $this->entityFormBuilder->getForm($comment); Chris@0: } Chris@0: Chris@0: /** Chris@0: * #lazy_builder callback; builds a comment's links. Chris@0: * Chris@0: * @param string $comment_entity_id Chris@0: * The comment entity ID. Chris@0: * @param string $view_mode Chris@0: * The view mode in which the comment entity is being viewed. Chris@0: * @param string $langcode Chris@0: * The language in which the comment entity is being viewed. Chris@0: * @param bool $is_in_preview Chris@0: * Whether the comment is currently being previewed. Chris@0: * Chris@0: * @return array Chris@0: * A renderable array representing the comment links. Chris@0: */ Chris@0: public function renderLinks($comment_entity_id, $view_mode, $langcode, $is_in_preview) { Chris@0: $links = [ Chris@0: '#theme' => 'links__comment', Chris@0: '#pre_render' => ['drupal_pre_render_links'], Chris@0: '#attributes' => ['class' => ['links', 'inline']], Chris@0: ]; Chris@0: Chris@0: if (!$is_in_preview) { Chris@0: /** @var \Drupal\comment\CommentInterface $entity */ Chris@18: $entity = $this->entityTypeManager->getStorage('comment')->load($comment_entity_id); Chris@0: $commented_entity = $entity->getCommentedEntity(); Chris@0: Chris@0: $links['comment'] = $this->buildLinks($entity, $commented_entity); Chris@0: Chris@0: // Allow other modules to alter the comment links. Chris@0: $hook_context = [ Chris@0: 'view_mode' => $view_mode, Chris@0: 'langcode' => $langcode, Chris@0: 'commented_entity' => $commented_entity, Chris@0: ]; Chris@0: $this->moduleHandler->alter('comment_links', $links, $entity, $hook_context); Chris@0: } Chris@0: return $links; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Build the default links (reply, edit, delete …) for a comment. Chris@0: * Chris@0: * @param \Drupal\comment\CommentInterface $entity Chris@0: * The comment object. Chris@0: * @param \Drupal\Core\Entity\EntityInterface $commented_entity Chris@0: * The entity to which the comment is attached. Chris@0: * Chris@0: * @return array Chris@0: * An array that can be processed by drupal_pre_render_links(). Chris@0: */ Chris@0: protected function buildLinks(CommentInterface $entity, EntityInterface $commented_entity) { Chris@0: $links = []; Chris@0: $status = $commented_entity->get($entity->getFieldName())->status; Chris@0: Chris@0: if ($status == CommentItemInterface::OPEN) { Chris@0: if ($entity->access('delete')) { Chris@0: $links['comment-delete'] = [ Chris@0: 'title' => t('Delete'), Chris@18: 'url' => $entity->toUrl('delete-form'), Chris@0: ]; Chris@0: } Chris@0: Chris@0: if ($entity->access('update')) { Chris@0: $links['comment-edit'] = [ Chris@0: 'title' => t('Edit'), Chris@18: 'url' => $entity->toUrl('edit-form'), Chris@0: ]; Chris@0: } Chris@0: if ($entity->access('create')) { Chris@0: $links['comment-reply'] = [ Chris@0: 'title' => t('Reply'), Chris@0: 'url' => Url::fromRoute('comment.reply', [ Chris@0: 'entity_type' => $entity->getCommentedEntityTypeId(), Chris@0: 'entity' => $entity->getCommentedEntityId(), Chris@0: 'field_name' => $entity->getFieldName(), Chris@0: 'pid' => $entity->id(), Chris@0: ]), Chris@0: ]; Chris@0: } Chris@0: if (!$entity->isPublished() && $entity->access('approve')) { Chris@0: $links['comment-approve'] = [ Chris@0: 'title' => t('Approve'), Chris@0: 'url' => Url::fromRoute('comment.approve', ['comment' => $entity->id()]), Chris@0: ]; Chris@0: } Chris@0: if (empty($links) && $this->currentUser->isAnonymous()) { Chris@0: $links['comment-forbidden']['title'] = $this->commentManager->forbiddenMessage($commented_entity, $entity->getFieldName()); Chris@0: } Chris@0: } Chris@0: Chris@0: // Add translations link for translation-enabled comment bundles. Chris@0: if ($this->moduleHandler->moduleExists('content_translation') && $this->access($entity)->isAllowed()) { Chris@0: $links['comment-translations'] = [ Chris@0: 'title' => t('Translate'), Chris@18: 'url' => $entity->toUrl('drupal:content-translation-overview'), Chris@0: ]; Chris@0: } Chris@0: Chris@0: return [ Chris@0: '#theme' => 'links__comment__comment', Chris@0: // The "entity" property is specified to be present, so no need to check. Chris@0: '#links' => $links, Chris@0: '#attributes' => ['class' => ['links', 'inline']], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Wraps content_translation_translate_access. Chris@0: */ Chris@0: protected function access(EntityInterface $entity) { Chris@0: return content_translation_translate_access($entity); Chris@0: } Chris@0: Chris@0: }