Skip to content

Commit 438bc12

Browse files
mikecpbszyman
andauthored
Issue 7426 (#7438)
* Text review * Adapt to "not availbale anymore" * Putting the warning first * Apply suggestions from code review Co-authored-by: Ben Szymanski <bszyman@icloud.com> * Adding link to Extension documentation. --------- Co-authored-by: Ben Szymanski <bszyman@icloud.com>
1 parent 54f5399 commit 438bc12

File tree

1 file changed

+8
-98
lines changed

1 file changed

+8
-98
lines changed

16/umbraco-cms/reference/content-delivery-api/additional-preview-environments-support.md

Lines changed: 8 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -6,110 +6,20 @@ description: >-
66
# Additional preview environments support
77

88
{% hint style="warning" %}
9-
The contents of this article have not yet been verified or updated for Umbraco 15.
9+
The original contents of this article, explaining how to change the backoffice UI, does not apply anymore to Umbraco 15 and above and has been removed.
10+
The way to achieve this beyond Umbraco 14 is to create and register an [Extension](../../customizing/extending-overview) to modify the backoffice UI preview button.
1011
{% endhint %}
1112

12-
With Umbraco, you can save and preview draft content before going live. The preview feature allows you to visualize how a page will look once it is published, directly from within the backoffice. This is also possible for the Content Delivery API data. You can extend the preview functionality in the backoffice by configuring external preview URLs for client libraries consuming the Content Delivery API.
13+
With Umbraco, you can save and preview draft content before going live. The preview feature allows you to visualize how a page will look like once it is published, directly from within the backoffice. This is also possible for the Content Delivery API data. You can extend the preview functionality in the backoffice by configuring external preview URLs for client libraries consuming the Content Delivery API.
1314

1415
{% hint style="info" %}
15-
To get familiar with the preview functionality in the Delivery API, please refer to the [Preview concept](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#preview) section.
16+
To get introduced to the preview functionality in the Content Delivery API, please refer to the [Preview concept](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#preview) section.
1617
{% endhint %}
1718

1819
{% hint style="info" %}
19-
The support for configuring additional preview environments in the Delivery API was introduced in version 12.3.
20+
Support for configuring additional preview environments in the Content Delivery API was introduced in version 12.3.
2021
{% endhint %}
2122

22-
## Configuring custom preview URLs
23-
24-
If your client libraries feature preview functionality, you can enable editors in Umbraco to navigate directly to their preferred preview environments. To achieve this, start by generating the necessary URLs for each environment you wish to allow for preview. These URLs need to trigger preview mode within your application, which will fetch and present draft content from the Delivery API.
25-
26-
Once you have these preview URLs, you will need to register them through code in Umbraco.
27-
28-
Additionally, there are plans to simplify this process further. In an upcoming major version of Umbraco, a UI will be introduced, allowing you to configure these custom preview URLs directly from the backoffice.
29-
30-
{% include "../../.gitbook/includes/obsolete-warning-ipublishedsnapshotaccessor.md" %}
31-
32-
Here is an example of how to register such preview URLs for both variant and invariant content using a notification handler:
33-
34-
{% code title="AdditionalPreviewUrlsNotificationHandler.cs" %}
35-
36-
```csharp
37-
using Umbraco.Cms.Core.Events;
38-
using Umbraco.Cms.Core.Models.ContentEditing;
39-
using Umbraco.Cms.Core.Notifications;
40-
using Umbraco.Cms.Core.PublishedCache;
41-
42-
namespace Umbraco.Docs.Samples;
43-
44-
public class AdditionalPreviewUrlsNotificationHandler : INotificationHandler<SendingContentNotification>
45-
{
46-
private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor;
47-
48-
public AdditionalPreviewUrlsNotificationHandler(IPublishedSnapshotAccessor publishedSnapshotAccessor)
49-
=> _publishedSnapshotAccessor = publishedSnapshotAccessor;
50-
51-
public void Handle(SendingContentNotification notification)
52-
{
53-
foreach (ContentVariantDisplay variantDisplay in notification.Content.Variants.Where(variant => variant.PublishDate.HasValue))
54-
{
55-
// Retrieve the route of each content item
56-
IPublishedSnapshot publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
57-
var route = publishedSnapshot.Content?.GetRouteById(true, notification.Content.Id, variantDisplay.Language?.IsoCode);
58-
if (route == null)
59-
{
60-
continue;
61-
}
62-
63-
route = route.TrimStart('/');
64-
65-
variantDisplay.AdditionalPreviewUrls = new[]
66-
{
67-
new NamedUrl
68-
{
69-
// Dynamically generate Preview URL
70-
Name = $"Development{(variantDisplay.Language != null ? $" ({variantDisplay.Language.Name})" : null)}",
71-
Url = $"https://dev.environment.org/{route}?culture={variantDisplay.Language?.IsoCode}&preview=true"
72-
},
73-
new NamedUrl
74-
{
75-
// Dynamically generate Preview URL
76-
Name = $"Staging{(variantDisplay.Language != null ? $" ({variantDisplay.Language.Name})" : null)}",
77-
Url = $"https://staging.environment.org/{route}?culture={variantDisplay.Language?.IsoCode}&preview=true"
78-
}
79-
};
80-
}
81-
}
82-
}
83-
```
84-
85-
{% endcode %}
86-
87-
The purpose of this notification handler is to dynamically generate additional preview URLs for published content items only (_for the sake of simplicity_). It constructs two custom preview URLs, one for a development environment and another for a staging environment. These URLs include the content's route, culture variant, and a `preview` query parameter to enable preview mode in the client application.
88-
89-
You can then register your notification handler in a composer like this:
90-
91-
{% code title="AdditionalPreviewUrlsNotificationHandlerComposer.cs" %}
92-
```csharp
93-
using Umbraco.Cms.Core.Composing;
94-
using Umbraco.Cms.Core.Notifications;
95-
96-
namespace Umbraco.Docs.Samples;
97-
98-
public class AdditionalPreviewUrlsNotificationHandlerComposer : IComposer
99-
{
100-
public void Compose(IUmbracoBuilder builder)
101-
=> builder.AddNotificationHandler<SendingContentNotification, AdditionalPreviewUrlsNotificationHandler>();
102-
}
103-
```
104-
{% endcode %}
105-
106-
## Accessing preview environments
107-
108-
Now that we have set up additional preview URLs for the Delivery API data, you can access them from the Content section. When you open a content node, you will see new preview options for the external environments you have configured. Next to the regular "Save and preview" button, there is an arrow for the multiple URLs that have been added. Click it to see all the available preview URLs, as shown below:
109-
110-
![Preview invariant content with Delivery API](images/preview-invariant-content.png)
111-
112-
Below is an example with variants, showcasing both the English and Danish versions of a content node.
113-
114-
![Preview English variant with Delivery API](images/preview-variant-content-en.png)
115-
![Preview Danish variant with Delivery API](images/preview-variant-content-da.png)
23+
{% hint style="info" %}
24+
An upcoming release of Umbraco will allow users to configure custom preview URLs directly in the backoffice.
25+
{% endhint %}

0 commit comments

Comments
 (0)