Orchard CMS work with MediaPickerField defined in Migrations.cs - orchardcms

MediaPickerFields still elude me.
I've defined a new Part in Migrations.cs and added a Boolean column and a MediaPickerField to the part as follows:
SchemaBuilder.CreateTable("ImageContentPartRecord", table =>
table.ContentPartRecord()
.Column("DisplayImage", DbType.Boolean));
ContentDefinitionManager.AlterPartDefinition("ImageContentPart", builder =>
builder
.Attachable()
.WithField("ImageField", fld =>
fld.OfType("MediaPickerField")
.WithDisplayName("Image")));
Assuming I have ImageContentPart and ImageContentPartRecord classes, how can I retrieve the data from my MediaPickerField (url, dimensions, alt text, class, etc) in my Driver and in my Part Templates (Edit / Display)?
i.e. - Parts.ImageContent.cshtml (I want to accomplish something like this):
<div>
<img src="#Model.ImageField.Url" alt="#Model.ImageField.Alt" />
</div>
Any ideas?

Here is the solution:
In my Display Driver method, I make sure to pass my ImageContentPart(part) when building the ContentShape:
return ContentShape("Parts_ImageContent", () =>
shapeHelper.Parts_ImageContent(
Content: part));
Then in my Template View, I leverage the dynamic nature of Orchard's architecture to consume my MediaPickerField. To do this, you access the .ContentItem property on your part, then leaning on dynamics, chain the part name (.ImageContentPart) and then the field name (.ImageContent) to access the field.
#{
// Attempting to access MediaPickerField named 'ImageContent'
var image = Model.Content.ContentItem.ImageContentPart.ImageContent;
// Leaning on Orchard dynamics to access properties of the field
var url = image.Url;
}
<img src="#url" alt="#T(image.AlternateText)" />
Here is an exhaustive list of MediaPickerField properties from \\Orchard.Fields\Views\Fields\MediaPicker.cshtml:
#*
Alternate Text: #Model.ContentField.AlternateText
Class: #Model.ContentField.Class
Style: #Model.ContentField.Style
Alignment: #Model.ContentField.Alignment
Width: #Model.ContentField.Width
Height: #Model.ContentField.Height
Url: #Model.ContentField.Url
You can also display an image using this example, and add the attributes you need:
<img src="#Href(Model.ContentField.Url)" />
*#
Hopefully, if you stumble upon a similar problem, this will help out!

Related

Image Metadata: altText , height in Orchard CMS

For many of Custom Types created, we have a Query for them. These queries are being used by Projection Widgets (Within Zones).
Few of the custom types have Media Picker field.The Layout type I used for my Queries is the Shape type as seen below:
=>>> Queries:
=>>> Layout:
. I have followed the steps from here.. I specified the name of the shape as : UpcomingHighlightsImages as seen below:
and then included the view: UpcomingHighlightsImages.cshtml in my Themes/MyFirstTheme/Views folder.
Everything works fine upto here.
In the View, problem is that there is NO way to read the Image Metadata such as altText, altHeight etc... Neither there seems to be a way to set these metadata firstly in Orchard itself.
#using Orchard.ContentManagement
#using Orchard.Core.Title.Models
#using Orchard.Fields.Fields
#using Orchard.Taxonomies.Fields
#using Orchard.Core.Common.Fields;
#using Orchard.MediaLibrary.Fields;
#{
var HighlightItems = ((IEnumerable<ContentItem>)Model.ContentItems).ToList();
}
#foreach (var item in HighlightItems)
{
String LinkUrl = ((TextField)item.Parts.SelectMany(x => x.Fields).Single(x => x.Name == "LinkURL")).Value;
String ImagePath = ((MediaLibraryPickerField)item.Parts.SelectMany(x => x.Fields).Single(x => x.Name == "MainImage")).MediaParts.First().MediaUrl;
<div>
<a target="_blank" href="#LinkUrl">
<img src="#ImagePath" />
</a>
</div>
}
So, as seen in above code and the tag, I need:
Set the altText, altHeight property of Image in ORchard CMS
Read these in my view , the way I read LinkUrl and ImagePath
Please guide me !
See how you got the first media part in that ImagePath expression? Well, then you can take that part and get Title, Caption, AlternateText, etc. from it. You can also As<ImagePart>() it and get its Width and Height.

Orchard CMS - Extending Users with Fields - exposing values in Blog Post

