Change theme in code

Topics: Customizing Orchard, Localization
May 7, 2013 at 9:48 AM
How I can change the theme in code not through the admin panel.

I added to my orchard new module (Culture Picker) to change the Site Culture from Arabic to English as you know the Arabic language work RTL and English LTR for this reason I want to change the theme when the user change the Site Culture.

for testing i created new class under App_Code to change the theme from TheThemeMachine to Contoso theme when start the website but the idea not working.
public class Theme : IThemeSelector {
        public ThemeSelectorResult GetTheme(RequestContext context) {
            return new ThemeSelectorResult { Priority = -5, ThemeName = "Contoso" };
        }
}
waiting help and suggestions.
Developer
May 7, 2013 at 3:23 PM
Does your GetTheme method gets executed at all?
Perhaps the theme needs its own project file, I don't know, but you can try.
In any case, I would like to suggest to create a module with your theme selector, instead of doing that in the theme.
May 15, 2013 at 12:06 PM
I created new module using Code Generator and add class to the root of the folder

`` using Orchard.Themes;
using System.Web.Routing;


public class RtlTheme : IThemeSelector
{
    public ThemeSelectorResult GetTheme(RequestContext context) {
        return new ThemeSelectorResult { Priority = 1, ThemeName = "Contoso" };
    }
}

``` But nothing happen when start the orchard
how i can debug my code
how i can know my code execute

please if any one have code or article or example about how i can change the theme on runtime
Developer
May 15, 2013 at 5:55 PM
Did you enable your module's feature?
You can see if your code is being hit by setting a breakpoint and attaching the debugger to your web server's process.
May 21, 2013 at 5:01 PM
Hi Zuhair,

I responded to the email you sent me regarding adding a module to change the theme based on the site culture. I thought I would post that response here in case others were interested:

What you're looking to do is actually fairly simple. You're going to want to create a file in the root directory of the module and name it something like CultureThemeSelector.cs

The file should look something like the following:
using System.Web.Routing;
using Orchard;
using Orchard.Themes;

namespace YOURMODULENAME {
    public class CultureThemeSelector : IThemeSelector {
        private readonly IOrchardServices _orchardServices;

        public CultureThemeSelector(IOrchardServices orchardServices) {
            _orchardServices = orchardServices;
        }

        public ThemeSelectorResult GetTheme(RequestContext context) {
            if (_orchardServices.WorkContext.CurrentCulture == "ar") {
                return new ThemeSelectorResult { Priority = 1, ThemeName = "MYARABICTHEME" };
            }

            return null;
        }
    }
}
In order for this to work properly, your English theme should be the default/enabled theme. You'll also need to enable the Arabic theme, but don't make it the default.

This class file will check the current site culture. If it is equal to "ar", then it will display the theme named MYARABICTHEME, otherwise it will display the default English theme. Obviously you'll need to change the name of the theme and the name of the module. You may also need to play around with the culture comparison...I'm not sure how your culture is going to appear or if it's going to be specific to a country (i.e. "ar-EG" for Egypt). You may need to change the if statement to .Contains("ar") if that suits your needs better. I haven't tested this, but it should at least get you going in the right direction.

Good luck...hope this information was helpful.
May 27, 2013 at 10:22 AM
Thanks Mr. Psenechal

I made what are explain above with small change to change the theme without check the Culture but the theme does not change i think the problem my module or class not execute in the execution cycle

How i add the Module to my Orchard
  • new orchard website using webmatrix
  • install codegen and enable
  • install Contoso theme and enable
  • create the module by command (codegen module CultureTheme) and enable
  • add new class to the root of module CultureThemeSelector.cs
using System.Web.Routing;
using Orchard;
using Orchard.Themes;

namespace CultureTheme {
    public class CultureThemeSelector : IThemeSelector {
        private readonly IOrchardServices _orchardServices;

        public CultureThemeSelector(IOrchardServices orchardServices) {
            _orchardServices = orchardServices;
        }

        public ThemeSelectorResult GetTheme(RequestContext context) {
            /*
            if (_orchardServices.WorkContext.CurrentCulture == "ar") {
                return new ThemeSelectorResult { Priority = 1, ThemeName = "Contoso" };
            }
            */
            return new ThemeSelectorResult { Priority = 1, ThemeName = "Contoso" };
        }
    }
}
  • run website from webmatrix but the theme does not change
Please if can help me how i can debug or trace the code to know where the problem.

Thanks in advance..
Coordinator
May 28, 2013 at 2:04 AM
Did you add the new class to the csproj for the module?
Jun 3, 2013 at 8:13 AM
Thanks for all
after I add my new class to the csproj it is working