Search code examples
hookpiranha-cms

Using hooks to trigger a process


I am trying to work out how to use the Hooks and just can't seem to get the syntax correct.

I have built a site using PirahnaCMS that has a blog component and am extending it to call some social plugins and auto post to FB, Twitter etc.

I just can't seem to get the syntax correct though. My app is MVC and I have looked at this section

1.2 ASP.NET MVC

If you're using ASP.NET MVC hooks should be attached in you Global.asax.cs in the Application_Start method, or any other place where you keep you startup code. You attach you hooks with the followin syntax:

protected void Application_Start() {


  Piranha.WebPages.Hooks.Menu.RenderItemLink = (ui, str, title, url) => {
    str.Append(String.Format("<span><a href=\"{0}\">{1}</a></span>", url, title)) ;  
  } ;
} 

The Hook I believe I want to use is Piranha.WebPages.Hooks.Manager.PostEditModelAfterSave but for the life of me I can't seem to work it out.


Solution

  • All of the hooks are just static delegates that you can attach methods to. In the above example an anonymous method has been assigned to the hook with the syntax:

    delegate += (parameters) => { method body }
    

    You could also assign a previously declared method.

    delegate += MyMethod
    

    Example skeletons for attaching hooks should be available in the Docs at the official site. If not you can find the hooks in the file:

    ~/WebPages/Hooks.cs
    

    And all delegates in:

    ~/Delegates.cs
    

    I hope these URL:s are correct as I'm typing from memory :)

    Regards