This project is read-only.

widget that shows selected fields from a blogpost

Topics: Writing modules
Oct 17, 2011 at 6:23 AM


What is the best way to achieve the following goal:

I added an imagefield to the blogpost content type and I managed to make the image appear in the summary view and the full view.

Now I want to have a widget in the sidebar that shows only the images associated with the blogposts (let's say that the widget will show 3 images). The posts to be shown by the widget should change on every page refresh.

Is there a way to tell which blogposts have an image and select only these matches in the widget? How can I only show the images without changing the summary template? How can I make the images change on each refresh?

I am waiting eagerly for your suggestions

Oct 17, 2011 at 5:35 PM

Because of the randomly taken content items, you need a different widget. You can reuse the one named Recent Content from the gallery, and convert the "recent" behavior to a random one, still letting users to select what content type to display.
Then users are able to change how it's displayed by enabling the Widget Alternates feature, and provide a template for a specific widget/shape. 

Oct 18, 2011 at 12:02 PM

Hi Sebastien,

thank you for your insight.

I installed the recent content widget and tried to generate a widget alternate but the option for it was not there! Widget alternates feature is activated and I checked with an htmlwidget and I was able to generate a widget alternate..

Is there some special code that I need to add in order to support widget alternates?

The interesting thing with the recent content widget is that it gives us the option to search for recent content based on the parts. this made me rise the following question: Of all the query examples that I have seen so far, we were only able to search for content having a particular type or part but what if we wanted to query the fields of a content?

In my case, I would like to show only the blog posts which have images in the widget because there are posts which don't have ones. What do you suggest?

Oct 18, 2011 at 5:53 PM

I have recently pushed a patch for th WidgetAlternates. There is an issue with them, you might want to take it from the 1.x branch.

And for querying over fields, you will need to way for next release, as I am currently working on it, part of the new Projections module, which by the way will solve you exact problem by default.

Oct 21, 2011 at 8:27 AM

Hi Sebastien,

I was trying during past days to add the "random" behavior to the recent content module. My idea was to select all content items returned by the recent content query and then select random indices to be shown by the widget.

A content query return an IEnumerable of ContentItem yet how can I access the imagefield of this ContentItem? What shall I write?(Obviously, I want to show only the images and thus I am only interested with posts that have an image).

I tried something like this :

 if (!String.IsNullOrWhiteSpace(queryResults[newIndex].Get(typeof(ContentPart)).Get(typeof(ImageField), "Image").FileName)) sw.WriteLine("True"); else sw.WriteLine("False");

but I am getting an error that says:

"The type or namespace name 'ImageField' could not be found (are you missing a using directive or an assembly reference?)"

but certainly ImageField as a type exists!

What do you suggest?

Oct 21, 2011 at 5:00 PM

Are you referencing the assembly where ImageField is defined?

Oct 22, 2011 at 9:08 AM
Edited Oct 22, 2011 at 2:25 PM

Oh my God! It seems that I am completely out of sync!

It is true, I needed to reference the ImageField assembly but after doing that, I only get null for image field even if there was an image. But hey! Do I really need to reference the assembly and casting the results to the correct type!? I don't think that I am doing it correctly.

Besides, it seems that the object that I am getting is not populated with anything!!

Forget everything that I wrote in my earlier post and please assist me in this scenario:

1- I am using this query to get the list of blogposts: (this is the same query of the Recent Content module, so I didn't write anything here)


var contentTypes = _contentManager.GetContentTypeDefinitions()
                .Where(x => x.Parts.Any(p => p.PartDefinition.Name == part.ForContentPart))
                .Select(x => x.Name)

            var query =_contentManager.Query(VersionOptions.Published, contentTypes)


2- then I am getting the count and list of results by :

 int count = query.Count();
                var queryResults = query.List().ToList() ;


3- Now suppose that I want to get the Image field of the blogpost for item  queryResults[0], what shall I write??

Oct 31, 2011 at 11:35 PM

I don't know, I would need to check out the image field. You can attach a debugger and find out. Should be something like queryResults.ContentItem.BlogPost.NameOfTheField.SomePropertyOfTheImageField.