class AcsfDuplicationScrubCommentHandler in Acquia Cloud Site Factory Connector 8
Same name and namespace in other branches
- 8.2 src/Event/AcsfDuplicationScrubCommentHandler.php \Drupal\acsf\Event\AcsfDuplicationScrubCommentHandler
Handles the scrubbing of Drupal comments.
Hierarchy
- class \Drupal\acsf\Event\AcsfEventHandler
- class \Drupal\acsf\Event\AcsfDuplicationScrubEntityHandler
- class \Drupal\acsf\Event\AcsfDuplicationScrubCommentHandler
- class \Drupal\acsf\Event\AcsfDuplicationScrubEntityHandler
Expanded class hierarchy of AcsfDuplicationScrubCommentHandler
2 files declare their use of AcsfDuplicationScrubCommentHandler
- acsf_duplication.api.php in acsf_duplication/
acsf_duplication.api.php - Documents hooks provided by the ACSF Duplication module.
- acsf_duplication.drush.inc in acsf_duplication/
acsf_duplication.drush.inc - Provides drush commands necessary for site duplication.
File
- src/
Event/ AcsfDuplicationScrubCommentHandler.php, line 8
Namespace
Drupal\acsf\EventView source
class AcsfDuplicationScrubCommentHandler extends AcsfDuplicationScrubEntityHandler {
/**
* Constructor.
*
* @param AcsfEvent $event
* The event that has been initiated.
*/
public function __construct(AcsfEvent $event) {
$this->entityTypeId = 'comment';
parent::__construct($event);
}
/**
* Implements AcsfEventHandler::handle().
*/
public function handle() {
$options = $this->event->context['scrub_options'];
if ($options['retain_content'] || !\Drupal::moduleHandler()
->moduleExists('comment')) {
// We still want to log that we were here.
drush_print(dt('Entered @class', [
'@class' => get_class($this),
]));
return;
}
// If we're using the standard comment storage handler, replace it with
// a handler that prefers scrubbing over consistency (i.e. in case of
// exceptions thrown, loads things anyway).
$original_class = get_class($this->entityTypeManager
->getStorage($this->entityTypeId));
if ($original_class === 'Drupal\\comment\\CommentStorage') {
// $this->entityTypeManager->handlers contains a.o. the current storage
// class instance which we want to change for another one. We can only do
// this through EntityTypeManager::clearCachedDefinitions() which
// clears:
// - handlers (all class instances per handler type / entity type);
// - definitions (the full entity type (plugin) definitions, including
// the one in the cache backend).
// We don't want to invalidate the cached definitions, but oh well...
$this->entityTypeManager
->clearCachedDefinitions();
// Now we want to set the (temporary) new class. For this there's only a
// method in the entity type, not in the manager. The (plugin / definition
// class for the) entity type will always be regenerated before setting
// its storage handler, because all definitions were cleared.
$this->entityTypeManager
->getDefinition($this->entityTypeId)
->setStorageClass('Drupal\\acsf\\Event\\AcsfDuplicationScrubCommentStorage');
// Also: try to load/delete orphaned comments by ID (not by loading the
// entities) in a custom method.
$limit = $options['batch_' . $this->entityTypeId];
if ($options['avoid_oom']) {
$var_name = 'acsf_duplication_scrubbed_' . $this->entityTypeId;
$max_id = $this->acsfVarStorage
->get($var_name, -1);
$this->entityTypeManager
->getStorage($this->entityTypeId)
->deleteOrphanedItems($limit, $max_id);
}
else {
// If 'avoid_oom' is not set, we should delete all orphaned comments
// now. (The limit is basically only to keep the SQL from becoming too
// long; processing isn't hugely expensive.)
do {
$orphaned_ids = $this->entityTypeManager
->getStorage($this->entityTypeId)
->deleteOrphanedItems($limit);
} while ($orphaned_ids);
}
}
parent::handle();
// Clean up after ourselves.
if ($original_class === 'Drupal\\comment\\CommentStorage') {
$this->entityTypeManager
->clearCachedDefinitions();
$this->entityTypeManager
->getDefinition($this->entityTypeId)
->setStorageClass($original_class);
}
}
/**
* {@inheritdoc}
*/
public function countRemaining() {
if (!\Drupal::moduleHandler()
->moduleExists('comment')) {
return 0;
}
return parent::countRemaining();
}
}