Created
July 3, 2018 23:18
-
-
Save heyrocker/9b977ba10e0a4696a17dac1149164286 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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