embarus
embarus

Reputation: 815

How to set new Orchard module to be Home Page via code

I'm very new with orchard.

To learn orchard module development, I followed the documentation and tried to create a commerce module.

The module consists of product part and product type which has product part.

During enable module, it will create admin and home menu for this module, "Commerce" and "Shop" respectively.

My questions are

I am attaching source code, please download it from the following link

download source code

Upvotes: 3

Views: 2423

Answers (3)

Henry C
Henry C

Reputation: 4801

You can, when creating a page as part of migrations.cs in a module, tell the Autoroute part to set your created page's alias as the homepage:

        //create a page page
        var homepage = _contentManager.Create("Page");
        homepage.As<TitlePart>().Title = "My Home";            
        _contentManager.Publish(homepage);

        var homePageArp = homepage.As<AutoroutePart>();
        homePageArp.DisplayAlias = String.Empty;            
        _autorouteService.PublishAlias(homePageArp);

This assumes you're going from a clean instance of Orchard without any prior homepages; if you have an existing homepage, you'll have to regenerate those pages' Aliases as part of your module too. This is how it's done as part of the AutoroutePartHandler in the Orchard.Autoroute project (inside the Publish Alias method):

            // regenerate the alias for the previous home page
            var currentHomePages = _orchardServices.ContentManager.Query<AutoroutePart, AutoroutePartRecord>().Where(x => x.DisplayAlias == "").List();
            foreach (var current in currentHomePages) {
                if (current != null) {
                    current.CustomPattern = String.Empty; // force the regeneration
                    current.DisplayAlias = _autorouteService.Value.GenerateAlias(current);
                }
                _autorouteService.Value.PublishAlias(current);
            }


        _autorouteService.Value.PublishAlias(part);

If you dig through the driver and handler for the autoroute project, you'll learn a lot about the internals; when you tick that "set as homepage" box in the Admin UI, it sets the Path to "/" and then that gets picked up, triggers the old homepage re-wire, clears the "/" path to String.Empty and then publishes that blank alias, giving you a new homepage.

(this is valid as of Orchard 1.6)

Upvotes: 1

Bertrand Le Roy
Bertrand Le Roy

Reputation: 17814

To take over the home page the standard Orchard way is to implement IHomePageProvider.

Upvotes: 4

santiagoIT
santiagoIT

Reputation: 9421

If your module is to be used by others, then it is better to make a widget which can be added to any layer (the homepage layer for example). That way each user can decide where your module comes into play. If you are using this module for yourself only, then you can just override the default routes (standard mvc functionallity). Look at my ExtendedRegistration module (Routes.cs) to see how it's done.

Here I am overriding the standard Account/Register URL. There should be nothing preventing you from overriding the default HomeController.

public class Routes : IRouteProvider
    {

        public void GetRoutes(ICollection<RouteDescriptor> routes)
        {
            foreach (var routeDescriptor in GetRoutes())
            {
                routes.Add(routeDescriptor);
            }
        }

        public IEnumerable<RouteDescriptor> GetRoutes()
        {
            return new[] {

                    new RouteDescriptor {
                    Priority = 19,
                    Route = new Route(
                        "Users/Account/Register",
                        new RouteValueDictionary {
                            {"area", "itWORKS.ExtendedRegistration"},
                            {"controller", "Account"},
                            {"action", "Register"}
                        },
                        new RouteValueDictionary(),
                        new RouteValueDictionary {
                            {"area", "itWORKS.ExtendedRegistration"}
                        },
                        new MvcRouteHandler())
                }
            };
        }
    }

Upvotes: 0

Related Questions