Content Classification & Taxonomies
Is there any documentation yet on classification that goes beyond Tags & Categories? I am looking to design my own hierarchical taxonomy and apply that to content. Will Generic Content and GC-based modules automatically be able to utilize the custom classifications built? For example, I would like to develop a classified ad module that uses a three-level hierarchical classification (i.e. Transportation -> Cars -> Sports Cars). The module will be GC-based. Pretty easy to do? I'm hoping to see some examples of more complex taxonomies, too. I know, you're all hard at work on this, and I can patiently wait if it's not ready yet. ;)
Thanks,
Jeff
Hi Jeff Vail,
You can create such taxonomy - Transportation -> Cars -> Sports Cars throught the UI.
Anyway there is a sample code that illustrates how to create new Taxonomy and add it the to UI.
public
static
void
TryCreateNewTaxonomy(
string
taxonomyTitle,
string
taxonomyName, TaxonomyType type,
params
Type[] contentTypes)
if
(
string
.IsNullOrEmpty(taxonomyTitle))
throw
new
ArgumentNullException(
"taxonomyTitle"
);
if
(
string
.IsNullOrEmpty(taxonomyName))
throw
new
ArgumentNullException(
"taxonomyName"
);
var taxonomyManager = TaxonomyManager.GetManager();
// first check if the taxonomy of specified type and name already exists.
ITaxonomy taxonomy =
null
;
var iconCss =
string
.Empty;
switch
(type)
case
TaxonomyType.Flat:
taxonomy = taxonomyManager.GetTaxonomies<FlatTaxonomy>().Where(t => t.Name == taxonomyName).SingleOrDefault();
iconCss =
"sfFlatTaxonIcn"
;
break
;
case
TaxonomyType.Hierarchical:
taxonomy = taxonomyManager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name == taxonomyName).SingleOrDefault();
iconCss =
"sfHierarchicalTaxonIcn"
;
break
;
case
TaxonomyType.Facet:
taxonomy = taxonomyManager.GetTaxonomies<FacetTaxonomy>().Where(t => t.Name == taxonomyName).SingleOrDefault();
break
;
case
TaxonomyType.Network:
taxonomy = taxonomyManager.GetTaxonomies<NetworkTaxonomy>().Where(t => t.Name == taxonomyName).SingleOrDefault();
break
;
// if the taxonomy of given name and specified type does not exist, we will create it here
if
(taxonomy ==
null
)
switch
(type)
case
TaxonomyType.Flat:
taxonomy = taxonomyManager.CreateTaxonomy<FlatTaxonomy>();
break
;
case
TaxonomyType.Hierarchical:
taxonomy = taxonomyManager.CreateTaxonomy<HierarchicalTaxonomy>();
break
;
case
TaxonomyType.Facet:
taxonomy = taxonomyManager.CreateTaxonomy<FacetTaxonomy>();
break
;
case
TaxonomyType.Network:
taxonomy = taxonomyManager.CreateTaxonomy<NetworkTaxonomy>();
break
;
taxonomy.Name = taxonomyName;
taxonomy.Title = taxonomyTitle;
taxonomyManager.SaveChanges();
// add dynamic fields to be used to store taxa from this new taxonomy
Manager.AddTaxonomyFieldToModel(taxonomy, taxonomyManager.Provider.Name, contentTypes);
// add the taxonomy control to the page editor toolbox
TaxonomyControlManager.AddTaxonomyControlToToolbox((Taxonomy)taxonomy, iconCss, taxonomy.Name);
// add user interface elements to the backend content forms
Manager.AddUserInterfaceElementToBackendForm(type, taxonomy, contentTypes);
private
static
void
AddTaxonomyFieldToModel(ITaxonomy taxonomy,
string
taxonomyProvider,
params
Type[] contentTypes)
MetaField field;
var metaMan = MetadataManager.GetManager();
foreach
(var contentType
in
contentTypes)
var type = metaMan.GetMetaType(contentType);
if
(type ==
null
)
type = metaMan.CreateMetaType(contentType);
var taxonomyName = taxonomy.Name;
var result = metaMan.GetMetafields().SingleOrDefault(f => f.FieldName == taxonomyName);
if
(result ==
null
)
field = metaMan.CreateMetafield(taxonomy.Name);
field.TaxonomyProvider = taxonomyProvider;
field.TaxonomyId = taxonomy.Id;
field.IsSingleTaxon =
false
;
type.Fields.Add(field);
metaMan.SaveChanges(
true
);