Loading drush.services.yml +1 −0 Original line number Diff line number Diff line services: entitySanitizer.commands: class: Drupal\entity_sanitizer\Commands\EntitySanitizerCommands arguments: ['@entity_sanitizer.sanitizer', '@config.factory', '@entity_type.manager'] tags: - { name: drush.command } example.whitelist.yml +1 −1 Original line number Diff line number Diff line Loading @@ -22,7 +22,7 @@ whitelist: product: - price # Determine if the replacement string is in a human readable format. # Determine if the replacement string is in a human-readable format. # False to disable. configuration: human_readble: true src/Commands/EntitySanitizerCommands.php +54 −16 Original line number Diff line number Diff line Loading @@ -2,6 +2,9 @@ namespace Drupal\entity_sanitizer\Commands; use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\entity_sanitizer\Sanitizer; use Drush\Commands\DrushCommands; use Drupal\entity_sanitizer\UnsupportedFieldTypeException; use Drupal\Core\Entity\Sql\SqlEntityStorageInterface; Loading @@ -13,6 +16,44 @@ use Drupal\Core\Entity\Sql\SqlEntityStorageInterface; */ class EntitySanitizerCommands extends DrushCommands { /** * The config factory service. * * @var \Drupal\Core\Config\ConfigFactory */ protected ConfigFactory $configFactory; /** * The sanitizer service. * * @var \Drupal\entity_sanitizer\Sanitizer */ protected Sanitizer $sanitizer; /** * Entity type manager. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected EntityTypeManagerInterface $entityTypeManager; /** * Constructs a EntitySanitizerCommands object. * * @param \Drupal\entity_sanitizer\Sanitizer $sanitizer * The sanitizer service. * @param \Drupal\Core\Config\ConfigFactory $config_factory * The config factory service. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager service. */ public function __construct(Sanitizer $sanitizer, ConfigFactory $config_factory, EntityTypeManagerInterface $entity_type_manager) { parent::__construct(); $this->sanitizer = $sanitizer; $this->configFactory = $config_factory; $this->entityTypeManager = $entity_type_manager; } /** * Sanitize entity data. * Loading @@ -24,15 +65,12 @@ class EntitySanitizerCommands extends DrushCommands { * Sanitizes all the non-whitelisted entities. */ public function sanitizeNonWhitelistEntity() { /** @var \Drupal\entity_sanitizer\Sanitizer $sanitizer */ $sanitizer = \Drupal::service('entity_sanitizer.sanitizer'); // Get the whitelist configuration. $whitelist = \Drupal::config('entity_sanitizer.whitelist')->get(); $whitelist = $this->configFactory->get('entity_sanitizer.whitelist')->get('whitelist'); // We retrieve the storage definitions for all fields stored in a database. // This automatically excludes any computed fields. $entities = $sanitizer->getAllEntityFieldDefinitions(); $entities = $this->sanitizer->getAllEntityFieldDefinitions(); // Our array with sanitizing operations. $sanitize_orders = []; Loading @@ -52,7 +90,7 @@ class EntitySanitizerCommands extends DrushCommands { } /** @var \Drupal\Core\Entity\Sql\SqlEntityStorageInterface $entity_storage */ $entity_storage = \Drupal::service('entity_type.manager')->getStorage($entity_id); $entity_storage = $this->entityTypeManager->getStorage($entity_id); // We only know how to handle fields stored in SQL. if (!$entity_storage instanceof SqlEntityStorageInterface) { Loading Loading @@ -111,7 +149,7 @@ class EntitySanitizerCommands extends DrushCommands { } foreach ($revision_table_types as $revision_table) { $table_name = $sanitizer->generateFieldTableName($definition, $revision_table); $table_name = $this->sanitizer->generateFieldTableName($definition, $revision_table); // Create an order to sanitize this table. if (!isset($sanitize_orders[$table_name])) { Loading Loading @@ -143,7 +181,7 @@ class EntitySanitizerCommands extends DrushCommands { foreach ($sanitize_orders as $table_name => $order) { try { $queries[] = $sanitizer->generateSanitizeQuery($table_name, $order); $queries[] = $this->sanitizer->generateSanitizeQuery($table_name, $order); } catch (UnsupportedFieldTypeException $exception) { $field_type = $exception->getFieldType(); Loading Loading @@ -178,14 +216,14 @@ class EntitySanitizerCommands extends DrushCommands { } } foreach ($entities as $entity_id => $fields) { foreach ($fields as $field_name => $definition) { // @todo Create a query for each field individually to update the value // based on the field type. // @todo Add a WHERE condition to the UPDATE query to exclude whitelisted // bundle/field combinations. } } // foreach ($entities as $entity_id => $fields) { // foreach ($fields as $field_name => $definition) { // } // } // @todo need add clearing cache drupal_flush_all_caches(); provides errors. echo PHP_EOL; Loading src/Sanitizer.php +45 −48 Original line number Diff line number Diff line Loading @@ -233,12 +233,6 @@ class Sanitizer { */ protected function getValuesForField(string $table_name, string $field_name, string $field_type, FieldStorageDefinitionInterface $definition): array { $fields = []; // Get the whitelist configuration. $configurations = $this->configFactory->get('entity_sanitizer.whitelist')->get('configuration'); if ($configurations !== NULL) { $columns = $definition->getColumns(); switch ($field_type) { // For text fields we sanitize the textual value. case 'text_with_summary': Loading @@ -253,8 +247,12 @@ class Sanitizer { case 'address': case 'geolocation': try { // Get the whitelist configuration. $configurations = $this->configFactory->get('entity_sanitizer.whitelist')->get('configuration'); $columns = $definition->getColumns(); return $this->fieldSanitizerManager ->createInstance($field_type, $configurations) ->createInstance($field_type, $configurations ?? []) ->getFieldValues($table_name, $field_name, $columns); } catch (PluginNotFoundException $e) { Loading Loading @@ -283,7 +281,6 @@ class Sanitizer { default: throw new UnsupportedFieldTypeException($field_type, $field_name); } } return $fields; } Loading Loading
drush.services.yml +1 −0 Original line number Diff line number Diff line services: entitySanitizer.commands: class: Drupal\entity_sanitizer\Commands\EntitySanitizerCommands arguments: ['@entity_sanitizer.sanitizer', '@config.factory', '@entity_type.manager'] tags: - { name: drush.command }
example.whitelist.yml +1 −1 Original line number Diff line number Diff line Loading @@ -22,7 +22,7 @@ whitelist: product: - price # Determine if the replacement string is in a human readable format. # Determine if the replacement string is in a human-readable format. # False to disable. configuration: human_readble: true
src/Commands/EntitySanitizerCommands.php +54 −16 Original line number Diff line number Diff line Loading @@ -2,6 +2,9 @@ namespace Drupal\entity_sanitizer\Commands; use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\entity_sanitizer\Sanitizer; use Drush\Commands\DrushCommands; use Drupal\entity_sanitizer\UnsupportedFieldTypeException; use Drupal\Core\Entity\Sql\SqlEntityStorageInterface; Loading @@ -13,6 +16,44 @@ use Drupal\Core\Entity\Sql\SqlEntityStorageInterface; */ class EntitySanitizerCommands extends DrushCommands { /** * The config factory service. * * @var \Drupal\Core\Config\ConfigFactory */ protected ConfigFactory $configFactory; /** * The sanitizer service. * * @var \Drupal\entity_sanitizer\Sanitizer */ protected Sanitizer $sanitizer; /** * Entity type manager. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected EntityTypeManagerInterface $entityTypeManager; /** * Constructs a EntitySanitizerCommands object. * * @param \Drupal\entity_sanitizer\Sanitizer $sanitizer * The sanitizer service. * @param \Drupal\Core\Config\ConfigFactory $config_factory * The config factory service. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager service. */ public function __construct(Sanitizer $sanitizer, ConfigFactory $config_factory, EntityTypeManagerInterface $entity_type_manager) { parent::__construct(); $this->sanitizer = $sanitizer; $this->configFactory = $config_factory; $this->entityTypeManager = $entity_type_manager; } /** * Sanitize entity data. * Loading @@ -24,15 +65,12 @@ class EntitySanitizerCommands extends DrushCommands { * Sanitizes all the non-whitelisted entities. */ public function sanitizeNonWhitelistEntity() { /** @var \Drupal\entity_sanitizer\Sanitizer $sanitizer */ $sanitizer = \Drupal::service('entity_sanitizer.sanitizer'); // Get the whitelist configuration. $whitelist = \Drupal::config('entity_sanitizer.whitelist')->get(); $whitelist = $this->configFactory->get('entity_sanitizer.whitelist')->get('whitelist'); // We retrieve the storage definitions for all fields stored in a database. // This automatically excludes any computed fields. $entities = $sanitizer->getAllEntityFieldDefinitions(); $entities = $this->sanitizer->getAllEntityFieldDefinitions(); // Our array with sanitizing operations. $sanitize_orders = []; Loading @@ -52,7 +90,7 @@ class EntitySanitizerCommands extends DrushCommands { } /** @var \Drupal\Core\Entity\Sql\SqlEntityStorageInterface $entity_storage */ $entity_storage = \Drupal::service('entity_type.manager')->getStorage($entity_id); $entity_storage = $this->entityTypeManager->getStorage($entity_id); // We only know how to handle fields stored in SQL. if (!$entity_storage instanceof SqlEntityStorageInterface) { Loading Loading @@ -111,7 +149,7 @@ class EntitySanitizerCommands extends DrushCommands { } foreach ($revision_table_types as $revision_table) { $table_name = $sanitizer->generateFieldTableName($definition, $revision_table); $table_name = $this->sanitizer->generateFieldTableName($definition, $revision_table); // Create an order to sanitize this table. if (!isset($sanitize_orders[$table_name])) { Loading Loading @@ -143,7 +181,7 @@ class EntitySanitizerCommands extends DrushCommands { foreach ($sanitize_orders as $table_name => $order) { try { $queries[] = $sanitizer->generateSanitizeQuery($table_name, $order); $queries[] = $this->sanitizer->generateSanitizeQuery($table_name, $order); } catch (UnsupportedFieldTypeException $exception) { $field_type = $exception->getFieldType(); Loading Loading @@ -178,14 +216,14 @@ class EntitySanitizerCommands extends DrushCommands { } } foreach ($entities as $entity_id => $fields) { foreach ($fields as $field_name => $definition) { // @todo Create a query for each field individually to update the value // based on the field type. // @todo Add a WHERE condition to the UPDATE query to exclude whitelisted // bundle/field combinations. } } // foreach ($entities as $entity_id => $fields) { // foreach ($fields as $field_name => $definition) { // } // } // @todo need add clearing cache drupal_flush_all_caches(); provides errors. echo PHP_EOL; Loading
src/Sanitizer.php +45 −48 Original line number Diff line number Diff line Loading @@ -233,12 +233,6 @@ class Sanitizer { */ protected function getValuesForField(string $table_name, string $field_name, string $field_type, FieldStorageDefinitionInterface $definition): array { $fields = []; // Get the whitelist configuration. $configurations = $this->configFactory->get('entity_sanitizer.whitelist')->get('configuration'); if ($configurations !== NULL) { $columns = $definition->getColumns(); switch ($field_type) { // For text fields we sanitize the textual value. case 'text_with_summary': Loading @@ -253,8 +247,12 @@ class Sanitizer { case 'address': case 'geolocation': try { // Get the whitelist configuration. $configurations = $this->configFactory->get('entity_sanitizer.whitelist')->get('configuration'); $columns = $definition->getColumns(); return $this->fieldSanitizerManager ->createInstance($field_type, $configurations) ->createInstance($field_type, $configurations ?? []) ->getFieldValues($table_name, $field_name, $columns); } catch (PluginNotFoundException $e) { Loading Loading @@ -283,7 +281,6 @@ class Sanitizer { default: throw new UnsupportedFieldTypeException($field_type, $field_name); } } return $fields; } Loading