Ejemplo n.º 1
0
        protected String GetSingleStringValue(string fieldName, ItemFields fields)
        {
            if (fields.Contains(fieldName))
            {
                // check if the field is a KeywordField or assume it is a TextField
                if (fields[fieldName].GetType().Equals(typeof(KeywordField)))
                {
                    KeywordField field = fields[fieldName] as KeywordField;
                    if (field != null)
                    {
                        return(field.Value.Title);
                    }
                }
                else
                {
                    TextField field = fields[fieldName] as TextField;
                    if (field != null)
                    {
                        return(field.Value);
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 2
0
 protected IList <Keyword> GetKeywordValues(string fieldName, ItemFields fields)
 {
     if (fields.Contains(fieldName))
     {
         KeywordField field = (KeywordField)fields[fieldName];
         return((field.Values.Count > 0) ? field.Values : null);
     }
     else
     {
         return(null);
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Retrieves multiple <see cref="T:Tridion.ContentManager.ContentManagement.Keyword" /> field values.
        /// </summary>
        /// <param name="itemField"><see cref="T:Tridion.ContentManager.ContentManagement.Fields.ItemField" /></param>
        /// <returns><see cref="T:Tridion.ContentManager.ContentManagement.Keyword" /> values</returns>
        public static IList <Keyword> KeywordValues(this ItemField itemField)
        {
            if (itemField != null)
            {
                KeywordField field = itemField as KeywordField;

                if (field != null && field.Values.Count > 0)
                {
                    return(field.Values);
                }
            }

            return(new List <Keyword>());
        }
        public override Field BuildField(ItemField tcmItemField, int currentLinkLevel)
        {
            Field dd4tField = base.BuildField(tcmItemField, currentLinkLevel);

            KeywordField tcmKeywordField = tcmItemField as KeywordField;

            if (tcmKeywordField != null)
            {
                int i = 0;
                foreach (Keyword dd4tKeyword in dd4tField.KeywordValues)
                {
                    TcmKeyword tcmKeyword = tcmKeywordField.Values[i++];
                    if (tcmKeyword.MetadataSchema != null)
                    {
                        dd4tKeyword.AddExtensionProperty(DxaExtensionDataSectionName, "MetadataSchemaId", tcmKeyword.MetadataSchema.Id);
                    }
                }
            }

            return(dd4tField);
        }
Ejemplo n.º 5
0
        public void Transform(Engine engine, Package package)
        {
            Component    c       = (Component)engine.GetObject(package.GetByName(Package.ComponentName));
            ItemFields   content = new ItemFields(c.Content, c.Schema);
            KeywordField topic   = (KeywordField)content["ContentCategory"];
            string       topics  = string.Empty;
            int          count   = 0;

            if (topic.Values.Count > 0)
            {
                topics += "(";
                foreach (Keyword key in topic.Values)
                {
                    if (count > 0)
                    {
                        topics += ", ";
                    }
                    topics += key.Title;
                    count++;
                }
                topics += ")";
            }
            package.PushItem("Topics", package.CreateStringItem(ContentType.Text, topics));
        }
Ejemplo n.º 6
0
 private void OutputKeywordValues(KeywordField field, StringBuilder sb)
 {
     if (field.Values.Count() == 0)
     {
         sb.Append("<empty/>\n");
         return;
     }
     foreach (Keyword keyword in field.Values)
     {
         sb.Append("<keyword taxonomyId=");
         sb.Append(GetQuotedString(keyword.Id));
         sb.Append(" path=");
         sb.Append(GetQuotedString(keyword.Path));
         sb.Append(" key=");
         sb.Append(GetQuotedString(keyword.Key));
         sb.Append("/>\n");
     }
 }
        public static Dictionary <string, Region> GetRegions(this Page page)
        {
            PageTemplate pageTemplate = page.PageTemplate;
            string       cacheKey     = pageTemplate.Id + "regions";

            if (page.Session.Cache.Get("", cacheKey) != null)
            {
                return((Dictionary <string, Region>)page.Session.Cache.Get("", cacheKey));
            }

            Dictionary <string, Region> regions = new Dictionary <string, Region>();

            Dictionary <string, Keyword> themeKeywords = new Dictionary <string, Keyword>();

            if (pageTemplate.Metadata != null)
            {
                ItemFields pageTemplateMeta = new ItemFields(pageTemplate.Metadata, pageTemplate.MetadataSchema);
                if (pageTemplateMeta.Contains("theme"))
                {
                    KeywordField        k     = (KeywordField)pageTemplateMeta["theme"];
                    Keyword             theme = k.Value;
                    ChildKeywordsFilter f     = new ChildKeywordsFilter(page.Session);

                    foreach (Keyword keyword in theme.GetChildKeywords(f))
                    {
                        string title               = keyword.Title;
                        string themepattern        = theme.Title + " - ";
                        string overridenregionname = title.Replace(themepattern, "");
                        themeKeywords.Add(overridenregionname, keyword);
                    }
                }
            }

            foreach (ComponentPresentation cp in page.ComponentPresentations)
            {
                ComponentTemplate ct = cp.ComponentTemplate;
                if (ct.Metadata != null)
                {
                    ItemFields metadata = new ItemFields(ct.Metadata, ct.MetadataSchema);
                    if (metadata.Contains("region"))
                    {
                        KeywordField keywordField = (KeywordField)metadata["region"];
                        if (keywordField.Values.Count > 0)
                        {
                            Keyword regionKeyword = keywordField.Value;
                            string  regionName    = regionKeyword.Title;

                            if (regions.ContainsKey(regionName))
                            {
                                regions[regionName].ComponentPresentations.Add(cp);
                            }
                            else
                            {
                                Region region = new Region
                                {
                                    Name = regionName,
                                    ComponentPresentations = new List <ComponentPresentation> {
                                        cp
                                    }
                                };
                                if (themeKeywords.ContainsKey(regionName))
                                {
                                    region.RegionDefinitionsKeyword = themeKeywords[regionName];
                                }
                                else
                                {
                                    region.RegionDefinitionsKeyword = regionKeyword;
                                }

                                regions.Add(regionName, region);
                            }
                        }
                    }
                }
            }
            page.Session.Cache.Add("", cacheKey, regions);
            return(regions);
        }
 /// <summary>
 /// Adds the metadata attributes from the page or structure group.
 /// </summary>
 /// <param name="fields">ItemFields collection containing the metadata fields & values.</param>
 /// <param name="writer">The XmlWriter.</param>
 /// <param name="prefix">The prefix to use - leave blank, it will be used when fields are embedded.</param>
 /// <remarks>This method will write values in the navigation XML output from the Page or StructureGroup metadata. Note that it will only include these values if Navigation.IncludePageMetadata or Navigation.IncludeStructureGroupMetadata are set to true. </remarks>
 private void AddMetadataAttributes(IEnumerable <ItemField> fields, XmlWriter writer, string prefix = null)
 {
     foreach (ItemField itemField in fields)
     {
         if (itemField is TextField || itemField is KeywordField)
         {
             bool   includeField  = false;
             string attributeName = null;
             if (Navigation.FieldsToInclude.Count == 0) //  Include all fields
             {
                 includeField  = true;
                 attributeName = itemField.Name.ToLower();
             }
             else if (Navigation.FieldsToInclude.ContainsKey(itemField.Name))
             {
                 includeField  = true;
                 attributeName = Navigation.FieldsToInclude[itemField.Name];
             }
             else
             {
                 _log.Debug("Not including field " + itemField.Name + " because it is not in the list of Navigation.FieldsToInclude");
             }
             if (includeField)
             {
                 if (prefix != null)
                 {
                     attributeName = prefix + attributeName;
                 }
                 if (itemField is TextField)
                 {
                     TextField field = (TextField)itemField;
                     if (field.Values.Count > 0)
                     {
                         writer.WriteAttributeString(attributeName, field.Value);
                     }
                 }
                 if (itemField is KeywordField)
                 {
                     KeywordField field = (KeywordField)itemField;
                     if (field.Values.Count > 0)
                     {
                         writer.WriteAttributeString(attributeName, field.Value.Title);
                     }
                 }
                 _countFields++;
             }
         }
         else if (itemField is EmbeddedSchemaField)
         {
             EmbeddedSchemaField embedded = (EmbeddedSchemaField)itemField;
             int count = 0;
             foreach (ItemFields embeddedFields in embedded.Values)
             {
                 AddMetadataAttributes(embeddedFields, writer, embedded.Name + count);
                 count++;
             }
         }
         else
         {
             _log.Debug("Not adding metadata for field " + itemField.Name + " since it is not a Text Field.");
         }
     }
 }
        /// <summary>
        /// Populates the dictionary with values from the item fields.
        /// </summary>
        /// <param name="itemFields">The Tridion itemfields to populate the dictionary with.</param>
        private void PopulateDynamicItemFields(ItemFields itemFields)
        {
            if (itemFields == null)
            {
                return;
            }

            foreach (ItemField itemField in itemFields)
            {
                string key = itemField.Name.ToLower();

                if (itemField is XhtmlField)
                {
                    XhtmlField xhtmlField = (XhtmlField)itemField;
                    if (xhtmlField.Definition.MaxOccurs == 1)
                    {
                        _dictionary[key] = TemplateUtilities.ResolveRichTextFieldXhtml(xhtmlField.Value);
                    }
                    else
                    {
                        List <string> values = new List <string>();
                        foreach (string value in xhtmlField.Values)
                        {
                            values.Add(TemplateUtilities.ResolveRichTextFieldXhtml(value));
                        }
                        _dictionary[key] = values;
                    }
                }
                else if (itemField is TextField)
                {
                    TextField textField = (TextField)itemField;
                    if (textField.Definition.MaxOccurs == 1)
                    {
                        _dictionary[key] = textField.Value;
                    }
                    else
                    {
                        _dictionary[key] = textField.Values;
                    }
                }
                else if (itemField is DateField)
                {
                    DateField dateField = (DateField)itemField;
                    if (dateField.Definition.MaxOccurs == 1)
                    {
                        _dictionary[key] = dateField.Value;
                    }
                    else
                    {
                        _dictionary[key] = dateField.Values;
                    }
                }
                else if (itemField is KeywordField)
                {
                    KeywordField keywordField = (KeywordField)itemField;
                    if (keywordField.Definition.MaxOccurs == 1)
                    {
                        if (keywordField.Value == null)
                        {
                            _dictionary[key] = null;
                        }
                        else
                        {
                            _dictionary[key] = new KeywordModel(_engine, keywordField.Value);
                        }
                    }
                    else
                    {
                        List <KeywordModel> keywords = new List <KeywordModel>();
                        int i = 0;
                        foreach (Keyword k in keywordField.Values)
                        {
                            var kw = new KeywordModel(_engine, k);
                            kw.Index  = i++;
                            kw.IsLast = Index == keywordField.Values.Count - 1;
                            keywords.Add(kw);
                        }
                        _dictionary[key] = keywords;
                    }
                }
                else if (itemField is EmbeddedSchemaField)
                {
                    EmbeddedSchemaField embeddedSchemaField = (EmbeddedSchemaField)itemField;
                    if (embeddedSchemaField.Definition.MaxOccurs == 1)
                    {
                        if (embeddedSchemaField.Values.Count == 0)
                        {
                            _dictionary[key] = null;
                        }
                        else
                        {
                            _dictionary[key] = new DynamicItemFields(_engine, embeddedSchemaField.Value);
                        }
                    }
                    else
                    {
                        List <dynamic> embeddedFields = new List <dynamic>();

                        int i = 0;
                        foreach (ItemFields fields in embeddedSchemaField.Values)
                        {
                            var dif = new DynamicItemFields(_engine, fields);
                            dif.Index  = i++;
                            dif.IsLast = dif.Index == embeddedSchemaField.Values.Count - 1;
                            embeddedFields.Add(dif);
                        }
                        _dictionary[key] = embeddedFields;
                    }
                }
                else if (itemField is ComponentLinkField)
                {
                    ComponentLinkField componentLinkField = (ComponentLinkField)itemField;
                    if (componentLinkField.Definition.MaxOccurs == 1)
                    {
                        if (componentLinkField.Value == null)
                        {
                            _dictionary[key] = null;
                        }
                        else
                        {
                            _dictionary[key] = new ComponentModel(_engine, componentLinkField.Value);
                        }
                    }
                    else
                    {
                        List <ComponentModel> components = new List <ComponentModel>();
                        int i = 0;
                        foreach (Component c in componentLinkField.Values)
                        {
                            var cm = new ComponentModel(_engine, c);
                            cm.Index  = i++;
                            cm.IsLast = cm.Index == componentLinkField.Values.Count - 1;
                            components.Add(cm);
                        }
                        _dictionary[key] = components;
                    }
                }
                else if (itemField is ExternalLinkField)
                {
                    ExternalLinkField externalLink = (ExternalLinkField)itemField;
                    if (externalLink.Definition.MaxOccurs == 1)
                    {
                        _dictionary[key] = externalLink.Value;
                    }
                    else
                    {
                        _dictionary[key] = externalLink.Values;
                    }
                }
                else if (itemField is NumberField)
                {
                    NumberField numberField = (NumberField)itemField;
                    if (itemField.Definition.MaxOccurs == 1)
                    {
                        _dictionary[key] = numberField.Value;
                    }
                    else
                    {
                        _dictionary[key] = numberField.Values;
                    }
                }
                else
                {
                    _dictionary[key] = itemField.ToString();
                }
            }
        }
        public void Transform(Engine engine, Package package)
        {
            // Find the best fit for SEO Keywords and Description for this page's content.
            // Leave if we're not rendering a page
            if (package.GetByName(Package.PageName) == null)
            {
                return;
            }

            List <string> seoKeywords    = new List <string>();
            string        seoDescription = string.Empty;

            Page page = (Page)engine.GetObject(package.GetByName(Package.PageName));

            if (page.Metadata != null)
            {
                ItemFields metadata = new ItemFields(page.Metadata, page.MetadataSchema);
                foreach (ItemField field in metadata)
                {
                    if (field.Name.Equals("ContentSource"))
                    {
                        seoKeywords.Add(((KeywordField)field).Value.Title);
                    }
                    if (field.Name.Equals("Keywords"))
                    {
                        TextField mvKeywords = (TextField)field;
                        foreach (string keyword in mvKeywords.Values)
                        {
                            seoKeywords.Add(keyword);
                        }
                    }
                    if (field.Name.Equals("Description"))
                    {
                        TextField description = (TextField)field;
                        seoDescription = description.Value;
                    }
                }
            }
            // Check if we have data...
            if (string.IsNullOrEmpty(seoDescription))
            {
                // Get description from first component.
                // If it exists...
                // If page has no components, then it must be a dynamic index page, we should get the description to match the page title
                if (page.ComponentPresentations.Count.Equals(0))
                {
                    seoDescription = package.GetValue("ArticlesByText");
                    // and the first seoKeyword should be the Content Source field.
                    if (seoKeywords.Count >= 1)
                    {
                        seoDescription += " " + seoKeywords[0];
                    }
                }
                else
                {
                    // Let's look at the content
                    // First find the type of page.
                    if (page.PageTemplate.Title.Contains("Content") || page.PageTemplate.Title.Contains("Home"))
                    {
                        // This is either a one page article, or the home page. In both cases, the SEO Description is the ArticleTitle of the first component
                        CP        cp = page.ComponentPresentations[0];
                        Component c  = cp.Component;
                        if (c.Schema.Title.Equals("Article"))
                        {
                            ItemFields content = new ItemFields(c.Content, c.Schema);
                            TextField  title   = (TextField)content["ArticleTitle"];
                            seoDescription = title.Value;
                            // If we have no keywords and the it's a content page, let's add the author to the keywords
                            if (seoKeywords.Count == 0 && page.PageTemplate.Title.Contains("Content"))
                            {
                                KeywordField source = (KeywordField)content["Source"];
                                if (source.Values.Count > 0)
                                {
                                    seoKeywords.Add(source.Value.Title);
                                }
                            }
                        }
                    }
                }
            }
            if (!(string.IsNullOrEmpty(seoDescription)))
            {
                package.PushItem(SeoDescriptionName, package.CreateStringItem(ContentType.Text, seoDescription));
            }
            if (seoKeywords.Count <= 0)
            {
                return;
            }
            string keywords = string.Empty;

            for (int i = 0; i < seoKeywords.Count; i++)
            {
                if (i > 0)
                {
                    keywords += ", ";
                }
                keywords += seoKeywords[i];
            }
            package.PushItem(SeoKeywordsName, package.CreateStringItem(ContentType.Text, keywords));
        }