Last week I talked about building a dictionary driven category system for articles using the fantastic Orion CMS. This week I’m going to continue in that same vein by building a fully customizable navbar!

So what’s the plan of attack? In Orion, we’ll define an entity called pages. This entity will hold the title and content for a variety of pages that will exist on our site. We want to be able to choose a selection of these pages to appear on our navbar in a specified order. We’ll keep that list of pages in an Orion dictionary.

Defining the Pages Entity

The first step is to define the pages entity that will be used by our application. This will be a straight-forward Orion entity:

orion.addEntity('pages', {
    title: {
        type: String,
        label: 'Title'
    },
    content: {
        type: String,
        label: 'Content',
        autoform: {
            afFieldInput: {
                type: 'froala',
                inlineMode: false,
            }
        }
    }
}, {
    icon: 'bookmark',
    sidebarName: 'Pages',
    pluralName: 'Pages',
    singularName: 'Page',
    tableColumns: [
        {
            data: 'title',
            title: 'Title'
        },
        orion.attributeColumn('froala', 'content', 'Preview')
    ]
});

Defining the Page Order

The next step is to define an Orion dictionary entry to hold the list of pages as they will appear in the navbar:

orion.admin.addAdminSubscription(orion.subs.subscribe('entity', 'pages'));

orion.dictionary.addDefinition('pages.$', 'config', {
    type: String,
    label: 'Page',
    optional: false,
    autoform: {
        type: 'select',
        options: function() {
            return orion.entities.pages.collection.find().fetch().map(function(page) {
                return {
                    value: page._id,
                    label: page.title
                };
            });
        }
    }
});

orion.dictionary.addDefinition('pages', 'config', {
    type: Array,
    minCount: 1,
    optional: false,
    label: 'Page Order'
});

There are a few moving parts here. Let’s break them down to get a better understanding of what’s going on.

Building Our Orion Interface

The first thing to notice is that our page order dictionary entry actually consists of two entries: pages and pages.$. Our goal is to have the page order list be a list of selections populated with pages entities. This means that pages must be an Array type. The children of pages (pages.$) are given the type String. When using Orion, it’s required that you add the definition for child dictionary entries (pages.$) before adding the parent (pages).

Populating Our Selects

In order to build the navbar, we need to be able to select which pages will appear from the set of all pages in the system. This means that we need to have the options of our pages.$ attribute driven by the pages entity collection. How do we do this?

Just like last time, the answer is to provide a custom options function. Our options function will fetch all of the documents in the pages entity collection and transform them into select options:

options: function() {
    return orion.entities.pages.collection.find().fetch().map(function(page) {
        return {
            value: page._id,
            label: page.title
        };
    });
}

If you were to visit your Orion dashboard at this point, you may notice that your page order selects aren’t populating with data (or maybe they are, if you visited the entities page first). This is because we haven’t subscribed to the pages entity collection yet. The final piece to this puzzle is to add a new subscription to the admin dashboard:

orion.admin.addAdminSubscription(orion.subs.subscribe('entity', 'pages'));

And that’s it! We now have a system where we can create pages in our CMS, and choose which of those pages will appear in our navbar. Check out the nav tag on my hello-orion Github project to see a working example.