-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to generate DisplayText with a Pattern (#4464)
- Loading branch information
1 parent
2067a6e
commit 85f2b58
Showing
12 changed files
with
275 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/OrchardCore.Modules/OrchardCore.Title/Handlers/TitlePartHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Fluid; | ||
using OrchardCore.ContentManagement; | ||
using OrchardCore.ContentManagement.Handlers; | ||
using OrchardCore.ContentManagement.Metadata; | ||
using OrchardCore.Liquid; | ||
using OrchardCore.Title.Models; | ||
|
||
namespace OrchardCore.Title.Handlers | ||
{ | ||
public class TitlePartHandler : ContentPartHandler<TitlePart> | ||
{ | ||
private readonly ILiquidTemplateManager _liquidTemplateManager; | ||
private readonly IContentDefinitionManager _contentDefinitionManager; | ||
|
||
public TitlePartHandler( | ||
ILiquidTemplateManager liquidTemplateManager, | ||
IContentDefinitionManager contentDefinitionManager) | ||
{ | ||
_liquidTemplateManager = liquidTemplateManager; | ||
_contentDefinitionManager = contentDefinitionManager; | ||
} | ||
|
||
|
||
public override async Task UpdatedAsync(UpdateContentContext context, TitlePart part) | ||
{ | ||
var settings = GetSettings(part); | ||
// Do not compute the title if the user can modify it and the text is already set. | ||
if (settings.Options == TitlePartOptions.Editable && !String.IsNullOrWhiteSpace(part.ContentItem.DisplayText)) | ||
{ | ||
return; | ||
} | ||
|
||
if (!String.IsNullOrEmpty(settings.Pattern)) | ||
{ | ||
var templateContext = new TemplateContext(); | ||
templateContext.SetValue("ContentItem", part.ContentItem); | ||
|
||
var title = await _liquidTemplateManager.RenderAsync(settings.Pattern, NullEncoder.Default, templateContext); | ||
title = title.Replace("\r", String.Empty).Replace("\n", String.Empty); | ||
|
||
part.Title = title; | ||
part.ContentItem.DisplayText = title; | ||
part.Apply(); | ||
} | ||
} | ||
|
||
private TitlePartSettings GetSettings(TitlePart part) | ||
{ | ||
var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(part.ContentItem.ContentType); | ||
var contentTypePartDefinition = contentTypeDefinition.Parts.FirstOrDefault(x => String.Equals(x.PartDefinition.Name, nameof(TitlePart), StringComparison.Ordinal)); | ||
return contentTypePartDefinition.GetSettings<TitlePartSettings>(); | ||
} | ||
} | ||
} |
2 changes: 0 additions & 2 deletions
2
src/OrchardCore.Modules/OrchardCore.Title/Models/TitlePart.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
using OrchardCore.ContentManagement; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace OrchardCore.Title.Models | ||
{ | ||
public class TitlePart : ContentPart | ||
{ | ||
[Required] | ||
public string Title { get; set; } | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/OrchardCore.Modules/OrchardCore.Title/Models/TitlePartSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System.ComponentModel; | ||
|
||
namespace OrchardCore.Title.Models | ||
{ | ||
public enum TitlePartOptions | ||
{ | ||
Editable, | ||
GeneratedDisabled, | ||
GeneratedHidden, | ||
} | ||
public class TitlePartSettings | ||
{ | ||
/// <summary> | ||
/// Gets or sets whether a user can define a custom title | ||
/// </summary> | ||
[DefaultValue(TitlePartOptions.Editable)] | ||
public TitlePartOptions Options { get; set; } = TitlePartOptions.Editable; | ||
|
||
/// <summary> | ||
/// The pattern used to build the Title. | ||
/// </summary> | ||
public string Pattern { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,39 @@ | ||
# Title (`OrchardCore.Title`) | ||
|
||
The `Title` module provides a **Title Part** that lets user define a title for a content item. | ||
It also defines the `DisplayText` property of the `ContentItemMetadata` aspect. | ||
The `Title` module provides a **Title Part** that lets you customize the DisplayText property of a content item. | ||
The DisplayText property is used throughout the Admin interface to help you recognize your content items. | ||
|
||
## TitlePart | ||
|
||
Attach this part to your content items to customize the DisplayText property of a ContentItem. | ||
|
||
### TitlePart Settings | ||
|
||
By default, attaching the TitlePart will allow content editors to manually edit the DisplayText(title) of a ContentItem. | ||
|
||
You can also generate the Title by specifying a pattern using a Liquid expression. | ||
|
||
The Pattern has access the current ContentItem and is executed on ContentItem update. | ||
For example, fields can be used to generate the pattern. The following example uses a __Text field__ named `Name`, on a `Product` content type. | ||
|
||
```liquid | ||
{{ ContentItem.Content.Product.Name.Text }} | ||
``` | ||
|
||
## Theming | ||
|
||
The following shapes are rendered when the **Title Part** is attached to a content type. | ||
|
||
| Name | Display Type | Default Location | Model Type | | ||
| ------| ------------ |----------------- | ---------- | | ||
| `TitlePart` | `Detail` | `Header:5` | `TitlePartViewModel` | | ||
| `TitlePart` | `Summary` | `Header:10` | `TitlePartViewModel` | | ||
| Name | Display Type | Default Location | Model Type | | ||
| ----------- | ------------ | ---------------- | -------------------- | | ||
| `TitlePart` | `Detail` | `Header:5` | `TitlePartViewModel` | | ||
| `TitlePart` | `Summary` | `Header:10` | `TitlePartViewModel` | | ||
|
||
### View Model | ||
|
||
The following properties are available in the `TitlePartViewModel` class. | ||
|
||
| Name | Type | Description | | ||
| -----| ---- |------------ | | ||
| `Title` | `string` | The title property of the part. | | ||
| `TitlePart` | `TitlePart` | The `TitlePart` instance. | | ||
| Name | Type | Description | | ||
| ----------- | ----------- | ------------------------------- | | ||
| `Title` | `string` | The title property of the part. | | ||
| `TitlePart` | `TitlePart` | The `TitlePart` instance. | |
68 changes: 68 additions & 0 deletions
68
src/OrchardCore.Modules/OrchardCore.Title/Settings/TitlePartSettingsDisplayDriver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Localization; | ||
using OrchardCore.Title.Models; | ||
using OrchardCore.Title.ViewModels; | ||
using OrchardCore.ContentManagement.Metadata.Models; | ||
using OrchardCore.ContentTypes.Editors; | ||
using OrchardCore.DisplayManagement.ModelBinding; | ||
using OrchardCore.DisplayManagement.Views; | ||
using OrchardCore.Liquid; | ||
|
||
namespace OrchardCore.Title.Settings | ||
{ | ||
public class TitlePartSettingsDisplayDriver : ContentTypePartDefinitionDisplayDriver | ||
{ | ||
private readonly ILiquidTemplateManager _templateManager; | ||
|
||
public TitlePartSettingsDisplayDriver(ILiquidTemplateManager templateManager, IStringLocalizer<TitlePartSettingsDisplayDriver> localizer) | ||
{ | ||
_templateManager = templateManager; | ||
T = localizer; | ||
} | ||
|
||
public IStringLocalizer T { get; private set; } | ||
|
||
public override IDisplayResult Edit(ContentTypePartDefinition contentTypePartDefinition, IUpdateModel updater) | ||
{ | ||
if (!String.Equals(nameof(TitlePart), contentTypePartDefinition.PartDefinition.Name, StringComparison.Ordinal)) | ||
{ | ||
return null; | ||
} | ||
|
||
return Initialize<TitlePartSettingsViewModel>("TitlePartSettings_Edit", model => | ||
{ | ||
var settings = contentTypePartDefinition.GetSettings<TitlePartSettings>(); | ||
model.Options = settings.Options; | ||
model.Pattern = settings.Pattern; | ||
model.TitlePartSettings = settings; | ||
}).Location("Content"); | ||
} | ||
|
||
public override async Task<IDisplayResult> UpdateAsync(ContentTypePartDefinition contentTypePartDefinition, UpdateTypePartEditorContext context) | ||
{ | ||
if (!String.Equals(nameof(TitlePart), contentTypePartDefinition.PartDefinition.Name, StringComparison.Ordinal)) | ||
{ | ||
return null; | ||
} | ||
|
||
var model = new TitlePartSettingsViewModel(); | ||
|
||
await context.Updater.TryUpdateModelAsync(model, Prefix, | ||
m => m.Pattern, | ||
m => m.Options); | ||
|
||
if (!string.IsNullOrEmpty(model.Pattern) && !_templateManager.Validate(model.Pattern, out var errors)) | ||
{ | ||
context.Updater.ModelState.AddModelError(nameof(model.Pattern), T["Pattern doesn't contain a valid Liquid expression. Details: {0}", string.Join(" ", errors)]); | ||
} | ||
else | ||
{ | ||
context.Builder.WithSettings(new TitlePartSettings {Pattern = model.Pattern, Options = model.Options,}); | ||
} | ||
|
||
return Edit(contentTypePartDefinition, context.Updater); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/OrchardCore.Modules/OrchardCore.Title/ViewModels/TitlePartSettingsViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using OrchardCore.Title.Models; | ||
|
||
namespace OrchardCore.Title.ViewModels | ||
{ | ||
public class TitlePartSettingsViewModel | ||
{ | ||
public TitlePartOptions Options { get; set; } | ||
public string Pattern { get; set; } | ||
|
||
[BindNever] | ||
public TitlePartSettings TitlePartSettings { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 17 additions & 7 deletions
24
src/OrchardCore.Modules/OrchardCore.Title/Views/TitlePart.Edit.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,25 @@ | ||
@model TitlePartViewModel | ||
@using OrchardCore.ContentLocalization | ||
@using OrchardCore.Localization | ||
@using OrchardCore.Title.Models | ||
@using OrchardCore.Title.ViewModels; | ||
|
||
@{ | ||
var culture = await Orchard.GetContentCultureAsync(Model.TitlePart.ContentItem); | ||
} | ||
|
||
<fieldset class="form-group" asp-validation-class-for="Title"> | ||
<label asp-for="Title">@T["Title"]</label> | ||
<input asp-for="Title" class="form-control content-preview-text content-caption-text" autofocus="autofocus" dir="@culture.GetLanguageDirection()" /> | ||
<span asp-validation-for="Title"></span> | ||
<span class="hint">@T["The title of the content item."]</span> | ||
</fieldset> | ||
@if (Model.Settings?.Options != TitlePartOptions.GeneratedHidden) | ||
{ | ||
<fieldset class="form-group" asp-validation-class-for="Title"> | ||
<label asp-for="Title">@T["Title"]</label> | ||
<input asp-for="Title" class="form-control content-preview-text content-caption-text" disabled="@(Model.Settings?.Options == TitlePartOptions.GeneratedDisabled)" autofocus="autofocus" dir="@culture.GetLanguageDirection()"/> | ||
<span asp-validation-for="Title"></span> | ||
@if (String.IsNullOrWhiteSpace(Model.Settings?.Pattern) && Model.Settings?.Options == TitlePartOptions.Editable) | ||
{ | ||
<span class="hint">@T["The title of the content item. Set empty to generate it using the pattern."]</span> | ||
} | ||
else | ||
{ | ||
<span class="hint">@T["The title of the content item. It will be automatically generated."]</span> | ||
} | ||
</fieldset> | ||
} |
Oops, something went wrong.