class CommentController in Drupal 8
Same name and namespace in other branches
- 9 core/modules/comment/src/Controller/CommentController.php \Drupal\comment\Controller\CommentController
- 10 core/modules/comment/src/Controller/CommentController.php \Drupal\comment\Controller\CommentController
Controller for the comment entity.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\comment\Controller\CommentController
Expanded class hierarchy of CommentController
See also
\Drupal\comment\Entity\Comment.
File
- core/
modules/ comment/ src/ Controller/ CommentController.php, line 29
Namespace
Drupal\comment\ControllerView source
class CommentController extends ControllerBase {
/**
* The HTTP kernel.
*
* @var \Symfony\Component\HttpKernel\HttpKernelInterface
*/
protected $httpKernel;
/**
* The comment manager service.
*
* @var \Drupal\comment\CommentManagerInterface
*/
protected $commentManager;
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* The entity repository.
*
* @var Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Constructs a CommentController object.
*
* @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
* HTTP kernel to handle requests.
* @param \Drupal\comment\CommentManagerInterface $comment_manager
* The comment manager service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager service.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
*/
public function __construct(HttpKernelInterface $http_kernel, CommentManagerInterface $comment_manager, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager = NULL, EntityRepositoryInterface $entity_repository = NULL) {
$this->httpKernel = $http_kernel;
$this->commentManager = $comment_manager;
$this->entityTypeManager = $entity_type_manager;
if (!$entity_field_manager) {
@trigger_error('The entity_field.manager service must be passed to CommentController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
$entity_field_manager = \Drupal::service('entity_field.manager');
}
$this->entityFieldManager = $entity_field_manager;
if (!$entity_repository) {
@trigger_error('The entity.repository service must be passed to CommentController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
$entity_repository = \Drupal::service('entity.repository');
}
$this->entityRepository = $entity_repository;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('http_kernel'), $container
->get('comment.manager'), $container
->get('entity_type.manager'), $container
->get('entity_field.manager'), $container
->get('entity.repository'));
}
/**
* Publishes the specified comment.
*
* @param \Drupal\comment\CommentInterface $comment
* A comment entity.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function commentApprove(CommentInterface $comment) {
$comment
->setPublished();
$comment
->save();
$this
->messenger()
->addStatus($this
->t('Comment approved.'));
$permalink_uri = $comment
->permalink();
$permalink_uri
->setAbsolute();
return new RedirectResponse($permalink_uri
->toString());
}
/**
* Redirects comment links to the correct page depending on comment settings.
*
* Since comments are paged there is no way to guarantee which page a comment
* appears on. Comment paging and threading settings may be changed at any
* time. With threaded comments, an individual comment may move between pages
* as comments can be added either before or after it in the overall
* discussion. Therefore we use a central routing function for comment links,
* which calculates the page number based on current comment settings and
* returns the full comment view with the pager set dynamically.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request of the page.
* @param \Drupal\comment\CommentInterface $comment
* A comment entity.
*
* @return \Symfony\Component\HttpFoundation\Response
* The comment listing set to the page on which the comment appears.
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
* @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
*/
public function commentPermalink(Request $request, CommentInterface $comment) {
if ($entity = $comment
->getCommentedEntity()) {
// Check access permissions for the entity.
if (!$entity
->access('view')) {
throw new AccessDeniedHttpException();
}
$field_definition = $this->entityFieldManager
->getFieldDefinitions($entity
->getEntityTypeId(), $entity
->bundle())[$comment
->getFieldName()];
// Find the current display page for this comment.
$page = $this
->entityTypeManager()
->getStorage('comment')
->getDisplayOrdinal($comment, $field_definition
->getSetting('default_mode'), $field_definition
->getSetting('per_page'));
// @todo: Cleaner sub request handling.
$subrequest_url = $entity
->toUrl()
->setOption('query', [
'page' => $page,
])
->toString(TRUE);
$redirect_request = Request::create($subrequest_url
->getGeneratedUrl(), 'GET', $request->query
->all(), $request->cookies
->all(), [], $request->server
->all());
// Carry over the session to the subrequest.
if ($request
->hasSession()) {
$redirect_request
->setSession($request
->getSession());
}
$request->query
->set('page', $page);
$response = $this->httpKernel
->handle($redirect_request, HttpKernelInterface::SUB_REQUEST);
if ($response instanceof CacheableResponseInterface) {
// @todo Once path aliases have cache tags (see
// https://www.drupal.org/node/2480077), add test coverage that
// the cache tag for a commented entity's path alias is added to the
// comment's permalink response, because there can be blocks or
// other content whose renderings depend on the subrequest's URL.
$response
->addCacheableDependency($subrequest_url);
}
return $response;
}
throw new NotFoundHttpException();
}
/**
* The _title_callback for the page that renders the comment permalink.
*
* @param \Drupal\comment\CommentInterface $comment
* The current comment.
*
* @return string
* The translated comment subject.
*/
public function commentPermalinkTitle(CommentInterface $comment) {
return $this->entityRepository
->getTranslationFromContext($comment)
->label();
}
/**
* Redirects legacy node links to the new path.
*
* @param \Drupal\Core\Entity\EntityInterface $node
* The node object identified by the legacy URL.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* Redirects user to new url.
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function redirectNode(EntityInterface $node) {
$fields = $this->commentManager
->getFields('node');
// Legacy nodes only had a single comment field, so use the first comment
// field on the entity.
if (!empty($fields) && ($field_names = array_keys($fields)) && ($field_name = reset($field_names))) {
return $this
->redirect('comment.reply', [
'entity_type' => 'node',
'entity' => $node
->id(),
'field_name' => $field_name,
]);
}
throw new NotFoundHttpException();
}
/**
* Form constructor for the comment reply form.
*
* There are several cases that have to be handled, including:
* - replies to comments
* - replies to entities
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request object.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity this comment belongs to.
* @param string $field_name
* The field_name to which the comment belongs.
* @param int $pid
* (optional) Some comments are replies to other comments. In those cases,
* $pid is the parent comment's comment ID. Defaults to NULL.
*
* @return array|\Symfony\Component\HttpFoundation\RedirectResponse
* An associative array containing:
* - An array for rendering the entity or parent comment.
* - comment_entity: If the comment is a reply to the entity.
* - comment_parent: If the comment is a reply to another comment.
* - comment_form: The comment form as a renderable array.
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function getReplyForm(Request $request, EntityInterface $entity, $field_name, $pid = NULL) {
$account = $this
->currentUser();
$build = [];
// The user is not just previewing a comment.
if ($request->request
->get('op') != $this
->t('Preview')) {
// $pid indicates that this is a reply to a comment.
if ($pid) {
// Load the parent comment.
$comment = $this
->entityTypeManager()
->getStorage('comment')
->load($pid);
// Display the parent comment.
$build['comment_parent'] = $this
->entityTypeManager()
->getViewBuilder('comment')
->view($comment);
}
elseif ($entity
->access('view', $account)) {
// We make sure the field value isn't set so we don't end up with a
// redirect loop.
$entity = clone $entity;
$entity->{$field_name}->status = CommentItemInterface::HIDDEN;
// Render array of the entity full view mode.
$build['commented_entity'] = $this
->entityTypeManager()
->getViewBuilder($entity
->getEntityTypeId())
->view($entity, 'full');
unset($build['commented_entity']['#cache']);
}
}
else {
$build['#title'] = $this
->t('Preview comment');
}
// Show the actual reply box.
$comment = $this
->entityTypeManager()
->getStorage('comment')
->create([
'entity_id' => $entity
->id(),
'pid' => $pid,
'entity_type' => $entity
->getEntityTypeId(),
'field_name' => $field_name,
]);
$build['comment_form'] = $this
->entityFormBuilder()
->getForm($comment);
return $build;
}
/**
* Access check for the reply form.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity this comment belongs to.
* @param string $field_name
* The field_name to which the comment belongs.
* @param int $pid
* (optional) Some comments are replies to other comments. In those cases,
* $pid is the parent comment's comment ID. Defaults to NULL.
*
* @return \Drupal\Core\Access\AccessResultInterface
* An access result
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function replyFormAccess(EntityInterface $entity, $field_name, $pid = NULL) {
// Check if entity and field exists.
$fields = $this->commentManager
->getFields($entity
->getEntityTypeId());
if (empty($fields[$field_name])) {
throw new NotFoundHttpException();
}
$account = $this
->currentUser();
// Check if the user has the proper permissions.
$access = AccessResult::allowedIfHasPermission($account, 'post comments');
// If commenting is open on the entity.
$status = $entity->{$field_name}->status;
$access = $access
->andIf(AccessResult::allowedIf($status == CommentItemInterface::OPEN)
->addCacheableDependency($entity))
->andIf(AccessResult::allowedIf($entity
->access('view')));
// $pid indicates that this is a reply to a comment.
if ($pid) {
// Check if the user has the proper permissions.
$access = $access
->andIf(AccessResult::allowedIfHasPermission($account, 'access comments'));
// Load the parent comment.
$comment = $this
->entityTypeManager()
->getStorage('comment')
->load($pid);
// Check if the parent comment is published and belongs to the entity.
$access = $access
->andIf(AccessResult::allowedIf($comment && $comment
->isPublished() && $comment
->getCommentedEntityId() == $entity
->id()));
if ($comment) {
$access
->addCacheableDependency($comment);
}
}
return $access;
}
/**
* Returns a set of nodes' last read timestamps.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request of the page.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The JSON response.
*
* @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function renderNewCommentsNodeLinks(Request $request) {
if ($this
->currentUser()
->isAnonymous()) {
throw new AccessDeniedHttpException();
}
$nids = $request->request
->get('node_ids');
$field_name = $request->request
->get('field_name');
if (!isset($nids)) {
throw new NotFoundHttpException();
}
// Only handle up to 100 nodes.
$nids = array_slice($nids, 0, 100);
$links = [];
foreach ($nids as $nid) {
$node = $this
->entityTypeManager()
->getStorage('node')
->load($nid);
$new = $this->commentManager
->getCountNewComments($node);
$page_number = $this
->entityTypeManager()
->getStorage('comment')
->getNewCommentPageNumber($node->{$field_name}->comment_count, $new, $node, $field_name);
$query = $page_number ? [
'page' => $page_number,
] : NULL;
$links[$nid] = [
'new_comment_count' => (int) $new,
'first_new_comment_link' => Url::fromRoute('entity.node.canonical', [
'node' => $node
->id(),
], [
'query' => $query,
'fragment' => 'new',
])
->toString(),
];
}
return new JsonResponse($links);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CommentController:: |
protected | property | The comment manager service. | |
CommentController:: |
protected | property | The entity field manager. | |
CommentController:: |
protected | property | The entity repository. | |
CommentController:: |
protected | property | The HTTP kernel. | |
CommentController:: |
public | function | Publishes the specified comment. | |
CommentController:: |
public | function | Redirects comment links to the correct page depending on comment settings. | |
CommentController:: |
public | function | The _title_callback for the page that renders the comment permalink. | |
CommentController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
CommentController:: |
public | function | Form constructor for the comment reply form. | |
CommentController:: |
public | function | Redirects legacy node links to the new path. | |
CommentController:: |
public | function | Returns a set of nodes' last read timestamps. | |
CommentController:: |
public | function | Access check for the reply form. | |
CommentController:: |
public | function | Constructs a CommentController object. | |
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |