diff core/modules/comment/src/CommentManager.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents c75dbcec494b
children
line wrap: on
line diff
--- a/core/modules/comment/src/CommentManager.php	Thu Feb 28 13:11:55 2019 +0000
+++ b/core/modules/comment/src/CommentManager.php	Thu May 09 15:34:47 2019 +0100
@@ -4,32 +4,46 @@
 
 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
 use Drupal\Core\Config\ConfigFactoryInterface;
+use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
+use Drupal\Core\Entity\EntityFieldManagerInterface;
 use Drupal\Core\Entity\EntityInterface;
-use Drupal\Core\Entity\EntityManagerInterface;
+use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Entity\FieldableEntityInterface;
 use Drupal\Core\Extension\ModuleHandlerInterface;
-use Drupal\Core\Routing\UrlGeneratorInterface;
-use Drupal\Core\Routing\UrlGeneratorTrait;
 use Drupal\Core\Session\AccountInterface;
 use Drupal\Core\StringTranslation\StringTranslationTrait;
 use Drupal\Core\StringTranslation\TranslationInterface;
+use Drupal\Core\Url;
 use Drupal\field\Entity\FieldStorageConfig;
 use Drupal\field\Entity\FieldConfig;
 use Drupal\user\RoleInterface;
+use Drupal\user\UserInterface;
 
 /**
  * Comment manager contains common functions to manage comment fields.
  */
 class CommentManager implements CommentManagerInterface {
   use StringTranslationTrait;
-  use UrlGeneratorTrait;
+  use DeprecatedServicePropertyTrait;
 
   /**
-   * The entity manager service.
+   * {@inheritdoc}
+   */
+  protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
+
+  /**
+   * The entity field manager.
    *
-   * @var \Drupal\Core\Entity\EntityManagerInterface
+   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
    */
-  protected $entityManager;
+  protected $entityFieldManager;
+
+  /**
+   * The entity type manager.
+   *
+   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
+   */
+  protected $entityTypeManager;
 
   /**
    * Whether the \Drupal\user\RoleInterface::AUTHENTICATED_ID can post comments.
@@ -62,38 +76,42 @@
   /**
    * Construct the CommentManager object.
    *
-   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
-   *   The entity manager service.
+   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
+   *   The entity type manager service.
    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
    *   The config factory.
    * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
    *   The string translation service.
-   * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
-   *   The url generator service.
    *  @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
    *   The module handler service.
    * @param \Drupal\Core\Session\AccountInterface $current_user
    *   The current user.
+   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
+   *   The entity field manager service.
    */
-  public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, UrlGeneratorInterface $url_generator, ModuleHandlerInterface $module_handler, AccountInterface $current_user) {
-    $this->entityManager = $entity_manager;
+  public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler, AccountInterface $current_user, EntityFieldManagerInterface $entity_field_manager = NULL) {
+    $this->entityTypeManager = $entity_type_manager;
     $this->userConfig = $config_factory->get('user.settings');
     $this->stringTranslation = $string_translation;
-    $this->urlGenerator = $url_generator;
     $this->moduleHandler = $module_handler;
     $this->currentUser = $current_user;
+    if (!$entity_field_manager) {
+      @trigger_error('The entity_field.manager service must be passed to CommentManager::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
+      $entity_field_manager = \Drupal::service('entity_field.manager');
+    }
+    $this->entityFieldManager = $entity_field_manager;
   }
 
   /**
    * {@inheritdoc}
    */
   public function getFields($entity_type_id) {
-    $entity_type = $this->entityManager->getDefinition($entity_type_id);
+    $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
     if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) {
       return [];
     }
 
-    $map = $this->entityManager->getFieldMapByFieldType('comment');
+    $map = $this->entityFieldManager->getFieldMapByFieldType('comment');
     return isset($map[$entity_type_id]) ? $map[$entity_type_id] : [];
   }
 
@@ -103,7 +121,7 @@
   public function addBodyField($comment_type_id) {
     if (!FieldConfig::loadByName('comment', $comment_type_id, 'comment_body')) {
       // Attaches the body field by default.
-      $field = $this->entityManager->getStorage('field_config')->create([
+      $field = $this->entityTypeManager->getStorage('field_config')->create([
         'label' => 'Comment',
         'bundle' => $comment_type_id,
         'required' => TRUE,
@@ -136,7 +154,7 @@
     if (!isset($this->authenticatedCanPostComments)) {
       // We only output a link if we are certain that users will get the
       // permission to post comments by logging in.
-      $this->authenticatedCanPostComments = $this->entityManager
+      $this->authenticatedCanPostComments = $this->entityTypeManager
         ->getStorage('user_role')
         ->load(RoleInterface::AUTHENTICATED_ID)
         ->hasPermission('post comments');
@@ -151,23 +169,23 @@
           'entity' => $entity->id(),
           'field_name' => $field_name,
         ];
-        $destination = ['destination' => $this->url('comment.reply', $comment_reply_parameters, ['fragment' => 'comment-form'])];
+        $destination = ['destination' => Url::fromRoute('comment.reply', $comment_reply_parameters, ['fragment' => 'comment-form'])->toString()];
       }
       else {
-        $destination = ['destination' => $entity->url('canonical', ['fragment' => 'comment-form'])];
+        $destination = ['destination' => $entity->toUrl('canonical', ['fragment' => 'comment-form'])->toString()];
       }
 
-      if ($this->userConfig->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) {
+      if ($this->userConfig->get('register') != UserInterface::REGISTER_ADMINISTRATORS_ONLY) {
         // Users can register themselves.
         return $this->t('<a href=":login">Log in</a> or <a href=":register">register</a> to post comments', [
-          ':login' => $this->urlGenerator->generateFromRoute('user.login', [], ['query' => $destination]),
-          ':register' => $this->urlGenerator->generateFromRoute('user.register', [], ['query' => $destination]),
+          ':login' => Url::fromRoute('user.login', [], ['query' => $destination])->toString(),
+          ':register' => Url::fromRoute('user.register', [], ['query' => $destination])->toString(),
         ]);
       }
       else {
         // Only admins can add new users, no public registration.
         return $this->t('<a href=":login">Log in</a> to post comments', [
-          ':login' => $this->urlGenerator->generateFromRoute('user.login', [], ['query' => $destination]),
+          ':login' => Url::fromRoute('user.login', [], ['query' => $destination])->toString(),
         ]);
       }
     }
@@ -201,7 +219,7 @@
       $timestamp = ($timestamp > HISTORY_READ_LIMIT ? $timestamp : HISTORY_READ_LIMIT);
 
       // Use the timestamp to retrieve the number of new comments.
-      $query = $this->entityManager->getStorage('comment')->getQuery()
+      $query = $this->entityTypeManager->getStorage('comment')->getQuery()
         ->condition('entity_type', $entity->getEntityTypeId())
         ->condition('entity_id', $entity->id())
         ->condition('created', $timestamp, '>')