Adding a new text field to a widget dynamically

Topics: Customizing Orchard, Writing modules
Nov 3, 2011 at 8:19 PM

I am creating a widget for a client. It has text fields for product descriptions that the user will fill out. Very simple. However I'm not sure how many product fields they will need to fill out. It could be anywhere from 3 to 15 or more. This needs to be a widget as opposed to a content part. So they can easily fill out the information and place it on a specific place on he page. My question is, how can I offer the user, once they are filling out the fields in the widget, to click a button and add another product field dynamically. I know this can be done when building a content type. you can add as many fields as you want. But I have not seen any tutorials on how to add more fields to a widget on the fly. Any suggestions?

Coordinator
Nov 3, 2011 at 9:37 PM

You have already found something similar. Look at the code.

Nov 3, 2011 at 9:51 PM

I'm sorry, I don't understand. Which code?

Coordinator
Nov 3, 2011 at 9:53 PM

You said "I know this can be done when building a content type. you can add as many fields as you want."

Nov 3, 2011 at 10:13 PM

So your saying after I enable my widget I can add other fields to it like Contrib.ImageField or Contrib.MediaPicker  and they will always be there every time I use my widget? I didn't know Orchard had this functionality already. I thought that was only for custom content types.  I will look into it. Thanks!

Nov 3, 2011 at 10:15 PM

Widgets are just custom content types, they have a special setting of Stereotype="Widget" (instead of Stereotype="Content" for normal items...)

Nov 3, 2011 at 10:23 PM

I created a new custom content type with image fields and hyperlink fields and then added my widget to that content type.

 Basically I need it to work the other way around. I need to add additional fields to my widget, not add my widget to a custom content type. Again, I have 3 product fields in my widget. But the user may need to add more than 3 products to the widget. I want to add more fields as needed to my widget when the user is filling out the product fields and then adding the widget to a specific layer/zone on a page. Does that make sense? Or is there a way to add a custom content type to a layer/zone like I can add widgets to a layer/zone?

Coordinator
Nov 3, 2011 at 10:30 PM

I think you're taking this from the wrong end. Why don't you jsut create a n-n relationship? There is a tutorial for that.

Nov 3, 2011 at 10:46 PM

Ok. I did see that tutorial. I plan on using that to build my more complex Widget where I need to pull data from my custom data tables. It looks like It may work for that too.

 

2 questions:

 

1. Is there anyway to completely delete all traces of a Module or Widget? I built a module/widget and then uninstalled it (Basically just disabled it and removed the folder from my Orchard install). I rebuilt the widget. And installed it. Now I have 2 instances of the Widget in my Modules page. Even though the old module was disabled/deleted.

2. Bertrand, on your blog you spoke of a feature called The Projector feature. Any news on this new feature? It sounds great.

Nov 3, 2011 at 10:56 PM

Also the Widgets still exist in the Widget list when adding a widget to a layer. Even though I deleted the modules/widgets.

Nov 3, 2011 at 11:39 PM

@RandomPete

Are you saying I can add "Parts" to my Widget that are exposed by other Modules in My Orchard install? Such as ContribImageField? For example:

 

.WithPart("ProductPart")
.WithPart("BodyPart")
 .WithPart("WidgetPart")
.WithPart("CommonPart")
.WithPart("ImageFieldPart")

Imagefield part being the part taken from the Imagefield Module.

Nov 3, 2011 at 11:48 PM

A widget is just a content type. It's no different than any other content type, except for a single setting, and having the WidgetPart.

So, any parts or fields you can add to other content types can also be added to widgets.

Coordinator
Nov 3, 2011 at 11:57 PM

Answering to the post 4 up: Projector is almost done. You can check it out by syncing to the latest in the 1.x branch.

Nov 4, 2011 at 12:08 AM

I'd love to check it out.  Although, I don't understand "syncing to the latest in the 1.x branch." 

Coordinator
Nov 4, 2011 at 12:29 AM

It means you need to clone the source repository and update to the latest change in the 1.x branch. If you don't understand that, you should read some Mercurial documentation, or wait for it to be released.

Nov 4, 2011 at 12:52 AM

Thanks. Where can I find the Source repository? Is it on codeplex? Do you have a link?

Coordinator
Nov 4, 2011 at 12:54 AM

See that "Source code" tab on top of the page? ;)

Nov 4, 2011 at 1:10 AM

Yeah I see. Thanks. I don't have a clue what to do with the source code. I was just looking for the module or whatever. Thanks though.

Nov 4, 2011 at 1:15 AM

I downloaded Syncing with Orchard.Projections.zip .I opened the Visual studio project file in src folder. I don't see Projection Feature or module anywhere.

Coordinator
Nov 4, 2011 at 1:18 AM

That's because it's in a subrepository, you won't get it from the zip. As I said, you might want to read a bit about Mercurial.

Nov 4, 2011 at 1:50 AM

I found the source code. Thanks! By the way, In short, and in laymans terms, can you explain what functionality the Projections Module will be adding to Orchard?

Coordinator
Nov 4, 2011 at 2:52 AM

Query the content database from the admin.

Nov 13, 2011 at 2:06 PM

I think this post was diverted. ( no widget, no Projections...)

at post 1: I'm not sure how many product fields they will need to fill out. It could be anywhere from 3 to 15 or more(could be 98,99, 100,indeed it is x)

add another product field dynamically.

I also have this requirement. what i can think is to use js to add  field and field in front end, but i do not know how to connect this field of front end to backend field (Property,column), which could be orchard field or part.

I do not think n-n can resolve this,because both sides of n-n have fixed fileds(properties) when you defined the parts. it can not add properties dynamically.

what i have done now is : if i estimated it was under 100 fields , i added (fixed) 100 orchard fields to a part ,then content type before user can  add the field (properties) dynamically.( from front end, i think)

I only see that clay has this ability, but I do not know how it connect to js in front end, and doubt that orchard perform this  function in the present projects.

any hint  to the diretion would be appreciated.

andy

Nov 14, 2011 at 10:58 AM
Edited Nov 14, 2011 at 10:59 AM

Basically, it's very difficult to do what you describe (right now)

*However* I've been writing this module called Mechanics that allows you to create n:n relationships between content items. For this scenario you should wait until I release the next version which will handle large pools of content much better (at the moment you'd get a huge list of checkboxes for every product item you have, which won't be ideal if you have hundreds of thousands of products...)

Jan 31 at 2:44 PM
Picking up an old thread.
Is there a solution to this without using taxonomies or content-pickers?

Is it possible ootb to add textfields dynamically to a content type when we instantiate it?
If not, would it be possible to develop such a field? Dynamic text field or something :)