Skip to content
Snippets Groups Projects
Commit c0b74d50 authored by Michael Mwebaze's avatar Michael Mwebaze
Browse files

Updated to support different entities

parent 2e5de477
No related branches found
No related tags found
No related merge requests found
......@@ -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
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']
......@@ -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;
......
......@@ -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);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment