Automatically adding MediaPart to content types if another part depends on it, fixes #4792

This commit is contained in:
Lombiq
2017-02-06 21:46:55 +01:00
committed by Zoltán Lehóczky
parent 18b2c01768
commit 727f70a8fc
3 changed files with 84 additions and 1 deletions

View File

@@ -0,0 +1,78 @@
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Models;
using Orchard.ContentTypes.Events;
using Orchard.MediaLibrary.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Orchard.MediaLibrary.Handlers {
/// <summary>
/// Automatically adds MediaPart to the content type if a content part is attached that needs it.
/// </summary>
public class MediaItemHandler : IContentDefinitionEventHandler {
private readonly IContentDefinitionManager _contentDefinitionManager;
public MediaItemHandler(IContentDefinitionManager contentDefinitionManager) {
_contentDefinitionManager = contentDefinitionManager;
}
public void ContentFieldAttached(ContentFieldAttachedContext context) {
}
public void ContentFieldDetached(ContentFieldDetachedContext context) {
}
public void ContentPartAttached(ContentPartAttachedContext context) {
AlterMediaItem(_contentDefinitionManager.GetTypeDefinition(context.ContentTypeName));
}
public void ContentPartCreated(ContentPartCreatedContext context) {
}
public void ContentPartDetached(ContentPartDetachedContext context) {
}
public void ContentPartImported(ContentPartImportedContext context) {
}
public void ContentPartImporting(ContentPartImportingContext context) {
}
public void ContentPartRemoved(ContentPartRemovedContext context) {
}
public void ContentTypeCreated(ContentTypeCreatedContext context) {
AlterMediaItem(context.ContentTypeDefinition);
}
public void ContentTypeImported(ContentTypeImportedContext context) {
AlterMediaItem(context.ContentTypeDefinition);
}
public void ContentTypeImporting(ContentTypeImportingContext context) {
}
public void ContentTypeRemoved(ContentTypeRemovedContext context) {
}
private void AlterMediaItem(ContentTypeDefinition contentTypeDefinition) {
var partNames = new string[]{
typeof(ImagePart).Name,
typeof(VectorImagePart).Name,
typeof(VideoPart).Name,
typeof(AudioPart).Name,
typeof(DocumentPart).Name,
typeof(OEmbedPart).Name };
if (contentTypeDefinition != null &&
contentTypeDefinition.Parts.Any(contentTypePartDefinition =>
partNames.Contains(contentTypePartDefinition.PartDefinition.Name))) {
_contentDefinitionManager.AlterTypeDefinition(contentTypeDefinition.Name,
cfg => cfg
.WithPart(typeof(MediaPart).Name)
.WithSetting("Stereotype", "Media"));
}
}
}
}

View File

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

View File

@@ -127,6 +127,7 @@
<Compile Include="Drivers\VectorImagePartDriver.cs" />
<Compile Include="Extensions\MediaMetaDataExtensions.cs" />
<Compile Include="Factories\VectorImageFactory.cs" />
<Compile Include="Handlers\MediaItemHandler.cs" />
<Compile Include="Handlers\MediaLibrarySettingsPartHandler.cs" />
<Compile Include="Implementation\DefaultMediaUsername.cs" />
<Compile Include="Models\MediaLibrarySettingsPart.cs" />
@@ -149,6 +150,10 @@
<Name>Orchard.Core</Name>
<Private>false</Private>
</ProjectReference>
<ProjectReference Include="..\Orchard.ContentTypes\Orchard.ContentTypes.csproj">
<Project>{0E7646E8-FE8F-43C1-8799-D97860925EC4}</Project>
<Name>Orchard.ContentTypes</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Tokens\Orchard.Tokens.csproj">
<Project>{6f759635-13d7-4e94-bcc9-80445d63f117}</Project>
<Name>Orchard.Tokens</Name>