/// <summary>
        /// Get the render widget models
        /// </summary>
        /// <param name="widgetZone">Name of widget zone</param>
        /// <param name="additionalData">Additional data object</param>
        /// <returns>
        /// A task that represents the asynchronous operation
        /// The task result contains the list of the render widget models
        /// </returns>
        public virtual async Task <List <RenderWidgetModel> > PrepareRenderWidgetModelAsync(string widgetZone, object additionalData = null)
        {
            var theme = await _themeContext.GetWorkingThemeNameAsync();

            var customer = await _workContext.GetCurrentCustomerAsync();

            var customerRoleIds = await _customerService.GetCustomerRoleIdsAsync(customer);

            var store = await _storeContext.GetCurrentStoreAsync();

            var cacheKey = _staticCacheManager.PrepareKeyForShortTermCache(NopModelCacheDefaults.WidgetModelKey,
                                                                           customerRoleIds, store, widgetZone, theme);

            var cachedModels = await _staticCacheManager.GetAsync(cacheKey, async() =>
                                                                  (await _widgetPluginManager.LoadActivePluginsAsync(customer, store.Id, widgetZone))
                                                                  .Select(widget => new RenderWidgetModel
            {
                WidgetViewComponentName      = widget.GetWidgetViewComponentName(widgetZone),
                WidgetViewComponentArguments = new RouteValueDictionary {
                    ["widgetZone"] = widgetZone
                }
            }));

            //"WidgetViewComponentArguments" property of widget models depends on "additionalData".
            //We need to clone the cached model before modifications (the updated one should not be cached)
            var models = cachedModels.Select(renderModel => new RenderWidgetModel
            {
                WidgetViewComponentName      = renderModel.WidgetViewComponentName,
                WidgetViewComponentArguments = new RouteValueDictionary {
                    ["widgetZone"] = widgetZone, ["additionalData"] = additionalData
                }
            }).ToList();

            return(models);
        }
Example #2
0
        /// <summary>
        /// Prepare render widget models
        /// </summary>
        /// <param name="widgetZone">Widget zone name</param>
        /// <param name="additionalData">Additional data</param>
        /// <returns>List of render widget models</returns>
        public virtual async Task <IList <RenderWidgetModel> > PrepareRenderWidgetModelsAsync(string widgetZone, object additionalData = null)
        {
            //get active widgets by widget zone
            var widgets = await _widgetPluginManager.LoadActivePluginsAsync(await _workContext.GetCurrentCustomerAsync(), widgetZone : widgetZone);

            //prepare models
            var models = widgets.Select(widget => new RenderWidgetModel
            {
                WidgetViewComponentName      = widget.GetWidgetViewComponentName(widgetZone),
                WidgetViewComponentArguments = new RouteValueDictionary {
                    ["widgetZone"] = widgetZone, ["additionalData"] = additionalData
                }
            }).ToList();

            return(models);
        }