CommentController::getReplyForm

public CommentController::getReplyForm(Request $request, EntityInterface $entity, $field_name, $pid = NULL)

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

File

core/modules/comment/src/Controller/CommentController.php, line 212

Class

CommentController
Controller for the comment entity.

Namespace

Drupal\comment\Controller

Code

public function getReplyForm(Request $request, EntityInterface $entity, $field_name, $pid = NULL) {
  $account = $this->currentUser();
  $build = array();

  // 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->entityManager()->getStorage('comment')->load($pid);
      // Display the parent comment.
      $build['comment_parent'] = $this->entityManager()->getViewBuilder('comment')->view($comment);
    }

    // The comment is in response to a entity.
    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->entityManager()->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->entityManager()->getStorage('comment')->create(array(
    'entity_id' => $entity->id(),
    'pid' => $pid,
    'entity_type' => $entity->getEntityTypeId(),
    'field_name' => $field_name,
  ));
  $build['comment_form'] = $this->entityFormBuilder()->getForm($comment);

  return $build;
}
doc_Drupal
2016-10-29 08:50:33
Comments
Leave a Comment

Please login to continue.