I'd like to extend the users content definition to include a short bio and picture that can be viewed on every blog post of an existing blog. I'm unsure of what the best method to do this is.
I have tried extending the User content type with those fields, but I can't seem to see them in the Model using the shape tracing tool on the front end.
Is there a way to pass through fields on the User shape in a blog post? If so, what is the best way to do it?
I also have done this a lot, and always include some custom functionality to achieve this.
There is a way to do this OOTB, but it's not the best IMO. You always have the 'Owner' property on the CommonPart of any content item, so in your blogpost view you can do this:
#{
var owner = Model.ContentItem.CommonPart.Owner;
}
<!-- This automatically builds anything that is attached to the user, except for what's in the UserPart (email, username, ..) -->
<h4>#owner.UserName</h4>
#Display(BuildDisplay((IUser) owner))
<!-- Or, with specific properties: -->
<h1>#T("Author:")</h1>
<h4>#owner.UserName</h4>
<label>#T("Biography")</label>
<p>
#Html.Raw(owner.BodyPart.Text)
</p>
<!-- <owner content item>.<Part with the image field>.<Name of the image field>.FirstMediaUrl (assuming you use MediaLibraryPickerField) -->
<img src="#owner.User.Image.FirstMediaUrl" />
What I often do though is creating a custom driver for this, so you can make use of placement.info and follow the orchard's best practices:
CommonPartDriver:
public class CommonPartDriver : ContentPartDriver<CommonPart> {
protected override DriverResult Display(CommonPart part, string displayType, dynamic shapeHelper) {
return ContentShape("Parts_Common_Owner", () => {
if (part.Owner == null)
return null;
var ownerShape = _contentManager.BuildDisplay(part.Owner);
return shapeHelper.Parts_Common_Owner(Owner: part.Owner, OwnerShape: ownerShape);
});
}
}
Views/Parts.Common.Owner.cshtml:
<h1>#T("Author")</h1>
<h3>#Model.Owner.UserName</h3>
#Display(Model.OwnerShape)
Placement.info:
<Placement>
<!-- Place in aside second zone -->
<Place Parts_Common_Owner="/AsideSecond:before" />
</Placement>
IMHO the best way to have a simple extension on an Orchard user, is to create a ContentPart, e.g. "UserExtensions", and attach it to the Orchard user.
This UserExtensions part can then hold your fields, etc.
This way, your extensions are clearly separated from the core user.
To access this part and its fields in the front-end, just add an alternate for the particular view you want to override.
Is there a way to pass through fields on the User shape in a blog post?
Do you want to display a nice picture / vita / whatever of the blog posts author? If so:
This could be your Content-BlogPost.Detail.cshtml - Alternate
#using Orchard.Blogs.Models
#using Orchard.MediaLibrary.Fields
#using Orchard.Users.Models
#using Orchard.Utility.Extensions
#{
// Standard Orchard stuff here...
if ( Model.Title != null )
{
Layout.Title = Model.Title;
}
Model.Classes.Add("content-item");
var contentTypeClassName = ( (string)Model.ContentItem.ContentType ).HtmlClassify();
Model.Classes.Add(contentTypeClassName);
var tag = Tag(Model, "article");
// And here we go:
// Get the blogPost
var blogPostPart = (BlogPostPart)Model.ContentItem.BlogPostPart;
// Either access the creator directly
var blogPostAuthor = blogPostPart.Creator;
// Or go this way
var blogPostAuthorAsUserPart = ( (dynamic)blogPostPart.ContentItem ).UserPart as UserPart;
// Access your UserExtensions part
var userExtensions = ( (dynamic)blogPostAuthor.ContentItem ).UserExtensions;
// profit
var profilePicture = (MediaLibraryPickerField)userExtensions.ProfilePicture;
}
#tag.StartElement
<header>
#Display(Model.Header)
#if ( Model.Meta != null )
{
<div class="metadata">
#Display(Model.Meta)
</div>
}
<div class="author">
<img src="#profilePicture.FirstMediaUrl"/>
</div>
</header>
#Display(Model.Content)
#if ( Model.Footer != null )
{
<footer>
#Display(Model.Footer)
</footer>
}
#tag.EndElement
Hope this helps, here's the proof:

Sharepoint 2013 JSLink TaskList custom item rendering with out of the box options

