4

I need to have a certain field (field type Link Field) to have anchoring feature for that I need the id to be generated I was thinking if we can use VariantField's Link attributes property to generate that as below.

I tried using $item.Name or $name in Link attributes in a VariantField

VariantField - Link attributes

Also, if anyone can guide on generating href with hash id #id for the same?

Please help.

// using SXA 1.7.1

7

TL;DR

Yes it is possible, use this: @(Item.ID)

I assume that you are using SXA renderings or if it's custom rendering its model inherits from RenderingModelBase

If you need an explanation, please read the rest of my answer.

Dynamic attributes

I will describe all the possibilities that I know and will paste a screenshot with example configuration and output at the end.

Field - basic

If you use the following notation: $(FIELD_NAME), a raw value of a field FIELD_NAME will be rendered (if it's not handled in a special way - see next point).

This case will be applied to Single-Line Text, Multi-Line ...

Field - custom "field renderers"

If you use the following notation: $(FIELD_NAME) and the field type is handled in a special way in the code you can expect something different than raw value.

For example, if you specify an image, a link to this image will be generated instead of the direct value from the field.

You can find a list of fields handled in a special way in class: RenderRenderingVariantFieldProcessor.cs

I will write what it's there in SXA 1.7.1

  • ImageField
  • FileField
  • ReferenceField
  • LinkField
  • TextField
  • MultilistField
  • LookupField

Model - property extractor

If you use this notation: @(PROPERTY_PATH) you can access properties from your model (code model) used to render rendering.

Example: Assuming that you are using the default model: RenderingModelBase you can write something like this:

@(IsEdit) - this will render bool value of the IsEdit property

enter image description here

I called it property extractor because you can go deeper. If you access for example Item property, then you can display its ID

Example

Examples are the best to understand something, so here is an example configuration and output.

enter image description here

6
  • I have tried $(Item.Title) on Link attributes and it resolves the field value from RenderRenderingVariantFieldProcessor.GetAttributeTokenValue(string fieldName, Item item) but I need Item's Name or ID but this method resolves the field value for an item, how should be the solution to resolve $(Item.ID)? – Harsh Baid Feb 1 '19 at 21:58
  • Also as per regex for AttributeFieldToken in RenderRenderingVariantFieldProcessor class I found @(Item.Title) will not work – Harsh Baid Feb 1 '19 at 22:00
  • @HarshBaid I was testing it on 1.8 but I compared the code and looked like it should work on 1.7.1 as well. The answer to your question is in my answer. – Alan Płócieniak Feb 4 '19 at 8:18
  • I'm using SXA's Navigation component this doesn't work on that. Another strange thing is NavigationController doesn't inherit from VariantsController but repository and model for this are inheriting from variants based base classes or interfaces. – Harsh Baid Feb 6 '19 at 16:23
  • I also cannot access @(Rendering.Parameters["Styles"]) from a VariantField (SXA 1.8) – Ben Sewards Feb 6 '19 at 17:27
2

I'm on SC 9.0.2 with SXA 1.7.1

Some observations,

  1. When tested this on promo component - @(Item.ID) works on DataAttributes but $(Item.ID) does not.
  2. Both @(Item.ID) and $(Item.ID) do not work LinkAttributes and specifically for Navigation component

So I had to override RenderVariantField class as below,

using Sitecore.Data.Items;

namespace ProjectName.Foundation.Navigation.Pipelines.RenderVariantField
{
    public class RenderVariantField : Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderVariantField
    {
        protected override string GetAttributeTokenValue(string fieldName, Item item)
        {
            if (fieldName == "Item.ID")
            {
                return item.ID.ToString();
            }
            else if (fieldName == "Item.Name")
            {
                return item.Name;
            }

            return base.GetAttributeTokenValue(fieldName, item);
        }
    }
}

In code above, I could have written more dynamic code to resolve all properties of Item but that was not required by me so kept it simple to just check for Item.ID and Item.Name.

Patch config as below:

<?xml version="1.0"?>

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <pipelines>
            <renderVariantField>
                <processor patch:instead="*[@type='Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderVariantField, Sitecore.XA.Foundation.RenderingVariants']"
                           type="ProjectName.Foundation.Navigation.Pipelines.RenderVariantField.RenderVariantField, ProjectName.Foundation.Navigation" resolve="true"/>
            </renderVariantField>
        </pipelines>
    </sitecore>
</configuration>

Thanks @Alan Płócieniak your answer helped me but I had to write some more code to get this working.

1
  • i was trying out your solution because i have the same problem and it worked for variantFields however i need to apply the same solution for VariantSection but it didn't work out, any idea why? – AME Jul 1 '19 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.