Get the current page id in a widget

Feb 12, 2011 at 11:46 AM

Hi,

I'm creating a simple cshtml disqus comments widget that I am newing up in the same way as the BadgeOfHonor.cshtml file in the ThemeMachine theme.

All I want is the get access to current page id in the file. I have tried debugging the Model but the dynamic objects always seem to return empty. WorkContext didn't seem to contain this info either.

Anyone have any ideas? Is this stuff documented anywhere? 

I'm sorry for constantly asking questions but It seems I hit a brick wall and there is not documentation or blogs to follow on everything I hit. Maybe it's because the framework is young....

Cheers,

 

Ian

Coordinator
Feb 13, 2011 at 12:07 AM

It might help if you could explain exactly what you are trying to do. Shouldn't the Disqus comments be attached to the content item? If so, why don't you do the work from the driver, where it is trivial to get the content item's id?

Feb 13, 2011 at 9:52 AM
Edited Feb 13, 2011 at 1:59 PM

The tutorials I have seen regarding driver etc imply that you need to have a database record, migrations etc. I don't need these so just wanted something simple. This is what I have so far.

 

@{
    var currentUrl = Request.Url.AbsoluteUri;
    var pageId = ??;
    var isBlog = HttpContext.Current.Request.Url.AbsolutePath.StartsWith("/blog");
}
@if (isBlog)
{
    <p>Page Id @Model.Text</p> 
    <div id="disqus_thread"></div>
    <script type="text/javascript">
        /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
        var disqus_shortname = 'shortname'; // required: replace example with your forum shortname

        // The following are highly recommended additional parameters. Remove the slashes in front to use.
        var disqus_developer = 1;
        var disqus_identifier = '@pageId';
        var disqus_url = '@currentUrl';

        /* * * DON'T EDIT BELOW THIS LINE * * */
        (function () {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
        })();
    </script>
    <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
    <a href="http://disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a>
}

 

I insert it in the layout like so.

 

WorkContext.Layout.AfterContent.Add(New.DisqusComments(), "1");

 

Just need the page id and this will work. I didn't think I needed a driver etc for something this simple as I don't need any database records etc.

When you say attached to the content item, what do you mean. Should I create a new module with a driver etc for this simple widget?

How would I get the content Id from a widget driver? I'll have a go at creating a driver and see how i get on. 

Thanks,

 

Ian

Feb 13, 2011 at 11:25 AM

Again fallen at the first hurdle. I have a written a full module for disqus comments following this http://blog.maartenballiauw.be/post/2011/01/21/Writing-an-Orchard-widget-LatestTwitter.aspx tutorial.

Here is my module code.

http://rapidshare.com/files/447695966/DisqusComments.rar 

When I enabled the module I got an exception and now my site is blowing up.

None of the constructors found with policy 'Orchard.Environment.AutofacUtil.DynamicProxy2.ConstructorFinderWrapper' on type 'DisqusComments.Handlers.DisqusCommentsWidgetRecordHandler' can be invoked with the available services and parameters:
Constructor 'Void .ctor(Orchard.Data.Repository`1[DisqusComments.Models.DisqusWidgetRecord])' parameter resolution failed at parameter 'Orchard.Data.Repository`1[DisqusComments.Models.DisqusWidgetRecord] repository'.

How do I recover from this and debug the error? 

Thanks,

 

Ian

Feb 13, 2011 at 3:56 PM

I found the error. I was adding Repository<DisqusWidgetRecord> to my handler instead of IRepository. Everything is working now.

Coordinator
Feb 14, 2011 at 9:47 PM

Cool. Just commenting on the code you published above, there is a lot that is hard-coded assumptions up there that will make this code difficult to re-use (why limit it to /blog for example). If I had to build something like this, I would create a part that you would be able to attach to any content type (and not just blog posts). The part would not necessarily have settings but its driver would create a simple shape that would get rendered by a template that would look much like what you wrote, only with less hard-coded constants). Makes sense?