S3StorageHandlerModuleDependencyCollector.php in Acquia Content Hub 8.2
File
modules/acquia_contenthub_s3/src/EventSubscriber/DependencyCollector/S3StorageHandlerModuleDependencyCollector.php
View source
<?php
namespace Drupal\acquia_contenthub_s3\EventSubscriber\DependencyCollector;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\depcalc\DependencyCalculatorEvents;
use Drupal\depcalc\Event\CalculateEntityDependenciesEvent;
use Drupal\depcalc\EventSubscriber\DependencyCollector\BaseDependencyCollector;
class S3StorageHandlerModuleDependencyCollector extends BaseDependencyCollector {
protected $requiredS3Modules = [
's3fs',
];
protected $moduleHandler;
public function __construct(ModuleHandlerInterface $moduleHandler) {
$this->moduleHandler = $moduleHandler;
}
public static function getSubscribedEvents() {
$events[DependencyCalculatorEvents::CALCULATE_DEPENDENCIES][] = [
'onCalculateDependencies',
];
return $events;
}
public function onCalculateDependencies(CalculateEntityDependenciesEvent $event) {
$entity = $event
->getEntity();
if ($entity
->getEntityTypeId() !== 'field_storage_config') {
return;
}
$uri_scheme = $entity
->getSetting('uri_scheme');
if (!$uri_scheme || $uri_scheme !== 's3') {
return;
}
$modules = $this
->getS3ModuleDependencies();
if (empty($modules)) {
return;
}
$modules = array_merge($event
->getModuleDependencies(), $modules);
$event
->setModuleDependencies($modules);
}
protected function getS3ModuleDependencies() : array {
$modules = [];
foreach ($this->requiredS3Modules as $module) {
if (!$this->moduleHandler
->moduleExists($module)) {
continue;
}
$modules[] = $module;
}
return $modules;
}
}