override Settings!?

Topics: Writing modules
Sep 5, 2011 at 9:54 PM

I'm willing to add new settings for the body part so I can limit the user input to the text field of the body part and restrict HTML tags or Javascripts ( XSS ) at a new module , any suggestions ?

Developer
Sep 6, 2011 at 1:34 AM

Body editor is rendered by TinyMce module. There is a shape Body-Html.Editor.cshtml inside (which builds the TinyMCE editor), which you can override in your module, adding necessary restrictions.

If you'd need some custom settings to be able to edit them via GUI (as opposed to hardcoding them in the shape file), I'd add a separate site settings part with your custom settings and access them from inside the overridden Body-Html.Editor.cshtml shape like this:

@{ 
    var settings = WorkContext.CurrentSite.As<MyCustomSettings>());
}
Sep 6, 2011 at 9:10 AM

Thanks pszmyd ...

I have searched for how to override the body shape of the TinyMce ,

still don't know how to do it but I have copied and pasted the Body-Html.Editor.cshtml to my module views,

now it uses the shape at module ,

still don't know how to define the new setting to override the mechanism of saving the body text ,

I have tried to access the Model properties but it's dynamic and wonder about how to edit the model 

I trying to review "Settings topic in here "http://www.szmyd.com.pl/blog/using-custom-settings-in-orchard-part-2-content-type-settings"

Sep 6, 2011 at 2:01 PM
Edited Sep 6, 2011 at 2:01 PM

I'm not sure but I think you would have to modify Orchard.Core to achieve this as I don't think you can override the DriverResult of Orchard.Core.Common.Drivers.BodyPartDriver to perform the validation from inside a module.  I don't think there is anyway to override the methods of the Drivers but I may be wrong...

Developer
Sep 6, 2011 at 7:51 PM

Did you ever manage to findout how to override the TinyMce settings?

Coordinator
Sep 6, 2011 at 8:07 PM

You can have several Drivers for the same part, and they will be triggered based on dependencies. Thus you could alter the body text.

Sep 8, 2011 at 2:22 PM

great advice sebastienros I didn't know it, I hope u write a book about Orchard soon! so novices like me can get on fast!, about the module yes I'm applying the idea and if any troubles I will ask for help again

Jetski I'm not sure what are you talking about but thanks for sharing

grokmonsieur , I guess we can can I'm applying the idea already many of the things at the core needs to be customized upon ur exact needs not for the default 

 

Developer
Sep 8, 2011 at 3:41 PM

Btw, what Sebastien wrote also applies to Handlers, too.

Sep 9, 2011 at 3:10 PM

Thanks pszmyd, I wish you write a book for Orchard too :) ,

now I'm having another problem I almost copied the driver of the body part and the edit view model and my own edit view for a flavor called limited , when I run it shows the view twice I tried to debug alot but I feel like swiming in the ocean , wonder why that happens , plus when I trying to save with some customization to the BodyEditorViewModel.cs in my module like this 

