You are here

function content_sync_entity_update in Content Synchronization 3.0.x

Same name and namespace in other branches
  1. 8.2 content_sync.module \content_sync_entity_update()
  2. 8 content_sync.module \content_sync_entity_update()

Implements hook_entity_update().

Keep the content snapshot table synced.

1 call to content_sync_entity_update()
content_sync_entity_insert in ./content_sync.module
Implements hook_entity_insert().

File

./content_sync.module, line 204
Allows site administrators to modify content.

Code

function content_sync_entity_update(EntityInterface $entity) {

  // Get submitted values.
  $entity_type = $entity
    ->getEntityTypeId();
  $entity_bundle = $entity
    ->bundle();
  $entity_id = $entity
    ->id();

  // Validate that it is a Content Entity.
  $entityTypeManager = \Drupal::entityTypeManager();
  $instances = $entityTypeManager
    ->getDefinitions();
  if (isset($instances[$entity_type]) && $instances[$entity_type] instanceof ContentEntityType) {
    $entity = \Drupal::entityTypeManager()
      ->getStorage($entity_type)
      ->load($entity_id);

    // Generate the YAML file.
    $serializer_context = [];
    $exported_entity = \Drupal::service('content_sync.exporter')
      ->exportEntity($entity, $serializer_context);

    // Create the name.
    $name = $entity_type . "." . $entity_bundle . "." . $entity
      ->uuid();

    // Insert/Update Data.
    $activeStorage = new ContentDatabaseStorage(\Drupal::database(), 'cs_db_snapshot');
    $activeStorage
      ->cs_write($name, Yaml::decode($exported_entity), $entity_type . "." . $entity_bundle);

    // Invalidate the CS Cache of the entity.
    $cache = \Drupal::cache('content')
      ->invalidate($entity_type . "." . $entity_bundle . ":" . $name);
  }
}