Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/comment/src/CommentForm.php @ 4:a9cd425dd02b
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:11:55 +0000 |
parents | c75dbcec494b |
children | 12f9dff5fda9 |
comparison
equal
deleted
inserted
replaced
3:307d7a7fd348 | 4:a9cd425dd02b |
---|---|
7 use Drupal\Component\Utility\Html; | 7 use Drupal\Component\Utility\Html; |
8 use Drupal\Component\Utility\Unicode; | 8 use Drupal\Component\Utility\Unicode; |
9 use Drupal\Core\Datetime\DrupalDateTime; | 9 use Drupal\Core\Datetime\DrupalDateTime; |
10 use Drupal\Core\Entity\ContentEntityForm; | 10 use Drupal\Core\Entity\ContentEntityForm; |
11 use Drupal\Core\Entity\EntityConstraintViolationListInterface; | 11 use Drupal\Core\Entity\EntityConstraintViolationListInterface; |
12 use Drupal\Core\Entity\EntityManagerInterface; | 12 use Drupal\Core\Entity\EntityFieldManagerInterface; |
13 use Drupal\Core\Entity\EntityRepositoryInterface; | |
13 use Drupal\Core\Entity\EntityTypeBundleInfoInterface; | 14 use Drupal\Core\Entity\EntityTypeBundleInfoInterface; |
14 use Drupal\Core\Form\FormStateInterface; | 15 use Drupal\Core\Form\FormStateInterface; |
15 use Drupal\Core\Render\RendererInterface; | 16 use Drupal\Core\Render\RendererInterface; |
16 use Drupal\Core\Session\AccountInterface; | 17 use Drupal\Core\Session\AccountInterface; |
17 use Symfony\Component\DependencyInjection\ContainerInterface; | 18 use Symfony\Component\DependencyInjection\ContainerInterface; |
36 * @var \Drupal\Core\Render\RendererInterface | 37 * @var \Drupal\Core\Render\RendererInterface |
37 */ | 38 */ |
38 protected $renderer; | 39 protected $renderer; |
39 | 40 |
40 /** | 41 /** |
42 * The entity field manager. | |
43 * | |
44 * @var \Drupal\Core\Entity\EntityFieldManagerInterface | |
45 */ | |
46 protected $entityFieldManager; | |
47 | |
48 /** | |
41 * {@inheritdoc} | 49 * {@inheritdoc} |
42 */ | 50 */ |
43 public static function create(ContainerInterface $container) { | 51 public static function create(ContainerInterface $container) { |
44 return new static( | 52 return new static( |
45 $container->get('entity.manager'), | 53 $container->get('entity.repository'), |
46 $container->get('current_user'), | 54 $container->get('current_user'), |
47 $container->get('renderer'), | 55 $container->get('renderer'), |
48 $container->get('entity_type.bundle.info'), | 56 $container->get('entity_type.bundle.info'), |
49 $container->get('datetime.time') | 57 $container->get('datetime.time'), |
58 $container->get('entity_field.manager') | |
50 ); | 59 ); |
51 } | 60 } |
52 | 61 |
53 /** | 62 /** |
54 * Constructs a new CommentForm. | 63 * Constructs a new CommentForm. |
55 * | 64 * |
56 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager | 65 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository |
57 * The entity manager service. | 66 * The entity repository. |
58 * @param \Drupal\Core\Session\AccountInterface $current_user | 67 * @param \Drupal\Core\Session\AccountInterface $current_user |
59 * The current user. | 68 * The current user. |
60 * @param \Drupal\Core\Render\RendererInterface $renderer | 69 * @param \Drupal\Core\Render\RendererInterface $renderer |
61 * The renderer. | 70 * The renderer. |
62 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info | 71 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info |
63 * The entity type bundle service. | 72 * The entity type bundle service. |
64 * @param \Drupal\Component\Datetime\TimeInterface $time | 73 * @param \Drupal\Component\Datetime\TimeInterface $time |
65 * The time service. | 74 * The time service. |
66 */ | 75 */ |
67 public function __construct(EntityManagerInterface $entity_manager, AccountInterface $current_user, RendererInterface $renderer, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) { | 76 public function __construct(EntityRepositoryInterface $entity_repository, AccountInterface $current_user, RendererInterface $renderer, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, EntityFieldManagerInterface $entity_field_manager = NULL) { |
68 parent::__construct($entity_manager, $entity_type_bundle_info, $time); | 77 parent::__construct($entity_repository, $entity_type_bundle_info, $time); |
69 $this->currentUser = $current_user; | 78 $this->currentUser = $current_user; |
70 $this->renderer = $renderer; | 79 $this->renderer = $renderer; |
80 $this->entityFieldManager = $entity_field_manager ?: \Drupal::service('entity_field.manager'); | |
71 } | 81 } |
72 | 82 |
73 /** | 83 /** |
74 * {@inheritdoc} | 84 * {@inheritdoc} |
75 */ | 85 */ |
76 public function form(array $form, FormStateInterface $form_state) { | 86 public function form(array $form, FormStateInterface $form_state) { |
77 /** @var \Drupal\comment\CommentInterface $comment */ | 87 /** @var \Drupal\comment\CommentInterface $comment */ |
78 $comment = $this->entity; | 88 $comment = $this->entity; |
79 $entity = $this->entityManager->getStorage($comment->getCommentedEntityTypeId())->load($comment->getCommentedEntityId()); | 89 $entity = $this->entityTypeManager->getStorage($comment->getCommentedEntityTypeId())->load($comment->getCommentedEntityId()); |
80 $field_name = $comment->getFieldName(); | 90 $field_name = $comment->getFieldName(); |
81 $field_definition = $this->entityManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()]; | 91 $field_definition = $this->entityFieldManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()]; |
82 $config = $this->config('user.settings'); | 92 $config = $this->config('user.settings'); |
83 | 93 |
84 // In several places within this function, we vary $form on: | 94 // In several places within this function, we vary $form on: |
85 // - The current user's permissions. | 95 // - The current user's permissions. |
86 // - Whether the current user is authenticated or anonymous. | 96 // - Whether the current user is authenticated or anonymous. |
367 $form_state->setValue('cid', $comment->id()); | 377 $form_state->setValue('cid', $comment->id()); |
368 | 378 |
369 // Add a log entry. | 379 // Add a log entry. |
370 $logger->notice('Comment posted: %subject.', [ | 380 $logger->notice('Comment posted: %subject.', [ |
371 '%subject' => $comment->getSubject(), | 381 '%subject' => $comment->getSubject(), |
372 'link' => $this->l(t('View'), $comment->urlInfo()->setOption('fragment', 'comment-' . $comment->id())) | 382 'link' => $this->l(t('View'), $comment->urlInfo()->setOption('fragment', 'comment-' . $comment->id())), |
373 ]); | 383 ]); |
374 | 384 |
375 // Explain the approval queue if necessary. | 385 // Explain the approval queue if necessary. |
376 if (!$comment->isPublished()) { | 386 if (!$comment->isPublished()) { |
377 if (!$this->currentUser->hasPermission('administer comments')) { | 387 if (!$this->currentUser->hasPermission('administer comments')) { |
378 drupal_set_message($this->t('Your comment has been queued for review by site administrators and will be published after approval.')); | 388 $this->messenger()->addStatus($this->t('Your comment has been queued for review by site administrators and will be published after approval.')); |
379 } | 389 } |
380 } | 390 } |
381 else { | 391 else { |
382 drupal_set_message($this->t('Your comment has been posted.')); | 392 $this->messenger()->addStatus($this->t('Your comment has been posted.')); |
383 } | 393 } |
384 $query = []; | 394 $query = []; |
385 // Find the current display page for this comment. | 395 // Find the current display page for this comment. |
386 $field_definition = $this->entityManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$field_name]; | 396 $field_definition = $this->entityFieldManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$field_name]; |
387 $page = $this->entityManager->getStorage('comment')->getDisplayOrdinal($comment, $field_definition->getSetting('default_mode'), $field_definition->getSetting('per_page')); | 397 $page = $this->entityTypeManager->getStorage('comment')->getDisplayOrdinal($comment, $field_definition->getSetting('default_mode'), $field_definition->getSetting('per_page')); |
388 if ($page > 0) { | 398 if ($page > 0) { |
389 $query['page'] = $page; | 399 $query['page'] = $page; |
390 } | 400 } |
391 // Redirect to the newly posted comment. | 401 // Redirect to the newly posted comment. |
392 $uri->setOption('query', $query); | 402 $uri->setOption('query', $query); |
393 $uri->setOption('fragment', 'comment-' . $comment->id()); | 403 $uri->setOption('fragment', 'comment-' . $comment->id()); |
394 } | 404 } |
395 else { | 405 else { |
396 $logger->warning('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', ['%subject' => $comment->getSubject()]); | 406 $logger->warning('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', ['%subject' => $comment->getSubject()]); |
397 drupal_set_message($this->t('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', ['%subject' => $comment->getSubject()]), 'error'); | 407 $this->messenger()->addError($this->t('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', ['%subject' => $comment->getSubject()])); |
398 // Redirect the user to the entity they are commenting on. | 408 // Redirect the user to the entity they are commenting on. |
399 } | 409 } |
400 $form_state->setRedirectUrl($uri); | 410 $form_state->setRedirectUrl($uri); |
401 } | 411 } |
402 | 412 |