Skip to content

Instantly share code, notes, and snippets.

@Dillie-O
Created April 15, 2015 13:08
Show Gist options
  • Save Dillie-O/dc286ad2c1353b3531cd to your computer and use it in GitHub Desktop.
Save Dillie-O/dc286ad2c1353b3531cd to your computer and use it in GitHub Desktop.
SiteFinity Retrieve FeeValue Content Block from Shortcode
public static class FeeValueShortcodeProcessor
{
/// <summary>
/// Expands all FeeValue shortcodes in the specified text
/// </summary>
/// <param name="inputText">The input text.</param>
/// <returns>String with replaced short code values</returns>
public static string Expand(object inputText)
{
if (inputText == null) return string.Empty;
var result = inputText.ToString();
// use regex to find matches
const string regex = @"\[FeeValue:(?<title>.*?)\]";
var matches = Regex.Matches(result, regex);
foreach (Match match in matches)
{
// Get Fee Title
var feeTitle = match.Groups["title"].Value;
// Replace shortcode text with retrieved Fee value from shared
// content blocks. Filter by the FeeValues category so that we don't
// get things mixed up with any actual content blocks.
var taxManager = TaxonomyManager.GetManager();
var catManager = taxManager
.GetTaxonomies<HierarchicalTaxonomy>()
.SingleOrDefault(s => s.Name == "Categories");
var feeValuesCategory = catManager.Taxa.FirstOrDefault(c => c.Title == "FeeValues");
var categoryId = feeValuesCategory.Id;
var manager = ContentManager.GetManager();
var contentItem = manager
.GetContent()
.FirstOrDefault(x => x.GetValue<TrackedList<Guid>>("Category").Contains(categoryId)
&& x.Title == feeTitle
&& x.Status == ContentLifecycleStatus.Live);
var feeValue = contentItem != null ? contentItem.Content.ToString() : "FEE VALUE NOT FOUND";
result = result.Replace(match.Value, feeValue);
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment