public function CommentController::getReplyForm in Drupal 10
Same name and namespace in other branches
- 8 core/modules/comment/src/Controller/CommentController.php \Drupal\comment\Controller\CommentController::getReplyForm()
- 9 core/modules/comment/src/Controller/CommentController.php \Drupal\comment\Controller\CommentController::getReplyForm()
Form constructor for the comment reply form.
There are several cases that have to be handled, including:
- replies to comments
- replies to entities
Parameters
\Symfony\Component\HttpFoundation\Request $request: The current request object.
\Drupal\Core\Entity\EntityInterface $entity: The entity this comment belongs to.
string $field_name: The field_name to which the comment belongs.
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 value
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
1 string reference to 'CommentController::getReplyForm'
- comment.routing.yml in core/
modules/ comment/ comment.routing.yml - core/modules/comment/comment.routing.yml
File
- core/
modules/ comment/ src/ Controller/ CommentController.php, line 230
Class
- CommentController
- Controller for the comment entity.
Namespace
Drupal\comment\ControllerCode
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;
}