3

I have a template intended for administrators (separate from the Craft control panel) where I'd like to generate tokenized preview links for unpublished posts (the same URL you get when you click on the "View" button from an entry page within the control panel). These links are intended for administrators to share with third parties so they can preview a page before it's published. Here's the Twig I have so far (which isn't working):

<a href="{{ element.url }}?token={{ craft.app.tokens.createToken() }}">Preview</a>

What am I missing?

3

I figured this out:

{% set token = craft.app.tokens.createToken(["preview/preview", {"elementType": "craft\\elements\\Entry", "sourceId": 123, "siteId": 1}]) %}
<a href="{{ element.url }}?token={{ token }}">Preview</a>

Make sure sourceId and siteId are set correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.