How to extend Admin > Settings with new fields?

Topics: Administration, Customizing Orchard
May 15, 2012 at 11:02 PM

Hello everyone,

Could somebody please explain the steps required to add two new fields to Orchard's "General Settings" page, store those values to the database, then access those values from within a view template? Basically what I'm trying to do is to be able to access a custom field (say  @WorkContext.CurrentSite.MyCustomField)  the same way I can access @WorkContext.CurrentSite.SiteName from within a view template.

I've seen how one can add a field to a Content Type, and have read references that an Orchard site is itself represented as a Content Type, but I don't see "Site" in the list of available Content Types that I can add fields or parts to.

Thanks in advance for any insight you can offer.

-Rich

Coordinator
May 16, 2012 at 6:21 AM

You have to do it from code. Plenty of examples in, for example, Vandelay.Industries.

May 16, 2012 at 11:30 PM

Thanks Bertrand. Using the Vandelay meta tag module as the basis for a new module, I was able to persist two new fields to a new table in the database. I then used the following line in the modules handler to have these new fields (City, StateCode) show up on the General Settings page:

Filters.Add(new ActivatingFilter<Models.AddressPart>("Site"));

Everything seems to work fine in that the information I enter into the new fields on the Admin > Settings page is saved to the database. However, when I try to access those fields from within a view template using @WorkContext.CurrentSite.City, I get the following error:

Compiler Error Message: CS1061: 'Orchard.Settings.ISite' does not contain a definition for 'City' and no extension method 'City' accepting a first argument of type 'Orchard.Settings.ISite' could be found (are you missing a using directive or an assembly reference?)

So I guess my updated question is how do I access the new fields that I added to the admin page from within a view template?

Thanks so much for everything. I've been playing with Orchard for a couple weeks now and am totally digging it.

-Rich

Coordinator
May 17, 2012 at 7:04 AM

The site settings are just a content item, so you can use As, like you would on a regular content item, to get to the other parts. Again, examples abound ;)

Apr 23, 2014 at 9:00 AM
Edited Apr 23, 2014 at 9:11 AM
Hi Bertrand,

Would you please give example here, it isn't very straight forward to discern this even after 5 hours of searching and hair-pulling:

Question is how does one retrieve value of a (text) Input Field added directly to Site content type?

Your answer in http://stackoverflow.com/questions/11907980/orchard-cms-custom-settings suggests that one should use:

workContext.CurrentSite.As<WhateverTheTypeOfThePartYouAddedIs>() but the WhateverTheTypeOfThePartYouAddedIs is created automatically (as per your answer in https://orchard.codeplex.com/discussions/396280) suggesting this should be "InputField" which of course does not seem to exist.

Shape tracing doesn't work in admin so that's of no help either.

To provide some context, I've added a simple Input Field (text) to the Site content type, named it "Short Name", and have overridden Header.cshtml from TheAdmin theme wishing to render the Short Name if available. This is where I'm stuck with how to access value of this field.

Thanks,
V

UPDATE: Found it, 10 minutes after posting this :-( Always the way...
For anyone else that comes here:
    dynamic ci = WorkContext.CurrentSite.ContentItem;
    string siteName = ci.Site.ShortName.Value ?? WorkContext.CurrentSite.SiteName;