You are here

public function ConfigEntityWithNullUuid::onPreEntitySave in Acquia Content Hub 8.2

Saves a configuration entity to assign the UUID of an imported entity.

Parameters

\Drupal\acquia_contenthub\Event\PreEntitySaveEvent $event: The pre entity save event.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

modules/acquia_contenthub_subscriber/src/EventSubscriber/PreEntitySave/ConfigEntityWithNullUuid.php, line 47

Class

ConfigEntityWithNullUuid
Handles config entities with null uuids.

Namespace

Drupal\acquia_contenthub_subscriber\EventSubscriber\PreEntitySave

Code

public function onPreEntitySave(PreEntitySaveEvent $event) {
  $entity = $event
    ->getEntity();
  if ($entity instanceof ConfigEntityInterface) {
    $local_entity = $this->entityTypeManager
      ->getStorage($entity
      ->getEntityTypeId())
      ->load($entity
      ->id());
    if ($local_entity && !$local_entity
      ->uuid()) {
      $config_id = $entity
        ->getConfigDependencyName();
      $config = \Drupal::configFactory()
        ->getEditable($config_id);
      $config
        ->set('uuid', $event
        ->getCdf()
        ->getUuid());
      $config
        ->save();
      $entity
        ->set('uuid', $event
        ->getCdf()
        ->getUuid());
    }
  }
}