Skip to content

Instantly share code, notes, and snippets.

@Dillie-O
Created April 15, 2015 13:29
Show Gist options
  • Save Dillie-O/51ce10eca1928de3e414 to your computer and use it in GitHub Desktop.
Save Dillie-O/51ce10eca1928de3e414 to your computer and use it in GitHub Desktop.
SiteFinity Retrieve RateValue Content Block from Shortcode
public static class RateValueShortcodeProcessor
{
/// <summary>
/// Expands all RateValue 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 regex1 = @"\[RateValue:(?<title>.*?)\]";
var matches = Regex.Matches(result, regex1);
foreach (Match match in matches)
{
// Get Rate Title
var rateTitle = match.Groups["title"].Value;
// Replace shortcode text with retrieved Rate value from shared
// content blocks. Filter by the RateValues 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 rateValuesCategory = catManager.Taxa.FirstOrDefault(c => c.Title == "RateValues");
var singleCategoryId = rateValuesCategory.Id;
var manager = ContentManager.GetManager();
var contentItem = manager
.GetContent()
.FirstOrDefault(x => x.GetValue<TrackedList<Guid>>("Category").Contains(singleCategoryId)
&& x.Title == rateTitle
&& x.Status == ContentLifecycleStatus.Live);
var rateValue = contentItem != null ? contentItem.Content.ToString() : "RATE VALUE NOT FOUND";
result = result.Replace(match.Value, rateValue);
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment