Skip to content
Snippets Groups Projects

Resolve #2559833 "Comment form should redirect"

5 files
+ 118
4
Compare changes
  • Side-by-side
  • Inline
Files
5
@@ -12,9 +12,11 @@
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -57,7 +59,9 @@ public static function create(ContainerInterface $container) {
$container->get('renderer'),
$container->get('entity_type.bundle.info'),
$container->get('datetime.time'),
$container->get('entity_field.manager')
$container->get('entity_field.manager'),
$container->get('current_route_match'),
$container->get('entity_type.manager')
);
}
@@ -76,6 +80,10 @@ public static function create(ContainerInterface $container) {
* The time service.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface|null $entity_field_manager
* (optional) The entity field manager service.
* @param \Drupal\Core\Routing\RouteMatchInterface $current_route_match
* The current route match.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(
EntityRepositoryInterface $entity_repository,
@@ -84,6 +92,8 @@ public function __construct(
EntityTypeBundleInfoInterface $entity_type_bundle_info,
TimeInterface $time,
EntityFieldManagerInterface $entity_field_manager = NULL,
protected RouteMatchInterface $current_route_match,
protected EntityTypeManagerInterface $entity_type_manager
) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->currentUser = $current_user;
@@ -126,6 +136,15 @@ public function form(array $form, FormStateInterface $form_state) {
// If not replying to a comment, use our dedicated page callback for new
// Comments on entities.
$entity_url = $entity->toUrl();
if ($entity_url->getRouteName() === $this->current_route_match->getRouteName() &&
$entity_url->getRouteParameters() == $this->current_route_match->getRawParameters()) {
$form['#action'] = Url::fromRoute('comment.reply', [
'entity_type' => $entity->getEntityTypeId(),
'entity' => $entity->id(),
'field_name' => $field_name,
])->toString();
}
if (!$comment->id() && !$comment->hasParentComment()) {
$form['#action'] = Url::fromRoute('comment.reply', ['entity_type' => $entity->getEntityTypeId(), 'entity' => $entity->id(), 'field_name' => $field_name])->toString();
}
@@ -418,7 +437,11 @@ public function save(array $form, FormStateInterface $form_state) {
$this->messenger()->addError($this->t('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', ['%subject' => $comment->getSubject()]));
// Redirect the user to the entity they are commenting on.
}
$form_state->setRedirectUrl($uri);
$entity_url = $entity->toUrl();
if ($entity_url->getRouteName() === $this->current_route_match->getRouteName() &&
$entity_url->getRouteParameters() === $this->current_route_match->getRawParameters()) {
$form_state->setRedirectUrl($uri);
}
}
}
Loading