You are here

public function GatsbyEntityLogger::logEntity in Gatsby Live Preview & Incremental Builds 2.0.x

Same name and namespace in other branches
  1. 8 modules/gatsby_fastbuilds/src/GatsbyEntityLogger.php \Drupal\gatsby_fastbuilds\GatsbyEntityLogger::logEntity()

Logs an entity create, update, or delete.

Parameters

Drupal\Core\Entity\ContentEntityInterface $entity: The entity to log the details for.

string $action: The action for this entity (insert, update, or delete).

File

modules/gatsby_fastbuilds/src/GatsbyEntityLogger.php, line 56

Class

GatsbyEntityLogger
Defines a service for logging content entity changes using log entities.

Namespace

Drupal\gatsby_fastbuilds

Code

public function logEntity(ContentEntityInterface $entity, string $action) {
  $this
    ->deleteLoggedEntity($entity
    ->uuid(), $entity
    ->language()
    ->getId());
  $json = [];
  if ($action !== 'delete') {
    $json = $this->gatsbyInstantPreview
      ->getJson($entity);
    if (!$json) {
      return;
    }
  }
  $json['id'] = $entity
    ->uuid();
  $json['action'] = $action;
  $json['langcode'] = $entity
    ->language()
    ->getId();

  // We keep langcode in both spots to help with backwards compatibility.
  $json['attributes'] = [
    'langcode' => $entity
      ->language()
      ->getId(),
    'drupal_internal__revision_id' => $entity
      ->getRevisionId(),
  ];
  $log_entry = [
    'entity_uuid' => $entity
      ->uuid(),
    'title' => $entity
      ->label(),
    'entity' => $entity
      ->getEntityTypeId(),
    'bundle' => $entity
      ->bundle(),
    'langcode' => $entity
      ->language()
      ->getId(),
    'action' => $action,
    'json' => json_encode($json),
  ];
  $log = $this->entityTypeManager
    ->getStorage('gatsby_log_entity')
    ->create($log_entry);
  $log
    ->save();
}