public override object GetResult(ValueProviderArgs args) { var renderingContext = RenderingContext.CurrentOrNull; if (string.IsNullOrEmpty(renderingContext?.Rendering.DataSource)) { return(null); } return(_sitecoreContext.CreateType(args.RequestedType, renderingContext.Rendering.Item, true, true, null, null)); }
private IGlassBase GetInstanceFromContext(Rendering rendering, ISitecoreContext sitecoreContext) { string datasource = rendering.DataSource; Type glassBaseType = typeof(GlassBase); if (!datasource.IsNotNullOrEmpty()) { return((IGlassBase)sitecoreContext.GetCurrentItem(glassBaseType, inferType: true)); } Item item = sitecoreContext.Database.GetItem(datasource); return((IGlassBase)sitecoreContext.CreateType(glassBaseType, item, true, true, null, null)); }
/// <summary> /// Gets the object. /// </summary> /// <param name="model">The model.</param> /// <param name="db">The db.</param> /// <returns></returns> /// <exception cref="Glass.Mapper.MapperException">Failed to find context {0}.Formatted(ContextName)</exception> public virtual object GetObject(string model, Database db, Rendering renderingItem) { if (model.IsNullOrEmpty()) { return(null); } //must be a path to a Model item if (model.StartsWith("/sitecore")) { var target = db.GetItem(model); if (target == null) { return(null); } string newModel = target[ModelTypeField]; return(GetObject(newModel, db, renderingItem)); } //if guid must be that to Model item Guid targetId; if (Guid.TryParse(model, out targetId)) { var target = db.GetItem(new ID(targetId)); if (target == null) { return(null); } string newModel = target[ModelTypeField]; return(GetObject(newModel, db, renderingItem)); } var type = Type.GetType(model, false); if (type == null || renderingModelType.IsAssignableFrom(type)) { return(null); } ISitecoreContext scContext = SitecoreContextFactory.GetSitecoreContext(); //this is really aggressive if (!scContext.GlassContext.TypeConfigurations.ContainsKey(type)) { //if the config is null then it is probably an ondemand mapping so we have to load the ondemand part IConfigurationLoader loader = new OnDemandLoader <SitecoreTypeConfiguration>(type); scContext.GlassContext.Load(loader); } if (renderingItem.DataSource.IsNotNullOrEmpty()) { var item = scContext.Database.GetItem(renderingItem.DataSource); return(scContext.CreateType(type, item, false, false, null)); } if (renderingItem.RenderingItem.DataSource.HasValue()) { var item = scContext.Database.GetItem(renderingItem.RenderingItem.DataSource); return(scContext.CreateType(type, item, false, false, null)); } /** * Issues #82: * Check Item before defaulting to the current item. */ if (renderingItem.Item != null) { return(scContext.CreateType(type, renderingItem.Item, false, false, null)); } return(scContext.GetCurrentItem(type)); }
public override void Process(GetModelArgs args) { var key = "GetModelFromView"; var watch = Stopwatch.StartNew(); try { if (!IsValidForProcessing(args)) { return; } string path = GetViewPath(args); if (string.IsNullOrWhiteSpace(path)) { return; } string cacheKey = modelCacheManager.GetKey(path); Type modelType = modelCacheManager.Get(cacheKey); if (modelType == typeof(NullModel)) { // The model has been attempted before and is not useful return; } // The model type hasn't been found before or has been cleared. if (modelType == null) { modelType = GetModel(args, path); modelCacheManager.Add(cacheKey, modelType); if (modelType == typeof(NullModel)) { // This is not the type we are looking for return; } } ISitecoreContext scContext = SitecoreContextFactory.GetSitecoreContext(); Rendering renderingItem = args.Rendering; object model = null; if (renderingItem.DataSource.HasValue()) { var item = scContext.Database.GetItem(renderingItem.DataSource); model = scContext.CreateType(modelType, item, false, false, null); } else if (renderingItem.RenderingItem.DataSource.HasValue()) { var item = scContext.Database.GetItem(renderingItem.RenderingItem.DataSource); model = scContext.CreateType(modelType, item, false, false, null); } else if (renderingItem.Item != null) { /** * Issues #82: * Check Item before defaulting to the current item. */ model = scContext.CreateType(modelType, renderingItem.Item, false, false, null); } else { model = scContext.GetCurrentItem(modelType); } args.Result = model; } finally { Sitecore.Diagnostics.Log.Debug("GetModelFromView {0} {1}".Formatted(watch.ElapsedMilliseconds, args.Rendering.RenderingItem.ID), this); } }
public void Initialize(Rendering rendering) { SiteRoot = _context.CreateType <SiteRoot>(rendering.Item); //helper = Sitecore.Mvc.Helpers. }
public override void Process(GetModelArgs args) { if (!IsValidForProcessing(args)) { return; } string path = GetViewPath(args); if (string.IsNullOrWhiteSpace(path)) { return; } string cacheKey = modelCacheManager.GetKey(path); Type modelType = modelCacheManager.Get(cacheKey); if (modelType == typeof(NullModel)) { // The model has been attempted before and is not useful return; } // The model type hasn't been found before or has been cleared. if (modelType == null) { var fullPath = HttpContext.Current.Server.MapPath(path); if (File.Exists(fullPath)) { modelType = GetModel(args, path); } else { modelType = typeof(NullModel); } modelCacheManager.Add(cacheKey, modelType); if (modelType == typeof(NullModel)) { // This is not the type we are looking for return; } } ISitecoreContext scContext = SitecoreContextFactory.GetSitecoreContext(); Rendering renderingItem = args.Rendering; object model = null; if (renderingItem.DataSource.HasValue()) { var item = scContext.Database.GetItem(renderingItem.DataSource); model = scContext.CreateType(modelType, item, false, false, null); } else if (renderingItem.RenderingItem.DataSource.HasValue()) { var item = scContext.Database.GetItem(renderingItem.RenderingItem.DataSource); model = scContext.CreateType(modelType, item, false, false, null); } else if (renderingItem.Item != null) { /** * Issues #82: * Check Item before defaulting to the current item. */ model = scContext.CreateType(modelType, renderingItem.Item, false, false, null); } else { model = scContext.GetCurrentItem(modelType); } args.Result = model; }