Skip to content

Commit

Permalink
Allow setting up meta fields for content that isn't published yet (#244)
Browse files Browse the repository at this point in the history
* Allow setting up meta fields for content that isn't published yet

* Revert solution change
  • Loading branch information
patrickdemooij9 authored Oct 10, 2023
1 parent 468cee8 commit 105ce1f
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public SeoContentAppFactory(ISeoSettingsService seoSettingsService,

public ContentApp GetContentAppFor(object source, IEnumerable<IReadOnlyUserGroup> userGroups)
{
if (source is not IContent { HasIdentity: true } content || !_seoSettingsService.IsEnabled(content.ContentTypeId))
if (source is not IContent content || !_seoSettingsService.IsEnabled(content.ContentTypeId))
return null;

using var scope = _serviceScopeFactory.CreateScope();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public SeoContentController(IContentService contentService, SeoDisplayCollection
public IActionResult Get(int contentId)
{
var content = _contentService.GetById(contentId);
if (content is null) return NotFound();

return new JsonResult(new SeoContentViewModel
{
Displays = _seoDisplayCollection.Select(it => it.Get(content)).WhereNotNull().ToArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace SeoToolkit.Umbraco.Common.Core.Interfaces
{
public interface ISeoDisplayProvider
{
SeoDisplayViewModel Get(IContent content);
SeoDisplayViewModel Get(IContent? content);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function () {

function SeoContentController($scope, $http, editorState, eventsService, notificationsService) {
function SeoContentController($scope, $http, editorState, eventsService, notificationsService, contentAppHelper) {
var unsubscribe = [];

var vm = this;
Expand All @@ -21,14 +21,14 @@
}

function save() {
vm.defaultButtonScope.defaultButton.saveButtonState = "busy";
$scope.$broadcast("seoContentSubmitting");

notificationsService.success("SEO content saved!");
vm.defaultButtonScope.defaultButton.saveButtonState = "success";
}

function init() {
if (!contentAppHelper.CONTENT_BASED_APPS.includes("seoContent")) {
contentAppHelper.CONTENT_BASED_APPS.push("seoContent");
}
$http.get("backoffice/SeoToolkit/SeoContent/Get?contentId=" + editorState.getCurrent().id).then(
function (response) {
if (response.status === 200) {
Expand All @@ -38,39 +38,20 @@
});
}

function initSaveButton() {
const maxTries = 20;
var tries = 0;
var currentScope = $scope.$parent;
while (currentScope && !currentScope.hasOwnProperty("defaultButton") && tries <= maxTries) {
currentScope = currentScope.$parent;
tries++;
}

if (maxTries > tries) {
vm.defaultButtonScope = currentScope;
}

if (vm.defaultButtonScope) {
vm.defaultButtonScope.defaultButton = {
letter: 'S',
labelKey: "buttons_save",
handler: save,
hotKey: "ctrl+s",
hotKeyWhenHidden: true,
alias: "save",
addEllipsis: "true"
}
}
}

unsubscribe.push(eventsService.on("app.tabChange",
(e, data) => {
if (data.alias !== "seoContent") {
return;
}

initSaveButton();
if (unsubscribe.length == 1) {
unsubscribe.push(eventsService.on("content.saved", () => {
save();
}));
unsubscribe.push(eventsService.on("content.unpublished", () => {
save();
}));
}
}));

vm.$onDestroy = function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,9 @@ public IActionResult Get(int nodeId, string culture)

using var ctx = _umbracoContextFactory.EnsureUmbracoContext();
var content = ctx.UmbracoContext.Content.GetById(true, nodeId);
if (content is null)
{
_logger.LogInformation("Could not find content by id {0}", nodeId);
return NotFound();
}

var metaTags = _seoService.Get(content, false);
var userValues = _seoValueService.GetUserValues(nodeId);
var metaTags = content is null ? _seoService.GetEmpty() : _seoService.Get(content, false);
var userValues = content is null ? new Dictionary<string, object>() : _seoValueService.GetUserValues(nodeId);

return new JsonResult(new MetaFieldsSettingsViewModel
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public interface IMetaTagsProvider
event EventHandler<MetaTagsModel> BeforeMetaTagsGet;

MetaTagsModel Get(IPublishedContent content, bool includeUserValues);

MetaTagsModel GetEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Models.PublishedContent;
using SeoToolkit.Umbraco.MetaFields.Core.Models.SeoService;

namespace SeoToolkit.Umbraco.MetaFields.Core.Interfaces.Services
{
public interface IMetaFieldsService
{
MetaTagsModel Get(IPublishedContent content, bool includeUserValues);
MetaTagsModel GetEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,12 @@ public MetaTagsModel Get(IPublishedContent content, bool includeUserValues)
return metaTags;
}
}

public MetaTagsModel GetEmpty()
{
var allFields = _seoFieldCollection.GetAll().ToArray();

return new MetaTagsModel(allFields.ToDictionary(it => it, it => (object)null));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@ public MetaTagsModel Get(IPublishedContent content, bool includeUserValues)
{
return _metaTagsProvider.Get(content, includeUserValues);
}

public MetaTagsModel GetEmpty()
{
return _metaTagsProvider.GetEmpty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@
save();
}));

vm.$onDestroy = function () {
unsubscribe.forEach(x => x());
}

init();
}

Expand Down

0 comments on commit 105ce1f

Please sign in to comment.