I was not able to find the way to customize SP2013 Task List item rendering with JSLink in order to completely change the way that list item is rendered BUT also keeping all out of the box functionalities provided by default.
I mean, I'd like to display list elements as colored boxes, but also keeping sorting options, "..." (Open Menu) icon etc.
How can I achieve it? Is there any documentation where I can find lists of all internal fields like PercentComplete etc. which rendering can be overriden?
Any code snippets would be really appreciated!
Thanks a lot!
Take a look here
In a nutshell, what you want to do is, on the Templates object in the override context object add an object that is called Fields. In this object attributes named the same as the static name of a column(field) are used for rendering the value using the 'View' attribute. So, the example from the link is:
var overrideCtx = {};
overrideCtx.Templates = {};
// Override field data
overrideCtx.Templates.Fields = {
// PercentComplate = internal name of the % Complete
// View = you want to change the field rendering of a view
// <div ... = here we define what the output of the field will be.
'PercentComplete': { 'View' : '<div style="background: #F3F3F3; display:block; height: 20px; width: 100px;"><div style="background: #0072C6; height: 100%; width: <#=ctx.CurrentItem.PercentComplete.replace(" %", "")#>%;"></div></div>' }
};
// Register the override of the field
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx
Using this method you will retain the default functionality in the other fields. Just make sure the column is visible in the current view.

How to extend Orchard navigation module to add images to menu items

