Skip to content

Instantly share code, notes, and snippets.

@mgroves
Created January 19, 2012 20:43
Show Gist options
  • Save mgroves/1642453 to your computer and use it in GitHub Desktop.
Save mgroves/1642453 to your computer and use it in GitHub Desktop.
// latestcommentswidgetdriver.cs
using LatestComments.Models;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
namespace LatestComments.Drivers
{
public class LatestCommentsWidgetDriver : ContentPartDriver<LatestCommentsWidgetPart>
{
// GET
protected override DriverResult Display(LatestCommentsWidgetPart part, string displayType, dynamic shapeHelper)
{
return ContentShape("Parts_LatestCommentsWidget",
() => shapeHelper.Parts_LatestCommentsWidget(
NumberOfComments: part.NumberOfComments));
}
// GET
protected override DriverResult Editor(LatestCommentsWidgetPart part, dynamic shapeHelper)
{
return ContentShape("Parts_LatestComments_Edit",
() => shapeHelper.EditorTemplate(
TemplateName: "Parts/LatestComments",
Model: part,
Prefix: Prefix));
}
// POST
protected override DriverResult Editor(LatestCommentsWidgetPart part, IUpdateModel updater, dynamic shapeHelper)
{
updater.TryUpdateModel(part, Prefix, null, null);
return Editor(part, shapeHelper);
}
protected override void Importing(LatestCommentsWidgetPart part, Orchard.ContentManagement.Handlers.ImportContentContext context) {
var partName = part.PartDefinition.Name;
part.NumberOfComments = int.Parse(context.Attribute(partName, "NumberOfComments"));
}
protected override void Exporting(LatestCommentsWidgetPart part, Orchard.ContentManagement.Handlers.ExportContentContext context) {
var partName = part.PartDefinition.Name;
context.Element(partName).SetAttributeValue("NumberOfComments", part.NumberOfComments);
}
}
}
// latestcommentswidgetrecordhandler.cs
using LatestComments.Models;
using Orchard.ContentManagement.Handlers;
using Orchard.Data;
namespace LatestComments.Handlers
{
public class LatestCommentsWidgetRecordHandler : ContentHandler
{
public LatestCommentsWidgetRecordHandler(IRepository<LatestCommentsWidgetRecord> repository)
{
Filters.Add(StorageFilter.For(repository));
}
}
}
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Records;
// latestcommentswidget.cs
namespace LatestComments.Models
{
public class LatestCommentsWidgetRecord : ContentPartRecord
{
public virtual int NumberOfComments { get; set; }
}
public class LatestCommentsWidgetPart : ContentPart<LatestCommentsWidgetRecord>
{
[Required]
[DefaultValue("5")]
[DisplayName("Number of comments to show")]
public int NumberOfComments
{
get { return Record.NumberOfComments; }
set { Record.NumberOfComments = value; }
}
}
}
// latestcommentswidget.cshtml
@model LatestComments.Models.LatestCommentsWidgetPart
<fieldset>
<legend>Latest Comments</legend>
<div class="editor-label">
@T("Number of comments to display"):
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.NumberOfComments)
@Html.ValidationMessageFor(model => model.NumberOfComments)
</div>
</fieldset>
// latestcommentswidget.cshtml
<p>Hello World, showing @Model.NumberOfComments comments!</p>
// migrations.cs
using System.Data;
using LatestComments.Models;
using Orchard.ContentManagement.MetaData;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
namespace LatestComments {
public class Migrations : DataMigrationImpl {
public int Create() {
// Creating table LatestCommentsWidgetRecord
SchemaBuilder.CreateTable("LatestCommentsWidgetRecord", table => table
.ContentPartRecord()
.Column("NumberOfComments", DbType.Int32)
);
ContentDefinitionManager.AlterPartDefinition(typeof(LatestCommentsWidgetPart).Name,
builder => builder.Attachable());
return 1;
}
}
}
// module.txt
Name: LatestComments
AntiForgery: enabled
Author: Matthew D. Groves
Website: http://mgroves.com
Version: 1.0
OrchardVersion: 1.0
Description: Display latest comments for a user in a widget.
Features:
LatestTwitter:
Description: Display latest comments for a user in a widget.
Category: Widget
Dependencies: Orchard.Widgets
placement.info
<Placement>
<Place Parts_LatestCommentsWidget_Edit="Content:3"/>
<Place Parts_LatestCommentsWidget="Content:3"/>
</Placement>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment