How do I access blog post custom fields from a custom widget?

I created a custom widget, which based on categories, allows me to use the blog posting system as a custom post type. Its not an ideal implementation, as the custom fields need to be defined for each post, but it will allow me to move forward until you guys put in a permenant solution for custom post types. My problem is I cant find a way to use the custom fields within the widget.

How is this done?


This is a code snippet I’m using in my custom widget to render the custom posts for the category :

for(var x=1; x lte arguments.max; x++){







I need to be able to access the custom fields I created from within this snippet.

Hi Robert, this is how you can get the value of the custom field in the widget.

var id = cb.getCustomField(‘nameoffieldIcreated’,0);

Is that what you were looking for?


George Murphy

This is what Im looking for and I’ve tried that already. It doesn’t seem to be available within a widget. If you look at the code snippet from the widget I created, I’m looping over the entries collection to display the entry list in a specific way. the cb scope doesn’t seem to be available from within a widget. I’m using the custom widget to create custom post types.

Hi Robert, does your custom widget component have this extends=“contentbox.model.ui.BaseWidget”

I am able to access the cb class inside my widgets. In models I have to inject that class. property name=“cb” inject=“id:CBHelper@cb” scope=“variables”


George Murphy

Thanks for the reply George. I think I found where the problem lies but let me give you a bit of background on what I’ve done and why.

I have a requirement for a client to create a roster of members page. In wordpress, this is simply a plugin that extends the base post framework with custom post types to allow you to predefine additional fields, the plugins also provide the interface side so when you add an entry, all your necessary fields are there for the post type, and display templates. This comes in very handy for all kinds of things, such as photo galleries, listings of all types.

Currently ContentBox does not provide a mechanism for defining custom post types, only custom fields per post. So in order to get around that limitation, I created a widget which displays blog posts based on a certain category. I created all the necessary posts for the members page with the necessary custom fields. I then created a new base page template with the base layout i needed, then created a new page using that template and inserted my custom widget into the page. it needs to be a page so it shows up in the menu. I don’t like doing it this way, but with my limited knowledge of ContentBox, it was the quickest way to get it done. Everything works fine, except when it comes to the custom fields and I now know why.

The custom fields are tied to a blog post, but the widget is actually displaying the individual blog entries from within a page. The get custom field function is looking to see if its being called from a page or an entry and since it sees its template as a page, it is not finding the custom fields for each entry. any idea how to get around this?

Hi Robert, I think I understand what you are doing. Is there anything in the event or RC or a custom field in the page that you can tap into by calling getRequestContext(somethingintheevent); and passing that on to the widget?

Perhaps, Luis, Andrew or Brad is reading this and can suggest something?


George Murphy

There is no reason, I might suggest it as a better way, is to pass the information into the widget.

For example

#cb.widget(name=“myWidget”, args = {CustomeFiled = cb.getCustomField(‘nameoffieldIcreated’,0);})#

I appreciate all the responses but i think the end goal is still being missed. The widget is very much like the most recent entries widget, which displays a certain number of post entries based on a specific category. The difference between the recent entries widget and mine is how it displays its content. The recent entries widget only displays the title of the most recent entries and can be restricted by category.

My widget also loops over a list of entries restricted to a specific category but it displays the Title, which in this case is the name of a person, the content, which in this case is a photo of a person, the excerpt, which in this case is a persons Short bio, and the custom fields that exist for each entry in the result set

The widget is called from a Custom Page not from a Blog Entry page. Since the getCustomField function is, within itself, determining if its being called from a Page or an Entry, its only rendering the custom fields for the page the widget lives in, not the individual entries being rendered. This is the main issue i have to overcome, how to render the custom fields for each individual Entry within the loop when the widget is called from a page.

Again, thanks for the responses!


I must admit I don’t understand your problem, one second your saying you can’t run the var id = cb.getCustomField(‘nameoffieldIcreated’,0); Because if your widget is not extending the baseWidget, then the DI objects are not going to be available. Which means you have to DI what you need.

Anything else that I might be missing.

When you’re looping over the pages/entries, each item in the loop is an entity, right? If so, can’t you just call the getter for the customfields property on the entity itself?

This would conceivably give you an array of custom field entities, which you could loop over, pass to a custom helper method on your widget, etc.

my custom widget is extending the baseWidget. Again the widget is being rendered in a Page not an Entry. The widget is displaying Entries within the Page. When i use getCustomField it returns the custom fields associated with the Page, not the individual entries retrieved and displayed by my widget. The getCustomField function does not find the custom fields associated with each entry displayed by the widget.

Thats what I thought, but since the parent display template is a page, not the index page of the blog, it returns the custom fields for the page, not the individual entries!


I still don’t see your problem, but I think if I am reading you right, what you should be doing is to write your own getEntryByCustomField(). Now I am assuming that you know the custom field that you are looking for, and as the CustomField is tied to the content, whether it be page or blog, you should be able to filter this in your HQL query.

This would return a list of Entries that match that custom field, then you can just loop over the entries and display what it is your looking for. This is on the assumption that your looking for a specific custom field, if that is not the case then you could just get all custom fields based on the type of entry and then loop over and display what your looking for.

Is that what you’re looking for?

I’m talking about using getCustomFiields() on the entity instance itself, not using the custom method in CBHelper. For example:

for( entity in entries) {
cf = entity.getCustomFields()

That is, instead if using the helper (which is making assumptions about the context), just use the entity objects getter for the custom fields of the entity currently in the loop

I’m probably causing alot of the confusion here because of my limited knowledge of content box. I’ve attached a screenshot of what i’ve done so far in the hopes that it will clear up the confusion.

The screenshot is of a Page created through the admin. The page itself has a custom field for a sub title directly under the page title. From the page editor, i inserted the custom widget i created which loops over blog entries attached to a specific category and displays them three across. When I create each blog post for the widget, I’m using the title field for the name of the person, the content field for their photo, and the excerpt field for the persons title. The persons title should be a custom field, so I can use the excerpt for a short bio.

Each blog entry will have additional custom fields that I need to display. But since the context is Page, it only renders the custom field created for the Page, not for the individual Blog Entires.

What i’m basically trying to do is mimic the custom post types feature of word press. Its a workaround since the custom fields have to be defined for every post that you want to display with the widget, but until Luis extends the posting framework with this capability, it will do as there will be tons of things that can be done this way.

Did you try the last two suggestions (eg using the getters from the entity instances themselves or creating a custom method of your own)?

I’m clear on what you’re trying to accomplish–I think now its just a matter of figuring out which of the suggestions that have already been made are going to be the best fit, why they aren’t working, etc.

I’ve spent the morning working on this and finally got it to work by using the following code within my widget:

var fields = “”

fields = entryResults.entries[x].getCustomFields();

var results = {};

for(var thisField in fields){

results[ thisField.getKey() ] = thisField.getValue();


Then getting each custom field in the results structure. Its basically the code used within the getCurrentCustomFields function in cbHelper excluding the if condition on the context (isPage()). Definitely not an ideal solution but it lets me get past this for now.

Don’t really understand why the isPage() is actually needed in the functions which deal with custom fields. The custom fields are tied to the content table via the content id which contains both page and entry content. I’m sure there is a reason for it but it seems unnecessary.

I really do hope they implement some kind of custom post type feature. Right now I have to define the same set of custom fields for each post as each post is created. In one post type, where i list a performance schedule, its 8 distinct fields. Pain in the butt when you have over 30 posts to create.

With a custom post type feature we could predefine any necessary custom fields and have them available for that post type which will greatly enhance the flexibility of contentbox and definitely put it on par with wordpress. The developer community would no doubt begin creating custom post type plugins for anyone to use.

Looking forward to that!

Glad to hear the suggestions worked.

Re: the isPageView() condition, in the context of the helper it’s necessary because pages and entries are handled differently within the app, and are passed in the prc as entries or pages (not just generic content). And so depending on the context of the request it needs to be smart about whether it retrieves a page or a entry from prc.

I’m curious now, and need to take a look at how WordPreas handles this. Seems like something that could be added in without a tremendous amount of heartburn.