Philip Holly
Philip Holly

Reputation: 753

Orchard CMS - How to implement fields in a custom form that behave differently on custom form page vs admin backend

I'd like to create a custom content type used in a form that has fields that behave differently when edited on the custom form page than from within the admin backend. The form will allow visitors to sign up to be notified when an online publication changes. I plan on using the form within an iframe inside another application.

Custom Content Type Fields

For users filling out the form on the custom form page (As opposed to creating a new content item from within the admin backend) I'd like to change the behavior of a few of the fields:

I'm new to Orchard and at first I was thinking about creating custom fields or custom parts or alternate views for the fields that used hidden inputs. After writing down the question I think I have a good solution but I'd like to know if there's a better way.

Possible Solution

  1. Create a custom content type specifically for the form that is different than the one above that only has FirstName, LastName, Occupation, Email, and PublicationId.
  2. Override the view for the form and instead of using a text input element for PublicationId use a hidden input that gets its value from a query string variable. Alternatively, create a custom view for just the PublicationId field.
  3. Using Workflows, upon form submission create an instance of the custom content type above. Set defaults for SubscribeDate and IsActive. I'm not sure how much control I have in Workflows, so alternatively create a handler for the form submission (does this exists?) that creates an instance of the custom content type above and sets the defaults.

Am I headed in the right direction? And should I create the custom content types in the admin UI or in code? I would like all my code related to this publication notification feature to be all in one spot so writing a module makes sense. Some other parts of this feature will be:

Thanks! Any input is greatly appreciated.

Upvotes: 0

Views: 839

Answers (1)

krbnr
krbnr

Reputation: 160

I think that you'll need to create a new content part, this content part should contain those fields (all of them). This part should be assigned to the new content type.

Create a field for the part (migrations)...

ContentDefinitionManager.AlterPartDefinition("YourPartName", builder => builder.WithField("YourFieldName", fieldBuilder => fieldBuilder.OfType("TextField").WithDisplayName("Your field name")));

After adding all the desired fields you should attach the new part to the new content type...

ContentDefinitionManager.AlterTypeDefinition("YourContentTypeName", 
    builder => builder.WithPart("YourPartName"));

Tip: Always use migrations, they allow you to have control and if you're working on different environments you'll have the same structure without having to create again the same parts/content types.

I recommend you to take a look at parts and drivers.

Hope this helps.

Regards.

Upvotes: 1

Related Questions