public class BodyEditorViewModel    {       

TagsHelper _helper = new TagsHelper();
        public BodyPart BodyPart { get; set; }
        public string Text        {  get { return BodyPart.Record.Text; }  set { BodyPart.Record.Text = _helper.RestrictTags(ref value); }   }       

where _helper.RestrictTags(ref value); implementation is 

public string RestrictTags(ref string text) { 

                      if (!String.IsNullOrEmpty(text)){ 

              var restrictedTags = new List<Tag>(){  new Tag {TagName = "<script>"},  new Tag {TagName = "</script>"},  new Tag {TagName = "<html>"},  new Tag {TagName = "</ html>"} };

                foreach (var tag in restrictedTags) {  text = text.Replace(tag.TagName, "");}  

             return text; }

 

It doesn't replace the text , where exactly the new value of the text filed is saved to the database 'persisted'  I'm still searching

If any suggestions i will be thankful

Sep 9, 2011 at 6:08 PM

I have done 50% of that task now I can save the data I want into the text field ,remaining how to prevent the duplicated views ?

Coordinator
Sep 9, 2011 at 6:37 PM

Hard to say without seeing the driver code.

Sep 9, 2011 at 7:34 PM

I didn't anything special I've copied and pasted the core driver 

and added little code to be able to apply the restrictions I want before saving to the database 

and that's what I have changed

private static Orchard.Core.Common.ViewModels.BodyEditorViewModel BuildEditorViewModel(BodyPart part) {

            var helper = new TagsHelper();

            var _orcEditBodyViewModel = new Orchard.Core.Common.ViewModels.BodyEditorViewModel();

            var _MohaEditBodyViewModel = new Mohamoon.LimitedBody.ViewModels.BodyEditorViewModel

            {

                BodyPart = part,

                EditorFlavor = GetFlavor(part),

            };

          

            _orcEditBodyViewModel.BodyPart = _MohaEditBodyViewModel.BodyPart;

            _orcEditBodyViewModel.EditorFlavor = _MohaEditBodyViewModel.EditorFlavor;

            _orcEditBodyViewModel.Text = helper.RestrictTags(_orcEditBodyViewModel.Text);

            return _orcEditBodyViewModel;

        }

and here is the full driver code

using Mohamoon.LimitingBodyPart.Helpers; // My module

namespace Mohamoon.LimitedBody.Drivers

{

    [UsedImplicitly]

    public class BodyPartDriver : ContentPartDriver<BodyPart>

    {

        private readonly IEnumerable<IHtmlFilter> _htmlFilters;

         private const string TemplateName = "Parts.Common.Body";

         public BodyPartDriver(IOrchardServices services, IEnumerable<IHtmlFilter> htmlFilters)

        {

            _htmlFilters = htmlFilters;

            Services = services;

        }

         public IOrchardServices Services { get; set; }

         protected override string Prefix

        {

            get { return "Body"; }

        }

         protected override DriverResult Display(BodyPart part, string displayType, dynamic shapeHelper)

        {

            return Combined(

                ContentShape("Parts_Common_Body",

                             () =>

                             {

                                 var bodyText = _htmlFilters.Aggregate(part.Text, (text, filter) => filter.ProcessContent(text));

                                 return shapeHelper.Parts_Common_Body(ContentPart: part, Html: new HtmlString(bodyText));

                             }),

                ContentShape("Parts_Common_Body_Summary",

                             () =>

                             {

                                 var bodyText = _htmlFilters.Aggregate(part.Text, (text, filter) => filter.ProcessContent(text));

                                 return shapeHelper.Parts_Common_Body_Summary(ContentPart: part, Html: new HtmlString(bodyText));

                             })

                );

        }

         protected override DriverResult Editor(BodyPart part, dynamic shapeHelper)

        {

            var model = BuildEditorViewModel(part);

            return ContentShape("Parts_Common_Body_Edit",

                                () => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: model, Prefix: Prefix));

        }

         protected override DriverResult Editor(BodyPart part, IUpdateModel updater, dynamic shapeHelper)

        {

            var model = BuildEditorViewModel(part);

            updater.TryUpdateModel(model, Prefix, null, null);

            return ContentShape("Parts_Common_Body_Edit",

                                () => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: model, Prefix: Prefix));

        }

         protected override void Importing(BodyPart part, Orchard.ContentManagement.Handlers.ImportContentContext context)

        {

            var importedText = context.Attribute(part.PartDefinition.Name, "Text");

            if (importedText != null)

            {

                part.Text = importedText;

            }

        }

         protected override void Exporting(BodyPart part, Orchard.ContentManagement.Handlers.ExportContentContext context)

        {

            context.Element(part.PartDefinition.Name).SetAttributeValue("Text", part.Text);

        }

     /************************************ that's what I've added *******************************************/

         private static Orchard.Core.Common.ViewModels.BodyEditorViewModel BuildEditorViewModel(BodyPart part) {

            var helper = new TagsHelper();

            var _orcEditBodyViewModel = new Orchard.Core.Common.ViewModels.BodyEditorViewModel();

            var _MohaEditBodyViewModel = new Mohamoon.LimitedBody.ViewModels.BodyEditorViewModel

            {

                BodyPart = part,

                EditorFlavor = GetFlavor(part),

            };

     

            _orcEditBodyViewModel.BodyPart = _MohaEditBodyViewModel.BodyPart;

            _orcEditBodyViewModel.EditorFlavor = _MohaEditBodyViewModel.EditorFlavor;

            _orcEditBodyViewModel.Text = helper.RestrictTags(_orcEditBodyViewModel.Text);

            return _orcEditBodyViewModel;

        }

 

        private static string GetFlavor(BodyPart part)

        {

            var typePartSettings = part.Settings.GetModel<BodyTypePartSettings>();

            return (typePartSettings != null && !string.IsNullOrWhiteSpace(typePartSettings.Flavor))

                       ? typePartSettings.Flavor

                       : part.PartDefinition.Settings.GetModel<BodyPartSettings>().FlavorDefault;

        }

 

        class PathBuilder

        {

            private readonly IContent _content;

            private string _path;

 

            public PathBuilder(IContent content)

            {

                _content = content;

                _path = "";

            }

 

            public override string ToString()

            {

                return _path;

            }

 

            public PathBuilder AddContentType()

            {

                Add(_content.ContentItem.ContentType);

                return this;

            }

 

            public PathBuilder AddContainerSlug()

            {

                var common = _content.As<ICommonPart>();

                if (common == null)

                    return this;

 

                var routable = common.Container.As<RoutePart>();

                if (routable == null)

                    return this;

 

                Add(routable.Slug);

                return this;

            }

 

            public PathBuilder AddSlug()

            {

                var routable = _content.As<RoutePart>();

                if (routable == null)

                    return this;

 

                Add(routable.Slug);

                return this;

            }

 

            private void Add(string segment)

            {

                if (string.IsNullOrEmpty(segment))

                    return;

                if (string.IsNullOrEmpty(_path))

                    _path = segment;

                else

                    _path = _path + "/" + segment;

            }

        }

    }

}

Coordinator
Sep 9, 2011 at 7:46 PM

Oh, so you added your own driver for the regular Body part? Well, no wonder you'd get duplicated shapes then. There can be (and is, apparently) more than one driver for a part. Maybe what you need to do is modify the shape that the regular driver is creting, rather than create your own.

Sep 9, 2011 at 8:13 PM

thanks for the fast reply ..
do you mean to modify the driver at Orchard Core ?

Coordinator
Sep 9, 2011 at 8:17 PM
No. I mean write a handler that modifies the existing shape.

Sent from my TI-99/4A

From: orchB
Sent: Friday, September 09, 2011 1:13 PM
To: Bertrand Le Roy
Subject: Re: override Settings!? [orchard:271556]

From: orchB

thanks for the fast reply ..
do you mean to modify the driver at Orchard Core ?

Sep 9, 2011 at 8:40 PM

sorry I'm not sure how to do it are there any module applied that Idea so i can have a grasp of what can I do exactly , appreciate ur effort here and thanks for ur time

Coordinator
Sep 9, 2011 at 8:46 PM

http://weblogs.asp.net/bleroy/archive/2011/01/10/orchard-shapeshifting.aspx

Sep 9, 2011 at 8:52 PM

thanks again for the amazing fast reply and the great help if any issues I will tell you , and great I idea about the Orchard Book project 

Sep 10, 2011 at 2:37 PM

Thanks for every one who helped here , I'm done with that task : ))