/// <summary> /// inject token over the given token values found in rich text fields /// </summary> /// <returns>dynamic object with information about what happened</returns> public dynamic Incorporate(bool preview = false, string type = "plain") { if (type == "plain") { _tokenValue = Regex.Escape(_tokenValue); } dynamic ret = new ExpandoObject(); ret.Count = 0; ret.Converted = new List <ExpandoObject>(); using (new SecurityDisabler()) { if (!preview && !TokenCollection.HasToken(_tokenName) && TokenCollection is SitecoreTokenCollection <IToken> ) { Item newToken = _item.Add(_tokenName, new TemplateID(new ID("{87BFAA2C-2E2F-42C6-A135-9F2AE7D32807}"))); newToken.Editing.BeginEdit(); newToken["Token"] = _tokenName; newToken["Value"] = _tokenValue; newToken.Editing.EndEdit(); var collection = TokenKeeper.CurrentKeeper.GetTokenCollection <IToken>(_category) as TokenCollection <IToken>; if (collection != null) { collection.AddOrUpdateToken(collection.InitiateToken(_tokenName)); } } Stack <Item> itemStack = new Stack <Item>(); Item content = _database.GetItem(new ID(_root)); LoadAllLanguageItems(content, itemStack); { while (itemStack.Any()) { var cur = itemStack.Pop(); if (cur.IsTokenManagerItem() || !_tokenCollection.IsCurrentContextValid(cur)) { continue; } foreach (var field in cur.Fields.Where(f => f.Type == "Rich Text")) { Regex r = new Regex(_tokenValue); MatchCollection m = r.Matches(field.Value); if (m.Count > 0) { var ttt = ConvertTextToToken(field, m, preview); if (ttt != null) { ret.Count++; ret.Converted.Add(ttt); } } } if (LanguageManager.DefaultLanguage.Name == cur.Language.Name) { foreach (Item child in cur.Children.Where(c => !TemplateManager.GetTemplate(c).IsDerived(new ID(Constants.TokenTemplateBaseId)))) { LoadAllLanguageItems(child, itemStack); } } } } } return(ret); }