recipe how can we write a value into the Name field on Orchard Widgets

Topics: Administration, Customizing Orchard, Installing Orchard, Writing modules
Feb 13, 2013 at 11:25 AM
Hi Orchard People,

Is it possible to write to the name field for the widgets setup. We have customize our css file, so we need to attach our widget to this field, so the css is also working.

Hope that someone have knowledge about this.
is it possible to find a list of commands we can use in the recipe file


Regards


Peter
Euronome
Coordinator
Feb 13, 2013 at 10:44 PM
What name field, and what widget setup?
Feb 14, 2013 at 5:25 AM
Yes, it is Orchard CMS Widget Setup, the one you push from Orchards Dashboard.
We have defined our own layer, and we need to create our widgets through the recipe file when we install a new installation.

There is a unique field called Name, when you create and define a widget inside orchard. It is used to identify tags inside the css file, that is used in the Theme css file.

But we have tried to use many different things for identifying this field in the recipe file, but no one works.

Are there a place were we can see commands and attributes that is available in Orchard. or do someone know, what this field should be called in the recipe file.

Thanks


Peter
Developer
Feb 14, 2013 at 12:06 PM
I needed that myself as well, but unfortunately the current "widget create" command does not accept a "Name" parameter, so I created that command myself.
One thing you could do is file a bug about this, fork Orchard, modify the command to include the "name" and send a pull request.
Alternatively, you can create your own command.
The code would look like this (notice the added "Name" property and its usage in the command):
using System;
using System.Linq;
using Orchard.Commands;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects;
using Orchard.Core.Common.Models;
using Orchard.Core.Navigation.Models;
using Orchard.Core.Navigation.Services;
using Orchard.Core.Title.Models;
using Orchard.Security;
using Orchard.Settings;
using Orchard.Widgets.Models;
using Orchard.Widgets.Services;

namespace Orchard.Widgets.Commands {
    public class WidgetCommands : DefaultOrchardCommandHandler {
        private readonly IWidgetsService _widgetsService;
        private readonly ISiteService _siteService;
        private readonly IMembershipService _membershipService;
        private readonly IContentManager _contentManager;
        private readonly IMenuService _menuService;
        private const string LoremIpsum = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur a nibh ut tortor dapibus vestibulum. Aliquam vel sem nibh. Suspendisse vel condimentum tellus.</p>";

        public WidgetCommands(
            IWidgetsService widgetsService, 
            ISiteService siteService, 
            IMembershipService membershipService,
            IContentManager contentManager,
            IMenuService menuService) {
            _widgetsService = widgetsService;
            _siteService = siteService;
            _membershipService = membershipService;
            _contentManager = contentManager;
            _menuService = menuService;

            RenderTitle = true;
        }

        [OrchardSwitch]
        public string Title { get; set; }

        [OrchardSwitch]
        public string Name { get; set; }

        [OrchardSwitch]
        public bool RenderTitle { get; set; }

        [OrchardSwitch]
        public string Zone { get; set; }

        [OrchardSwitch]
        public string Position { get; set; }

        [OrchardSwitch]
        public string Layer { get; set; }

        [OrchardSwitch]
        public string Identity { get; set; }

        [OrchardSwitch]
        public string Owner { get; set; }

        [OrchardSwitch]
        public string Text { get; set; }

        [OrchardSwitch]
        public bool UseLoremIpsumText { get; set; }

        [OrchardSwitch]
        public bool Publish { get; set; }

        [OrchardSwitch]
        public string MenuName { get; set; }

        [CommandName("widget create")]
        [CommandHelp("widget create <type> /Title:<title> /Zone:<zone> /Position:<position> /Layer:<layer> [/Identity:<identity>] [/RenderTitle:true|false] [/Owner:<owner>] [/Text:<text>] [/UseLoremIpsumText:true|false] [/MenuName:<name>]\r\n\t" + "Creates a new widget")]
        [OrchardSwitches("Title,Name,Zone,Position,Layer,Identity,Owner,Text,UseLoremIpsumText,MenuName,RenderTitle")]
        public void Create(string type) {
            var widgetTypeNames = _widgetsService.GetWidgetTypeNames();
            if (!widgetTypeNames.Contains(type)) {
                Context.Output.WriteLine(T("Creating widget failed : type {0} was not found. Supported widget types are: {1}.", 
                    type,
                    widgetTypeNames.Aggregate(String.Empty, (current, widgetType) => current + " " + widgetType)));
                return;
            }

            var layer = GetLayer(Layer);
            if (layer == null) {
                Context.Output.WriteLine(T("Creating widget failed : layer {0} was not found.", Layer));
                return;
            }

            var widget = _widgetsService.CreateWidget(layer.ContentItem.Id, type, T(Title).Text, Position, Zone);
            var text = String.Empty;
            if (widget.Has<BodyPart>()) {
                if (UseLoremIpsumText) {
                    text = T(LoremIpsum).Text;
                }
                else {
                    if (!String.IsNullOrEmpty(Text)) {
                        text = Text;
                    }
                }
                widget.As<BodyPart>().Text = text;
            }

            widget.Name = Name;
            widget.RenderTitle = RenderTitle;

            if(widget.Has<MenuWidgetPart>() && !String.IsNullOrWhiteSpace(MenuName)) {
                // flushes before doing a query in case a previous command created the menu
                _contentManager.Flush();

                var menu = _menuService.GetMenu(MenuName);
                
                if(menu != null) {
                    widget.RenderTitle = false;
                    widget.As<MenuWidgetPart>().Menu = menu.ContentItem.Record;
                }
            }

            if (String.IsNullOrEmpty(Owner)) {
                Owner = _siteService.GetSiteSettings().SuperUser;
            }
            var owner = _membershipService.GetUser(Owner);
            widget.As<ICommonPart>().Owner = owner;

            if (widget.Has<IdentityPart>() && !String.IsNullOrEmpty(Identity)) {
                widget.As<IdentityPart>().Identifier = Identity;
            }

            Context.Output.WriteLine(T("Widget created successfully.").Text);
        }

        private LayerPart GetLayer(string layer) {
            var layers = _widgetsService.GetLayers();
            return layers.FirstOrDefault(layerPart => String.Equals(layerPart.Name, layer, StringComparison.OrdinalIgnoreCase));
        }
    }
}
Feb 16, 2013 at 10:11 PM
Hi Skywalker,

Very good help, think that we will try to make it like you - and report it as an bug.
Just wonna tell you, that I have learn a lot from your articles about orchard, realy good stuff :-)


Thanks


Peter
Developer
Feb 17, 2013 at 8:24 AM
Thanks :)