Created
June 25, 2022 15:24
-
-
Save agehlot/0cd2c2a8f9e0a6e0f995239bb4a2bfcd to your computer and use it in GitHub Desktop.
If you want to customize your JSON rendering response and want complete customized and controlled output with your own logic behind. Here is the solution for you.
This file contains hidden or 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
using Microsoft.Extensions.DependencyInjection; | |
using Newtonsoft.Json.Linq; | |
using Sitecore.Data.Items; | |
using Sitecore.JavaScriptServices.GraphQL.LayoutService; | |
using Sitecore.LayoutService.Configuration; | |
using Sitecore.LayoutService.ItemRendering.ContentsResolvers; | |
using Sitecore.Mvc.Presentation; | |
using System; | |
namespace MyCore.Foundation.LayoutService.RenderingContentsResolvers | |
{ | |
/// <summary> | |
/// Render current item siblings that belong to the same parent | |
/// </summary> | |
public class ItemSiblingRenderingContentsResolver : RenderingContentsResolver | |
{ | |
private readonly IServiceProvider serviceProvider; | |
public ItemSiblingRenderingContentsResolver(IServiceProvider serviceProvider) | |
{ | |
this.serviceProvider = serviceProvider; | |
} | |
public override object ResolveContents(Rendering rendering, IRenderingConfiguration renderingConfig) | |
{ | |
if (rendering.Item.Parent.TemplateID == IDealConstants.TemplateId) | |
{ | |
var contents = (JObject)base.ResolveContents(rendering, renderingConfig); | |
var query = rendering.RenderingItem.InnerItem[Sitecore.JavaScriptServices.Core.FieldIDs.JsonRendering.GraphQLQuery]; | |
if (string.IsNullOrWhiteSpace(query)) | |
{ | |
return contents; | |
} | |
var graphqlResolver = ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider, typeof(GraphQLAwareRenderingContentsResolver)) as IRenderingContentsResolver; | |
var graphqlContent = (JObject)graphqlResolver.ResolveContents(rendering, renderingConfig); | |
////if both rendering content + graphql content need to be rendered uncomment below lines. | |
//contents["additionalData"] = graphqlContent; | |
//return contents; | |
return graphqlContent; | |
} | |
return null; | |
} | |
protected override JObject ProcessItem(Item item, Rendering rendering, IRenderingConfiguration renderingConfig) | |
{ | |
JObject jRootObject = new JObject(); | |
if (item.Parent.TemplateID == IDealConstants.TemplateId) | |
{ | |
JArray jarrayObj = new JArray(); | |
foreach (Item obj in item.Parent.Children) | |
{ | |
if (item.ID != obj.ID) | |
{ | |
//JObject jobject1 = this.ProcessItem(obj, rendering, renderingConfig); | |
JObject childObject = new JObject() | |
{ | |
["id"] = (JToken)obj.ID.Guid.ToString(), | |
["name"] = (JToken)obj.Name, | |
["displayName"] = (JToken)obj.DisplayName, | |
["ShortTitle"] = (JToken)obj.Fields["ShortTitle"].Value, | |
["LongTitle"] = (JToken)obj.Fields["LongTitle"].Value, | |
}; | |
jarrayObj.Add(childObject); | |
} | |
} | |
jRootObject.Add("Siblings", jarrayObj); | |
return jRootObject; | |
} | |
return null; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment