Orchard CMS 1.10 adding Taxonomy with migration in a custom part - orchardcms

I am attempting to add a taxonomy to a custom type in orchard cms.
ContentDefinitionManager.AlterPartDefinition("ExpertPart",
b => b
.WithField("ExpertOf", fld => fld
.OfType("TaxononmyField")
.WithDisplayName("Expert Of")
.WithSetting("TaxonomyFieldSettings.Taxonomy", "ExpertOf")
.WithSetting("TaxonomyFieldSettings.LeavesOnly", "false")
.WithSetting("TaxonomyFieldSettings.SingleChoice", "true")
.WithSetting("TaxonomyFieldSettings.Required", "true")));
When i run this code the taxonomy shows up under "parts" and not "fields" in the content definition. I can manually add this to fields and it works fine. What is the new migration code for Orchard 1.10 that allows you to add a taxonomy field programatically to a custom content type?
Thanks for your help with this! So I have tried this.
ContentDefinitionManager.AlterTypeDefinition("Expert",
b => b.WithPart("ExpertPart"));
ContentDefinitionManager.AlterPartDefinition("ExpertPart",
b => b
.WithField("ExpertOf", fld => fld
.OfType("TaxononmyField")
.WithDisplayName("ExpertOf")
.WithSetting("TaxonomyFieldSettings.Taxonomy", "ExpertOf")
.WithSetting("TaxonomyFieldSettings.LeavesOnly", "false")
.WithSetting("TaxonomyFieldSettings.SingleChoice", "true")
.WithSetting("TaxonomyFieldSettings.Required", "true")));
When i run this migration, then go into the content definition of the part. The expert of is not listed under "fields" in the content definition. It it listed under "Parts". You can not get to the taxonomy settings. This is what happens
Orchard Problems
Also, the content edit screen does not have the taxonomy listed. So i cannot attach the expert part to the taxonomy.

You do this:
ContentDefinitionManager.AlterPartDefinition("ExpertPart", ...
So you say: Add the field to the ExpertPart. When you add a field manually in the dashboard to the 'Fields' section of a content type, Orchard adds it to the part with the same name of the content type (which is implicitly created with a content type). NOTE: This part does not really exists, but orchard creates it on the fly when the type is loaded.
So, assuming your custom content type is called 'Expert', Orchard adds the field in the dashboard to the part called 'Expert'.
Therefore, to let your field show up under Fields instead of under a part, your migrations should look like this:
// Orchard can only handle these migrations if you explicitly add the
// Expert part to the Expert content type
ContentDefinitionManager.AlterTypeDefinition("Expert", type => type
.WithPart("Expert"));
// Add the field to the part
ContentDefinitionManager.AlterPartDefinition("Expert", part => part
.WithField("ExpertOf", fld => fld
.OfType("TaxononmyField"));

Related

Access content field from views in Orchard CMS 1.10.1

I would like to display certain first level menuitem in bold.
This setting should be done by a checkbox when the user create / edit a menu item. (I have a workaround using the Model.Href, but it is not nice).
So I created a boolean field in Content definition / Menuitem URL (I don't know the name of the corresponding content definition in English Orchard).
How to access a custom field (Content Field) from a view?
(There already is a view which is used to customize the menu)
The examples I found use custom shapes, where the fields are accessed as built in fields (e.g. Model.ContentItem.FieldName ). But this is a different case.
With the help of "Piedone", the solution:
Model.Content.ContentItem.MenuItem.FieldTechnicalName.Value
Explanation
Examining the Model object in Visual Studio, the Model is a dynamic shape that have eg. Href property and a Content.
Content is a MenuPart, that is a content part that have a ContentItem property with the content item itself. Technically only content parts have Fields. When you (seemingly) add a field to a type it will be a part corresponding the type's name, that is MenuItem in this case (It's confusing that the display name of 'MenuItem' content type is Custom Link...)
The field's technical name is as you name it. When you add to a type, the Value will be a property of the BooleanField class. (By the way, it is nullable, so if you dont't save after adding the field, it will be null else the value you set).

Are Containable Parts and Widgets exclusive of each other?

I'm struggling with a decision to choose between creating a custom type as a Content Type with the Containable Part, or to create them as a Widget. I know that an existing Content Part can be used to create a new Widget, but this leaves me with having to create each instance from within the Widget Management section of the dashboard (I think). I'm not able to select a content item that already exists and have it act as a widget.
What I would really like is to have the ability to select a single, existing, Content Item and display it as a Widget. The widget could then expose a link that would take the user to the page-level display of that item. This would also allow me to have a list of these content types on a page where I also had html elements (html widgets) sprinkled in and amoungst them.
Alternatively, I could create my Content Type and attach the Containable Part. This would allow me to create the ad-hoc list (not a Projection) of content items I want. I would, however, have to create another Content Type to hold the HTML want to have appearing between the primary types.
Are my assumptions correct?: Must one create a new instance of a widget when they add it to a zone? Or can they select one from a list of existing content types?
Are Containable Parts and Widgets exclusive of each other?
Create a widget type with a content item picker field.
Thanks, Bertrand for the suggestion to create a widget type with a content item picker field. Here is my implementation:
public UpdateFrom1() {
ContentDefinitionManager.AlterTypeDefinition("BioPickerWidget", cfg => cfg
.WithPart("BioPickerWidget")
.WithPart("WidgetPart")
.WithPart("CommonPart")
.WithSetting("Stereotype", "Widget"));
ContentDefinitionManager.AlterPartDefinition("BioPickerWidget", cfg => cfg
.WithField("BioPicker", fb => fb
.OfType("ContentPickerField")
.WithSetting("ContentPickerFieldSettings.Hint", "Add Bio Content Items here.")
.WithSetting("ContentPickerFieldSettings.Required", "True")
.WithSetting("ContentPickerFieldSettings.Multiple", "True")
.WithSetting("ContentPickerFieldSettings.ShowContentTab", "True")
.WithSetting("ContentPickerFieldSettings.ShowSearchTab", "True")
.WithSetting("ContentPickerFieldSettings.DisplayedContentTypes", "BioPart")
.WithDisplayName("Bio Content Picker"))
.WithSetting("ContentPartSettings.Attachable", "True")
);
return 2;
}
This allows me to add 1 or more of my Bio Content Types to a widget. Seems to be working nicely. All I have to do now is figure out the view customization.

orchard cms: how to add media picker field to a custom part

My question is simmalar to questions/10369967/orchard-cms-how-to-add-media-picker-field-to-anew-module
I've created a new content part, that has a select list, a text box and ... I want to include a media picker filed.
I have added this to the content part with Bertrand Le Roy's suggestion of:
ContentDefinitionManager.AlterPartDefinition("Product",
builder => builder.WithField("ProductImage",
fieldBuilder => fieldBuilder
.OfType("MediaPickerField")
.WithDisplayName("Product Image")));
However I've no idea of how to show this in my custom editor View
I'm sure there should be something easy like #Display(Model.ProductImage) ... but I've followed the writing a content part form the Orchard docs, and my model in my editor view is not dynamic.
So if such magic as #Display(Model.ProductImage) exists, how do I add the media picker item to my view model ?
Update
The Media picker field seems to be showing on the content types where I add this part, just not where I want it! I want to show / hide this field biased on values selected form a select list, How can I stop the default rendering of the field Content Item and Render it in my custom view ?
Update 2
I've added this in the model
public MediaPickerField MediaPicker
{
get{ return (MediaPickerField)((dynamic)ContentItem).HeaderPart.Image;}
}
this to the view
#Html.Partial("EditorTemplates/Fields/MediaPicker.Edit", Model.MediaPicker)
and this is the placement.info
<!-- MediaPicker -->
<Place Fields_MediaPicker_Edit="-"/>
Now it looks right on the editing via cms end... but dosen't seem to save the images to the database!
Should be something like #Model.ContentItem.Product.ProductImage.Url

Orchard CMS 1.4: Adding a Text Field to Custom ContentPart

In the module for a custom ContentPart, how do I set a field to be a Text field?
In my migrations.cs class, I have created the table for the part:
public int UpdateFrom1()
{
SchemaBuilder.CreateTable("RightContentPartRecord", table =>
table.ContentPartRecord()
.Column<string>("Html"));
return 2;
}
So, I have a column called Html. I want to use the WYSIWYG editor, so I am told I need a Text field to get this to work "out of the box".
However, this isn't happening for me, so what do I need to do to turn my column called Html into a Text field on the part?
And how do I configure it to use the WYSIWYG editor?
If you want a property of your own custom contentpart to be displayed as a htmleditor, configure it as follows in the editortemplate of your part.
#Script.Require("OrchardTinyMce")
#Html.TextAreaFor(x => x.Header, new { #class = "html tinymce" })
In this case the 'Header' property is displayed as a html editor.
If you need this in more parts you can consider writing a Html extension or editor template for it.
A text field is not the same thing as a part property. Fields are not stored as their own database column. Here is an example of how you add a field to a part from a migration:
ContentDefinitionManager.AlterPartDefinition("Product",
builder => builder.WithField("ProductImage", fieldBuilder => fieldBuilder.OfType("MediaPickerField").WithDisplayName("Product Image")));
For text field, you'd also need to set the flavor setting by adding .WithSetting("Flavor", "html") to the field builder.

strange content type in drupal

i want to have a content type for my properties. let's name it 'my properties'.
and i have a taxonomy called 'my properties type' that contains 'books,shoes,clothes'.
the form for adding properties change on taxonomy selected item,because each of its type have different fields.
is it possible to have such content type in drupal ??
The solution within Drupal is to create separate content types for each property type. However, you can easily write some jquery to show/hide fields when taxonomy field is modified:
$('#your-taxonomy-dropdown').change(function() {
if($(this).val() == 'book') {
$('#field1').hide();
$('#field2').show();
}
});

Resources