From c0b74d509a50e81562c8a944105845aa6957b277 Mon Sep 17 00:00:00 2001
From: Michael Mwebaze <mwebaze@outlook.com>
Date: Thu, 28 Jul 2022 23:31:56 -0400
Subject: [PATCH] Updated to support different entities

---
 integer_to_decimal.info.yml                  |  4 +-
 integer_to_decimal.services.yml              |  2 +-
 src/Service/FieldUpdaterService.php          | 45 ++++++++------------
 src/Service/FieldUpdaterServiceInterface.php | 14 ++++--
 4 files changed, 31 insertions(+), 34 deletions(-)

diff --git a/integer_to_decimal.info.yml b/integer_to_decimal.info.yml
index e638d32..ba3c79d 100644
--- a/integer_to_decimal.info.yml
+++ b/integer_to_decimal.info.yml
@@ -2,6 +2,6 @@ name: Field Updater
 type: module
 description: 'Changes field type numeric with data to field type decimal.'
 package: Other
-core_version_requirement: ^8 || ^9
+core_version_requirement: ^8 || ^9 || ^10
 dependencies:
-  - node
+  - drupal:node
diff --git a/integer_to_decimal.services.yml b/integer_to_decimal.services.yml
index a000d9a..d1687f2 100644
--- a/integer_to_decimal.services.yml
+++ b/integer_to_decimal.services.yml
@@ -1,4 +1,4 @@
 services:
   field_updater:
     class: Drupal\integer_to_decimal\Service\FieldUpdaterService
-    arguments: ['@database', '@entity_type.manager', '@config.factory', '@messenger']
\ No newline at end of file
+    arguments: ['@database', '@entity_type.manager', '@config.factory', '@messenger', '@entity_field.manager']
diff --git a/src/Service/FieldUpdaterService.php b/src/Service/FieldUpdaterService.php
index a3efd54..f283b45 100644
--- a/src/Service/FieldUpdaterService.php
+++ b/src/Service/FieldUpdaterService.php
@@ -2,6 +2,7 @@
 
 namespace Drupal\integer_to_decimal\Service;
 
+use Drupal\Core\Entity\EntityFieldManagerInterface;
 use Drupal\Core\Messenger\MessengerInterface;
 use Drupal\Core\StringTranslation\StringTranslationTrait;
 use Drupal\field\Entity\FieldConfig;
@@ -18,40 +19,28 @@ use Drupal\Core\Config\ConfigFactoryInterface;
  */
 class FieldUpdaterService implements FieldUpdaterServiceInterface {
   use StringTranslationTrait;
-
-  /**
-   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
-   */
-  protected $entityTypeManager;
-
-  /**
-   * @var \Drupal\Core\Database\Connection
-   */
-  protected $connection;
-
-  /**
-   * @var \Drupal\Core\Config\ConfigFactoryInterface
-   */
-  protected $configFactory;
-  /**
-   * @var \Drupal\Core\Messenger\MessengerInterface $messenger
-   */
-  protected $messenger;
+  protected EntityTypeManagerInterface $entityTypeManager;
+  protected Connection $connection;
+  protected ConfigFactoryInterface $configFactory;
+  protected MessengerInterface $messenger;
+  protected EntityFieldManagerInterface $entityFieldManager;
 
   /**
    * FieldUpdaterService constructor.
    *
-   * @param \Drupal\Core\Database\Connection $connection
-   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
-   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
-   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
+   * @param Connection $connection
+   * @param EntityTypeManagerInterface $entityTypeManager
+   * @param ConfigFactoryInterface $configFactory
+   * @param MessengerInterface $messenger
+   * @param EntityFieldManagerInterface $entityFieldManager
    */
   public function __construct(Connection $connection, EntityTypeManagerInterface $entityTypeManager,
-                              ConfigFactoryInterface $configFactory, MessengerInterface $messenger) {
+                              ConfigFactoryInterface $configFactory, MessengerInterface $messenger, EntityFieldManagerInterface $entityFieldManager) {
     $this->connection = $connection;
     $this->entityTypeManager = $entityTypeManager;
     $this->configFactory = $configFactory;
     $this->messenger = $messenger;
+    $this->entityFieldManager = $entityFieldManager;
   }
 
   /**
@@ -62,11 +51,11 @@ class FieldUpdaterService implements FieldUpdaterServiceInterface {
     $database = $this->connection;
     $existingData = [];
     $tables = [];
-
-    $entity_type_manager = \Drupal::getContainer()->get('entity_type.manager');
+    //$entity_type_manager = \Drupal::getContainer()->get('entity_type.manager');
+    //$entity_field_manager = \Drupal::getContainer()->get('entity_field.manager');
     // Get an instance of Drupal\Core\Entity\Sql\DefaultTableMapping class
-    $table_mapping = $entity_type_manager->getStorage('node')->getTableMapping();
-    $storage_definitions = $entity_type_manager->getFieldStorageDefinitions($entity_type);
+    $table_mapping = $this->entityTypeManager->getStorage($entity_type)->getTableMapping();
+    $storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($entity_type);
     $data_table = $table_mapping->getDedicatedDataTableName($storage_definitions[$field]);
     if ($data_table) {
       $tables[] = $data_table;
diff --git a/src/Service/FieldUpdaterServiceInterface.php b/src/Service/FieldUpdaterServiceInterface.php
index 27860f6..8e7a2bf 100644
--- a/src/Service/FieldUpdaterServiceInterface.php
+++ b/src/Service/FieldUpdaterServiceInterface.php
@@ -2,6 +2,8 @@
 
 namespace Drupal\integer_to_decimal\Service;
 
+use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
+
 /**
  * provides an interface for Field Updater services.
  *
@@ -17,14 +19,20 @@ interface FieldUpdaterServiceInterface
    * @param string $type
    * Field type such as integer, decimal
    *
+   * @param $entity_type
+   * The entity machine name
+   *
    * @param string $bundle
    * The bundle to which the converted field is associated with
    *
-   * @param integer $precision precision associated with decimal field type
+   * @param integer $precision
+   * precision associated with decimal field type
+   *
+   * @param integer $scale
+   * scale associated with decimal field type
    *
-   *@param integer $scale scale associated with decimal field type
    *
-   * @return mixed
+   * @throws InvalidPluginDefinitionException
    */
     public function fieldUpdater($field, $type, $entity_type, $bundle, $precision, $scale);
 }
-- 
GitLab