diff --git a/core/modules/comment/comment.services.yml b/core/modules/comment/comment.services.yml index 94540d1..8bc91b3 100644 --- a/core/modules/comment/comment.services.yml +++ b/core/modules/comment/comment.services.yml @@ -15,4 +15,4 @@ services: comment.post_render_cache: class: Drupal\comment\CommentPostRenderCache - arguments: ['@entity.manager', '@entity.form_builder'] + arguments: ['@entity.manager', '@entity.form_builder', '@current_user'] diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index 3b800b3..1580181 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -130,6 +130,11 @@ public function form(array $form, array &$form_state) { $date = !empty($comment->date) ? $comment->date : DrupalDateTime::createFromTimestamp($comment->getCreatedTime()); } + $form['uid'] = array( + '#type' => 'value', + '#value' => $comment->getOwnerId(), + ); + // Add the author name field depending on the current user. $form['name']['widget'][0]['value']['#required'] = ($this->currentUser->isAnonymous() && $anonymous_contact == COMMENT_ANONYMOUS_MUST_CONTACT); if ($is_admin) { @@ -237,7 +242,6 @@ protected function actions(array $form, array &$form_state) { * Overrides Drupal\Core\Entity\EntityForm::validate(). */ public function validate(array $form, array &$form_state) { - parent::validate($form, $form_state); $entity = $this->entity; if (!$entity->isNew()) { @@ -265,6 +269,7 @@ public function validate(array $form, array &$form_state) { } } } + parent::validate($form, $form_state); } /** diff --git a/core/modules/comment/src/CommentPostRenderCache.php b/core/modules/comment/src/CommentPostRenderCache.php index 15831e0..897328c 100644 --- a/core/modules/comment/src/CommentPostRenderCache.php +++ b/core/modules/comment/src/CommentPostRenderCache.php @@ -9,6 +9,7 @@ use Drupal\Core\Entity\EntityFormBuilderInterface; use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Session\AccountInterface; use Drupal\field\Entity\FieldConfig; /** @@ -31,16 +32,26 @@ class CommentPostRenderCache { protected $entityFormBuilder; /** + * The current user. + * + * @var \Drupal\Core\Session\AccountInterface + */ + protected $currentUser; + + /** * Constructs a new CommentPostRenderCache object. * * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager service. * @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder * The entity form builder service. + * @param \Drupal\Core\Session\AccountInterface $current_user + * The current user. */ - public function __construct(EntityManagerInterface $entity_manager, EntityFormBuilderInterface $entity_form_builder) { + public function __construct(EntityManagerInterface $entity_manager, EntityFormBuilderInterface $entity_form_builder, AccountInterface $current_user) { $this->entityManager = $entity_manager; $this->entityFormBuilder = $entity_form_builder; + $this->currentUser = $current_user; } /** @@ -67,6 +78,7 @@ public function renderForm(array $element, array $context) { 'field_name' => $field_name, 'comment_type' => $field->getSetting('bundle'), 'pid' => NULL, + 'uid' => $this->currentUser->id(), ); $comment = $this->entityManager->getStorage('comment')->create($values); $form = $this->entityFormBuilder->getForm($comment); diff --git a/core/modules/comment/src/Controller/CommentController.php b/core/modules/comment/src/Controller/CommentController.php index e550afc..2e4090b 100644 --- a/core/modules/comment/src/Controller/CommentController.php +++ b/core/modules/comment/src/Controller/CommentController.php @@ -256,6 +256,7 @@ public function getReplyForm(Request $request, $entity_type, $entity_id, $field_ 'pid' => $pid, 'entity_type' => $entity->getEntityTypeId(), 'field_name' => $field_name, + 'uid' => $this->currentUser()->id(), )); $build['comment_form'] = $this->entityFormBuilder()->getForm($comment); diff --git a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php index 4597761..2c5740a 100644 --- a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php +++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php @@ -192,6 +192,7 @@ public function viewElements(FieldItemListInterface $items) { 'field_name' => $field_name, 'comment_type' => $this->getFieldSetting('comment_type'), 'pid' => NULL, + 'uid' => $this->currentUser->id(), )); $output['comment_form'] = $this->entityFormBuilder->getForm($comment); }