Skip to content

Instantly share code, notes, and snippets.

@GuyPaddock GuyPaddock/resurrect.php
Last active Sep 18, 2019

Embed
What would you like to do?
Resurrecting an Unsaved Drupal 8 Node from the Database using Devel
<?php
// NOTE: Node previews expire after 7 days. Be sure to use this method to extract the desired data before then.
// IMPORTANT: Set this to the user ID of the person who was creating/lost the node.
$user_id = 1;
// IMPORTANT: Set this to the UUID that appeared in the node preview URL.
// This value is from a URL that looked like this:
// https://my-site/node/preview/ed4eaac0-3c32-427e-860f-6ce8fc79c3b2/full
$preview_id = 'ed4eaac0-3c32-427e-860f-6ce8fc79c3b2';
$user = user_load($user_id);
$account_proxy = new \Drupal\Core\Session\AccountProxy();
$account_proxy->setAccount($user);
$temp_store = new Drupal\Core\TempStore\PrivateTempStoreFactory(
\Drupal::service('keyvalue.expirable'),
\Drupal::service('lock'),
$account_proxy,
\Drupal::service('request_stack'),
604800
);
$preview_converter = new \Drupal\node\ParamConverter\NodePreviewConverter($temp_store);
$preview = $preview_converter->convert($preview_id, NULL, NULL, []);
$preview_array = (array)$preview;
// The values of the node form are under a key called "*values".
dpm($preview_array);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.