public Tag GetTag(string tagName) { var settings = _tagsSettingService.GetCustomTaggerSettingModel(); var existingTags = _tagsEntryService.AllTags(settings); var item = _tagsEntryService.GetTag(tagName, settings, existingTags); return(item == null ? null : new Tag { TagName = item.Name, ID = item.ID.ToString(), TaxonomyProviderId = ProviderId }); }
public void TagContent(Item contentItem, IEnumerable <Tag> tags) { var tagsFieldId = _settingsService.GetCustomTaggerSettingModel(contentItem).TagsFieldTarget; if (tagsFieldId == Guid.Empty) { Log.Warn("CustomTagger: Tags field name not defined in settings", this); return; } var tagsField = contentItem.Fields[new ID(tagsFieldId)]; if (tagsField == null || string.IsNullOrEmpty(tagsField.Type)) { Log.Warn($"CustomTagger: Field {tagsFieldId} not found or wrong type in item {contentItem.ID}", this); return; } var tagsEditField = (MultilistField)tagsField; contentItem.Editing.BeginEdit(); foreach (var tag in tags) { if (ID.TryParse(tag.ID, out ID id) && !tagsEditField.TargetIDs.Contains(id)) { tagsEditField.Add(tag.ID); } } contentItem.Editing.EndEdit(); }
public TaggableContent GetContent(Item source) { var stringContent = new StringContent(); var stringBuilder = new StringBuilder(); var settings = _tagsSettingService.GetCustomTaggerSettingModel(source); foreach (Field field in source.Fields) { if (!field.Name.StartsWith("__", StringComparison.InvariantCulture) && field.ID != settings.TagsFieldTargetId) { stringBuilder.Append(field.Value); if (stringBuilder.Length > 0) { stringBuilder.Append(" "); } } } stringContent.Content = stringBuilder.ToString(); return(stringContent); }
/// <summary> /// Store tags in the content item /// </summary> /// <param name="contentItem"></param> /// <param name="tags"></param> public void TagContent(Item contentItem, IEnumerable <Tag> tags) { var tagsFieldId = _settingsService.GetCustomTaggerSettingModel(contentItem).TagsFieldTargetId; var tagsField = contentItem.Fields[tagsFieldId]; if (tagsField == null || string.IsNullOrEmpty(tagsField.Type)) { throw new CustomTaggerSettingsException($"CustomTagger: Field {tagsFieldId} not found or wrong type in item {contentItem.ID}"); } var tagsEditField = (MultilistField)tagsField; contentItem.Editing.BeginEdit(); foreach (var tag in tags) { if (ID.TryParse(tag.ID, out ID id) && !tagsEditField.TargetIDs.Contains(id)) { tagsEditField.Add(tag.ID); } } contentItem.Editing.EndEdit(); }
public IEnumerable <Tag> CreateTags(Item contentItem, IEnumerable <TagData> tagData) { var tagsSettingsModel = _tagsSettingService.GetCustomTaggerSettingModel(contentItem); var categoryTemplate = new TemplateItem(Database.GetItem(new ID(tagsSettingsModel.TagCategoryTemplate))); if (categoryTemplate == null) { Sitecore.Diagnostics.Log.Warn($"CustomizableTaxonomyProvider: category template item with ID {tagsSettingsModel.TagCategoryTemplate} for tags not found", this); return(new List <Tag>()); } var template = new TemplateItem(Database.GetItem(new ID(tagsSettingsModel.TagEntryTemplate))); if (template == null) { Sitecore.Diagnostics.Log.Warn($"CustomizableTaxonomyProvider: template item with ID {tagsSettingsModel.TagEntryTemplate} for tags not found", this); return(new List <Tag>()); } var tagsRepositoryRootItem = Database.GetItem(new ID(tagsSettingsModel.TagsCollectionRootItem)); if (tagsRepositoryRootItem == null) { Sitecore.Diagnostics.Log.Warn($"CustomizableTaxonomyProvider: tags root item with ID {tagsSettingsModel.TagsCollectionRootItem} not found", this); return(new List <Tag>()); } var tagFieldEntry = new ID(tagsSettingsModel.TagEntryValueField); if (tagFieldEntry == ID.Null || tagFieldEntry == ID.Undefined) { Sitecore.Diagnostics.Log.Warn($"CustomizableTaxonomyProvider: tag field with ID {tagsSettingsModel.TagsCollectionRootItem} not found", this); return(new List <Tag>()); } //var tagsWithCategories = new List<TagData>(); var tagsWithoutCategories = new List <TagData>(); var tagsCategories = new Dictionary <string, List <TagData> >(); foreach (var tag in tagData) { if (!tag.Properties.Any(p => { if (p.Key != CustomizableTaxonomyProvider.PropertyKey || p.Value == null) { return(false); } var jObject = p.Value as JObject; if (jObject == null) { return(false); } if (!jObject.ContainsKey(CustomizableTaxonomyProvider.PropertyValueKey)) { return(false); } var categoryName = jObject.GetValue(CustomizableTaxonomyProvider.PropertyValueKey); //var valueStart = jObject.IndexOf('"', startIndex + CustomizableTaxonomyProvider.PropertyValueKey.Length + 1) + 1; //if (valueStart < 0) //{ // return false; //} //jObject = jObject.Substring(valueStart + 1); //var valueEnd = jObject.IndexOf('"'); //if (valueEnd < 0) //{ // return false; //} //jObject = jObject.Substring(0, valueEnd); //if (string.IsNullOrWhiteSpace(jObject)) //{ // return false; //} if (!tagsCategories.ContainsKey(categoryName.ToString())) { tagsCategories.Add(categoryName.ToString(), new List <TagData>()); } tagsCategories[categoryName.ToString()].Add(tag); return(true); } )) { tagsWithoutCategories.Add(tag); } } if (template.Fields.Select(x => x.ID).Contains(tagFieldEntry)) { List <Tag> tagList = new List <Tag>(); foreach (var tagCategory in tagsCategories) { var categoryItem = PrepareNewCategory(tagCategory.Key, tagsRepositoryRootItem, categoryTemplate); if (categoryItem != null) { foreach (var data in tagCategory.Value) { PrepareNewTag(template, tagFieldEntry, tagList, data, categoryItem); } } else { tagsWithoutCategories.AddRange(tagCategory.Value); } } foreach (var tagWithoutCategory in tagsWithoutCategories) { PrepareNewTag(template, tagFieldEntry, tagList, tagWithoutCategory, tagsRepositoryRootItem); } return(tagList); } else { Sitecore.Diagnostics.Log.Warn($"CustomTagger: template {tagsSettingsModel.TagEntryTemplate} doesn't contain field with ID {tagsSettingsModel.TagsCollectionRootItem}", this); return(new List <Tag>()); } }