AcquiaContentHubConfigNullUuidsFix.php in Acquia Content Hub 8.2
File
modules/acquia_contenthub_site_health/src/Commands/AcquiaContentHubConfigNullUuidsFix.php
View source
<?php
namespace Drupal\acquia_contenthub_site_health\Commands;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\Entity\ConfigEntityType;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drush\Commands\DrushCommands;
class AcquiaContentHubConfigNullUuidsFix extends DrushCommands {
protected $entityTypeManager;
protected $configFactory;
protected $moduleHandler;
protected $uuidGenerator;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, UuidInterface $uuidGenerator) {
$this->entityTypeManager = $entity_type_manager;
$this->configFactory = $config_factory;
$this->moduleHandler = $module_handler;
$this->uuidGenerator = $uuidGenerator;
}
public function fixConfigEntitiesWithNullUuids() {
$this
->output()
->writeln(sprintf("Checking Drupal configuration entities for compatibility with Content Hub...\n\n"));
if (!$this->moduleHandler
->moduleExists('acquia_contenthub_publisher')) {
throw new \Exception("This command should only be run on a publisher site.");
}
$missing_uuid_count = 0;
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type) {
if (!$entity_type instanceof ConfigEntityType) {
continue;
}
$entity_type_id = $entity_type
->id();
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
$entities = $storage
->loadMultiple();
$missing_uuid_count = 0;
foreach ($entities as $entity) {
if (!$entity
->uuid()) {
$missing_uuid_count++;
$config_id = $entity
->getConfigDependencyName();
$config = $this->configFactory
->getEditable($config_id);
$config
->set('uuid', $this->uuidGenerator
->generate());
$config
->save();
$entity = $storage
->load($entity
->id());
$this
->output()
->writeln(sprintf("Entity type: %s, Entity id: %s, Entity uuid: %s\n", $entity_type
->id(), $entity
->id(), $entity
->uuid()));
}
}
}
if ($missing_uuid_count === 0) {
$this
->output()
->writeln(sprintf("\n\nAll Drupal configuration entities have proper UUIDs."));
}
}
}