Skip to content

Ensure additional preview URLs work on non-variant content#14995

Merged
elit0451 merged 1 commit intov12/devfrom
v12/fix/additional-preview-urls-for-non-variant-content
Oct 18, 2023
Merged

Ensure additional preview URLs work on non-variant content#14995
elit0451 merged 1 commit intov12/devfrom
v12/fix/additional-preview-urls-for-non-variant-content

Conversation

@kjac
Copy link
Contributor

@kjac kjac commented Oct 18, 2023

Prerequisites

  • I have added steps to test this contribution in the description below

Description

#14773 added UI for the "additional preview URLs" feature implemented in #14479. Unfortunately, the "additional preview URLs" UI currently does not work with culture invariant content.

This PR fixes that issue. It is purely a client-side issue which has to do with the active variant election. See the code change 😄

Testing this PR

Try adding a SendingContentNotification notification handler like the following, and ensure that the generated additional preview URLs are displayed for both variant and invariant content.

using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.PublishedCache;

namespace Umbraco.Cms.Web.UI.Custom;

public class AdditionalPreviewUrlsNotificationHandler : INotificationHandler<SendingContentNotification>
{
    private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor;

    public AdditionalPreviewUrlsNotificationHandler(IPublishedSnapshotAccessor publishedSnapshotAccessor)
        => _publishedSnapshotAccessor = publishedSnapshotAccessor;

    public void Handle(SendingContentNotification notification)
    {
        foreach (ContentVariantDisplay variantDisplay in notification.Content.Variants.Where(variant => variant.PublishDate.HasValue))
        {
            IPublishedSnapshot publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
            var route = publishedSnapshot.Content?.GetRouteById(true, notification.Content.Id, variantDisplay.Language?.IsoCode);
            if (route == null)
            {
                continue;
            }

            route = route.TrimStart('/');

            variantDisplay.AdditionalPreviewUrls = new[]
            {
                new NamedUrl
                {
                    Name = $"Environment One{(variantDisplay.Language != null ? $" ({variantDisplay.Language.Name})" : null)}",
                    Url = $"https://environment.one/{route}?culture={variantDisplay.Language?.IsoCode}"
                },
                new NamedUrl
                {
                    Name = $"Environment Two{(variantDisplay.Language != null ? $" ({variantDisplay.Language.Name})" : null)}",
                    Url = $"https://environment.two/{route}?culture={variantDisplay.Language?.IsoCode}"
                }
            };
        }
    }
}

public class AdditionalPreviewUrlsNotificationHandlerComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
        => builder.AddNotificationHandler<SendingContentNotification, AdditionalPreviewUrlsNotificationHandler>();
}

@elit0451 elit0451 merged commit 6845086 into v12/dev Oct 18, 2023
@elit0451 elit0451 deleted the v12/fix/additional-preview-urls-for-non-variant-content branch October 18, 2023 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants