Skip to content

Instantly share code, notes, and snippets.

@heyrocker
Created July 3, 2018 23:18
Show Gist options
  • Save heyrocker/9b977ba10e0a4696a17dac1149164286 to your computer and use it in GitHub Desktop.
Save heyrocker/9b977ba10e0a4696a17dac1149164286 to your computer and use it in GitHub Desktop.
function dartmouth_server_entity_load(array $entities, $entity_type_id) {
if ($entity_type_id == "node") {
$request = \Drupal::request();
$controller = $request->get("_controller");
$staging = $request->query->get("staging");
if ($staging == "TRUE" && strpos($controller, "jsonapi") !== false) {
// If we are previewing…
foreach ($entities as $entity) {
// Get the nid of the current entity
$nid = $entity->get('nid')->getValue()[0]['value'];
// Get the latest revision of the current entity
$result = \Drupal::entityTypeManager()->getStorage('node')->getQuery()
->latestRevision()
->condition('nid', $nid, '=')
->execute();
// Extract its ID
$revision_id = key($result);
// And load that revision to be passed on for serialization
$entity = \Drupal::entityTypeManager()->getStorage('node')->loadRevision($revision_id);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment