If you only want to publish related media items, there are a few things you'll need to do.
Disclaimer
It sounds like workflow would work best to satisfy your use-case and reduce the number of customizations you need to make. That would be my first recommendation. However, an outline for what you've asked for is below.
Remove Existing Processors From getItemReferences
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<getItemReferences>
<patch:delete />
</getItemReferences>
<getItemReferences>
<!-- Finds linked media items on related items -->
<processor type="MyNamespace.GetRelatedMediaItems, MyDll" />
</getItemReferences>
</pipelines>
</sitecore>
</configuration>
Notice how I've also included a processor
to meet your needs-- GetRelatedMedaiItems
.
GetRelatedMediaItems Processor
namespace MyNamespace
{
public class GetRelatedMediaItems : GetItemReferencesProcessor
{
protected override List<Item> GetItemReferences(PublishItemContext context)
{
Assert.ArgumentNotNull(context, nameof(context));
var objList = new List<Item>();
if (context.PublishOptions.Mode != SitecorePublishing.PublishMode.SingleItem)
return objList;
switch (context.Action)
{
case SitecorePublishing.PublishAction.PublishSharedFields:
var sourceItem = context.PublishHelper.GetSourceItem(context.ItemId);
if (sourceItem == null)
return objList;
objList.AddRange(GetReferences(sourceItem, true));
break;
case SitecorePublishing.PublishAction.PublishVersion:
var versionToPublish = context.VersionToPublish;
if (versionToPublish == null)
return objList;
objList.AddRange(GetReferences(versionToPublish, false));
break;
default:
return objList;
}
return objList;
}
private IEnumerable<Item> GetReferences(Item item, bool sharedOnly)
{
Assert.ArgumentNotNull(item, nameof(item));
var sharedFilter = sharedOnly ? (Func<ItemLink, bool>)SharedFilter : link => true;
return item
.Links
.GetValidLinks()
.Where(sharedFilter)
.Select(link => link.GetTargetItem())
.Where(Is.NotNull)
.SelectMany(GetLinkedMediaItems);
}
private static bool SharedFilter(ItemLink source)
{
var sourceItem = source.GetSourceItem();
if (sourceItem == null)
return false;
return ID.IsNullOrEmpty(source.SourceFieldID) || sourceItem.Fields[source.SourceFieldID].Shared;
}
private static IEnumerable<Item> GetLinkedMediaItems(Item item)
{
return item
.Links
.GetValidLinks()
.Select(link => link.GetTargetItem())
.Where(Is.NotNull)
.Where(target => target.Paths.IsMediaItem) // this is they key
.SelectMany(
target =>
target
.GetAncestorsUntil(parent => parent.ID == ItemIDs.MediaLibraryRoot, true) // you can remove this step if you wish. this ensures the parents to a media item are published to ensure the media item ACTUALLY gets published
.Reverse()
);
}
}
}
Then, it is important to note that you would instruct your Content Authors to select the Publish related items
option.