#19956: Adding a token for MediaLibraryFieldPicker

Work Item: 19956
This commit is contained in:
Sebastien Ros 2013-08-20 16:54:55 -07:00
parent 70c38496f4
commit 2fc774e93b
5 changed files with 61 additions and 10 deletions

View File

@ -3,13 +3,12 @@ using System.Collections.Generic;
using System.Linq;
using Orchard.ContentManagement;
using Orchard.ContentManagement.FieldStorage;
using Orchard.ContentManagement.Utilities;
using Orchard.MediaLibrary.Models;
namespace Orchard.MediaLibrary.Fields {
public class MediaLibraryPickerField : ContentField {
private static readonly char[] separator = new [] {'{', '}', ','};
internal LazyField<IEnumerable<MediaPart>> _contentItems = new LazyField<IEnumerable<MediaPart>>();
internal Lazy<IEnumerable<MediaPart>> _contentItems;
public int[] Ids {
get { return DecodeIds(Storage.Get<string>()); }

View File

@ -1,4 +1,6 @@
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using Orchard.ContentManagement.MetaData;
@ -18,20 +20,23 @@ namespace Orchard.MediaLibrary.Handlers {
_contentDefinitionManager = contentDefinitionManager;
}
protected override void Loading(LoadContentContext context) {
protected override void Loaded(LoadContentContext context) {
base.Loading(context);
InitilizeLoader(context.ContentItem);
}
var fields = context.ContentItem.Parts.SelectMany(x => x.Fields.Where(f => f.FieldDefinition.Name == typeof(MediaLibraryPickerField).Name)).Cast<MediaLibraryPickerField>();
// define lazy initializer for ContentPickerField.ContentItems
var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(context.ContentType);
private void InitilizeLoader(ContentItem contentItem) {
var fields = contentItem.Parts.SelectMany(x => x.Fields.OfType<MediaLibraryPickerField>());
// define lazy initializer for MediaLibraryPickerField.MediaParts
var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(contentItem.ContentType);
if (contentTypeDefinition == null) {
return;
}
foreach (var field in fields) {
var localField = field;
field._contentItems.Loader(x => _contentManager.GetMany<MediaPart>(localField.Ids, VersionOptions.Published, QueryHints.Empty));
localField._contentItems = new Lazy<IEnumerable<MediaPart>>(() => _contentManager.GetMany<MediaPart>(localField.Ids, VersionOptions.Published, QueryHints.Empty).ToList());
}
}
}

View File

@ -9,5 +9,5 @@ Features:
Orchard.MediaLibrary:
Name: Media Library
Description: Provides enhanced Media management tools.
Dependencies: Title, Orchard.MediaProcessing
Dependencies: Title, Orchard.MediaProcessing, Orchard.Tokens
Category: Media

View File

@ -109,6 +109,10 @@
<Project>{9916839C-39FC-4CEB-A5AF-89CA7E87119F}</Project>
<Name>Orchard.Core</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Tokens\Orchard.Tokens.csproj">
<Project>{6f759635-13d7-4e94-bcc9-80445d63f117}</Project>
<Name>Orchard.Tokens</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Compile Include="ModalAdminFilter.cs" />
@ -159,6 +163,7 @@
<Compile Include="Services\XmlRpcHandler.cs" />
<Compile Include="Settings\MediaLibraryPickerFieldEditorEvents.cs" />
<Compile Include="Settings\MediaLibraryPickerFieldSettings.cs" />
<Compile Include="Tokens\FieldTokens.cs" />
<Compile Include="ViewModels\MediaLibraryPickerFieldViewModel.cs" />
<Compile Include="ViewModels\MediaManagerMediaItemsViewModel.cs" />
<Compile Include="ViewModels\MediaManagerFolderCreateViewModel.cs" />

View File

@ -0,0 +1,42 @@
using System.Linq;
using Orchard.ContentManagement;
using Orchard.Localization;
using Orchard.MediaLibrary.Fields;
using Orchard.MediaLibrary.Models;
using Orchard.Tokens;
namespace Orchard.MediaLibrary.Tokens {
public class FieldTokens : ITokenProvider {
private readonly IContentManager _contentManager;
public FieldTokens(IContentManager contentManager) {
_contentManager = contentManager;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public void Describe(DescribeContext context) {
context.For("MediaLibraryPickerField", T("First media for Media Library Picker Field"), T("Tokens for Media Picker Fields"))
.Token("Url", T("Url"), T("The url of the media."), "Url")
;
}
public void Evaluate(EvaluateContext context) {
context.For<MediaLibraryPickerField>("MediaLibraryPickerField")
.Token("Url", field => {
var mediaId = field.Ids.FirstOrDefault();
var media = _contentManager.Get<MediaPart>(mediaId);
return media == null ? "" : media.MediaUrl;
})
.Chain("Url", "Url", field => {
var mediaId = field.Ids.FirstOrDefault();
var media = _contentManager.Get<MediaPart>(mediaId);
return media == null ? "" : media.MediaUrl;
})
;
}
}
}