UPDATE: I've changed the original question drastically based on Bertrand's suggestions and my own findings. Now it provides an incomplete solution in its text instead of my own blind meanderings and commentary on Orchard, which were completely WRONG!
I need to display a menu using images instead of text, one standard, and another for when hovered/selected. The requirements for the site states that the end-user should be able to manage the menu item images. The standard navigation module now provides an HTML menu item, which is not what the end user wants. The customer wants a very simple, intuitive interface for configuring the sites many menus, and all menus are image-based.
Based on Bertrand's advice, and after realizing that Content Menu Item IS A CONTENT TYPE, I've created a new Content Part in the Admin Interface (not by code, I only want to write code for parts and content types when ultimately needed... I really want to see how far I can go with Orchard just by using the admin interface and templating/CSSing).
So, I've created a Menu Image Part, with two Content Picker fields added to it: Image and Hover Image. Then I've added this part to the Content Menu Item in the Manage Content Items admin interface.
Since I didn't write a Driver for it, the Model passed to the menu item template does not have an easily accessible property like #Model.Href... I've overriden the MenuItemLink-ContentMenuItem.cshtml with the following code so far:
#using Orchard.Core.Common.Models
#using Orchard.ContentManagement
#{
var contentManager = WorkContext.Resolve<IContentManager>();
var itemId = Model.Content.ContentItem.ContentMenuItemPart.Id;
ContentItem contentItem = contentManager.Get(itemId);
ContentField temp = null;
var menuImagePart = contentItem.Parts.FirstOrDefault(p => p.PartDefinition.Name == "MenuImagePart");
if (menuImagePart != null)
{
temp = menuImagePart.Fields.First();
}
}
<span>#temp</span>
#Model.Text
This yields the expected title for the Menu in a link, with a span before it with the following text:
Orchard.Fields.Fields.MediaPickerField
So all the above code (get the current content manager and the id of the ContentItem representing the ContentMenuItemPart, then use the content manager to get ContentItem itself, then linqing over its Parts to find the MenuImagePart (I can't use Get to get it because it requires a type and the MenuImagePart is not a type, it was created in the admin interface), then finally getting the first field for debugging purposes (this should be the Image field of the MenuImagePart I've created...)... all the above code actually got me to the Media Picker Field on my Meny Image Part...
What I'm not being able to do, and what makes me certainly a lot obtuse and stupid, is to find a way to read the MediaPickerField URL property! I've tried casting it to MediaPickerField, but I can't access its namespace from inside my template code above. I don't even know which reference to add to my theme to be able to add the following directive to it:
#using Orchard.Fields.Fields
I've finally succeeded in this task (thanks to Bertrand's direction).
UPDATE: And thanks again to Bertrand I've polished the solution which was running in circles, querying content items from the content manager when they were already available on the Model... now I'm leveraging the dynamic nature of content item, etc. And I'm finally satisfied with this solution.
It was necessary to create a new Content Part called Menu Image, then add this to the Content Type named Content Item Menu, and finally overriding the Content Item Menu template. This last part was the really tricky one. If it was not for Bertrand's directions the code bellow would have been smelly and daunting. The template ended up as follow:
#using Orchard.Utility.Extensions;
#using System.Dynamic
#{
/* Getting the menu content item
***************************************************************/
var menu = Model.Content.ContentItem;
/* Creating a unique CSS class name based on the menu item
***************************************************************/
// !!! for some reason the following code throws: 'string' does not contain a definition for 'HtmlClassify'
//string test = menu.ContentType.HtmlClassify();
string cssPrefix = Orchard.Utility.Extensions.StringExtensions.HtmlClassify(menu.ContentType);
var uniqueCSSClassName = cssPrefix + '-' + Model.Menu.MenuName;
/* Adds the normal and hovered styles to the html if any
***************************************************************/
if (menu.MenuImagePart != null)
{
if (!string.IsNullOrWhiteSpace(menu.MenuImagePart.Image.Url))
{
using(Script.Head()){
<style>
.#uniqueCSSClassName {
background-image: url('#Href(menu.MenuImagePart.Image.Url)');
width: #{#menu.MenuImagePart.Image.Width}px;
height: #{#menu.MenuImagePart.Image.Height}px;
display: block;
}
</style>
}
}
if (!string.IsNullOrWhiteSpace(menu.MenuImagePart.HoverImage.Url))
{
using(Script.Head()){
<style>
.#uniqueCSSClassName:hover {
background-image: url('#Href(menu.MenuImagePart.HoverImage.Url)');
width: #{#menu.MenuImagePart.HoverImage.Width}px;
height: #{#menu.MenuImagePart.HoverImage.Height}px;
}
</style>
}
}
}
}
<a class="#uniqueCSSClassName" href="#Model.Href">#Model.Text</a>
The only thing that I didn't understand is why I can't use HtmlClassify as an extension method with menu.ContentItem.HtmlClassify() and have to resort to calling the method as a standard static method (see the line with the comment `// !!! for some reason the following code throws´...)
Thanks again Bertrand!

Orchard - How to display custom Term fields in a taxonomy field?

I've got a contentType (product) that has a taxonomy field (features). The taxonomy term (product feature term) has been customized to include an image field and a description field.
I'd like for the product detail view to display the image from the term along with the name, but I can't find the property to access it.
I've created the following:
Taxonomy
ProductFeature Taxonomony
Vocabulary: Feat1, Feat2, Feat3
ContentTypes
Product
Fields: Features(Taxonomy)
Product Features Term
Fields: Description(Html), Image(Image)
Views
Fields.Contrib.TaxonomyField-Features.cshtml
<!-- Old Code -->
#if (Model.Terms.Count > 0) {
<p class="taxonomy-field">
<span class="name">#name.CamelFriendly():</span>
#(new HtmlString( string.Join(", ", terms.Select(t => Html.ItemDisplayLink(Html.Encode(t.Name), t.ContentItem ).ToString()).ToArray()) ))
</p>
}
<!-- New Code -->
#if (Model.Terms.Count > 0)
{
<div>
#foreach (var myTerm in Model.Terms)
{
#Display(???)
}
</div>
}
What do replace the question marks with? I'd thought it'd be myTerm.Image but that field doesn't exist on the dynamic object.
I've attached an image of the designer viewer.
If you wanted to use the current dev branch on the module, you could access the TermsPart of the content items, which leads you to all currently applied terms.
If you are using version 0.9 of the module, then you can dynamically have access to the fields by getting a reference to your Content Item, then do contentItem.PARTNAME.FIELDNAME. In the case of a type named Product, and a field name Feature it would be contentItem.Product.Feature. Then if this term has a property named Image, it will be termContentItem.ProductTerm.Image.
I would need more information to give you the exact syntax, like the type of field, exact name of content types. Or you can post the question on the module's codeplex project discussion forum.
As Sebastien helped me figure out over on http://orchardtaxonomies.codeplex.com/discussions/263844
Below is what ended up working.
(The key bit being: contentField = myTerm.ContentItem.Features.TermImage;)
#foreach (var myTerm in Model.Terms)
{
var contentField = myTerm.ContentItem.Features.TermImage;
if (!String.IsNullOrWhiteSpace(contentField.FileName)) {
<p class="image-field">
<img src="#Url.Content(contentField.FileName)" alt="#contentField.AlternateText" width="#contentField.Width" height="#contentField.Height"/>
</p>
}
}

Resources