0
votes

I am new to Orchard. So please forgive me if there is anything looking silly!

I want to create a custom widget for my Orchard website to encourage visitors to sign up for my Newsletter service. I have seen there is an option of using HTML widget but I want to create a new widget type like "Newsletter" which I shall use conditionally at AsideFirst block.

Is this possible to do? I only want to grab visitor's Name and Email address, and the form submission will be done using an action controller.

Do I have to create this widget through by-hand coding in VS? In fact I want to this way, not through the Orchard admin console.

Seeking for help. Any suggestion please?

Edit: I have managed to create the widget following Sipke Schoorstra's suggestion. The area where I want to display the widget is now showing along with the the title I set from admin at the time of adding it to a zone. But the content (form elements) I created in the view is not displaying.

The View: (Views/NewsLetterSignupPart/NewsletterSignup.cshtml)

@model Emfluence.Intrust.Models.NewsLetterSignupPart
@{
ViewBag.Title = "Newsletter Signup";
}
@using (Html.BeginForm("NewsletterSignup", "NewsLetter", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="row-fluid">
<div class="span6">
<label>Name</label>
<input type="text" name="txtNewsletterUserName" required maxlength="50" style="width: 95%" />
<label>Email</label>
<input name="txtNewsletterUserEmail" type="email" required maxlength="85" style="width: 95%" />
<button class="btn pull-right">Submit</button>
</div>
</div>
}

Migration.cs

public int UpdateFrom15()
{
ContentDefinitionManager.AlterTypeDefinition(
"NewsletterWidget", cfg => cfg
.WithPart("NewsLetterSignupPart")
.WithPart("CommonPart")
.WithPart("WidgetPart")
.WithSetting("Stereotype", "Widget")
);
return 16;
}

NewsLetterSignupPart.cs

public class NewsLetterSignupPart : ContentPart<NewsletterSignupRecord>
{
[Required]
public string Name
{
get { return Record.Name; }
set { Record.Name = value; }
}
[Required]
public string Email
{
get { return Record.Email; }
set { Record.Email = value; }
}
}

And NewsletterSignupRecord.cs

public class NewsletterSignupRecord : ContentPartRecord
{
public virtual string Name { get; set; }
public virtual string Email { get; set; }
}

Where I am doing wrong?

2
Can you expand on why you want to hard code this? This kind of feature is available by default using the Custom Forms module.David Fox
What is a Custom Form module? No, I do not want to hard code anything. All I want is to create a widget through code in my already built custom module. Orchard has some widget provided out of the box like Blog Archive, Projection Widget etc. I want to create another re-usable widget like these. Custom Form, which you mentioned, where I can find it in the admin? Can I use it like a widget / re-usable control?Subrata Sarkar

2 Answers

1
votes

The Custom Forms module is great if you don't want or need to code something yourself. In case you do want to handle form submissions yourself without using Custom Forms, this is what you could do:

  1. Create a custom module
  2. Create a migrations class that defines a new widget content type (see the docs for details on how to do this. Note: you don't need to create a custom part. You don't even need to create a migrations file to create a content type - you could do it using a recipe file. The nice thing about a migration though is that it will execute automatically when your module's feature is enabled).
  3. Create a view specific for content items of your widget type (e.g. Widget-Newsletter.cshtml).
  4. Inside of this view, write markup that includes a form element and input elements. Have this form post back to your controller.
  5. Create your controller.
0
votes

In the /admin interface, click Modules, on the Features` tab search for Custom Forms and click Enable. This will add a new Forms admin link on the left.

Next, create a custom content type (under Content Definition) called Newsletter, and add two fields (of type Text Field) called Name and E-mail.

Finally, click Forms and add a new Custom Form. Give it a title: this will be the default URL to access e.g. "Newsletter Form" will have a URL of /newsletter-form by Orchard defaults. Under Content Type select your newly created content type, Newsletter, from the dropdown. Customize anything else you want on this page, and click Publish Now

If you want to make this a widget, edit the content type and add the Widget Part. Create a layer with the rules you need and you can add the "Newsletter" widget to any zone you need on that layer.