2

I am using Sitecore in a Headless mode using the JSS. I have never tried to make any changes to the CM/functionality behind the CM, so please forgive my ignorance.

I am current researching for a way to use tokens of a parent page in a child item. Currently it sounds like it is not possible.

Example: I have a page called My Sample Page and there is a datasource in the Page Content called Page Data (My Sample Page -> Page Content -> Page Data).

Is there a way to use $name to set a field in Page Data to be "My Sample Page"? Is there a different way to resolve this?

1

2 Answers 2

1

You need to create a custom token to achieve it. Create a custom Token processor inheriting ExpandInitialFieldValueProcessor with the following code. It checks if the value of the field contains the $ancestortitle token. If it does it checks if the parent is not null. If the parent is not null and there are parent of parent item the value is set to the field.

using Sitecore.Pipelines.ExpandInitialFieldValue;
 
    public class TokenAncestorTitleProcessor : ExpandInitialFieldValueProcessor
    {
        public override void Process(ExpandInitialFieldValueArgs args)
        {
            if (args.SourceField.Value.Contains("$ancestortitle"))
            {
                if (args.TargetItem != null && args.TargetItem.Parent != null && args.TargetItem.Parent.Parent != null)
                {
                    //Replace any specific filed value like here used Title from parent item
                    args.Result = args.Result.Replace("$ancestortitle", args.TargetItem.Parent.Parent.Fields["Title"].Value);
                    
                    // If you want to replace Name of the parent then uncomment below line and comment above one.
                    //args.Result = args.Result.Replace("$ancestortitle", args.TargetItem.Parent.Parent.Name);
                }
            }
        }
    }

Config change

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
        <expandInitialFieldValue>
          <processor resolve="true" type="MyAssembly.MyNamespace.TokenAncestorTitleProcessor, MyAssembly" patch:after="processor[@type='type=Sitecore.Pipelines.ExpandInitialFieldValue.ReplaceVariables, Sitecore.Kernel']"/>
        </expandInitialFieldValue>  
      </pipelines>
  </sitecore>
</configuration>

Template configuration: enter image description here

Hope it helps!

0

https://doc.sitecore.com/xp/en/developers/90/sitecore-experience-manager/standard-values.html

These are default tokens available. If they doesn't serve the purpose, create custom token.

https://sitecorecorner.com/2014/08/03/sitecore-custom-tokens/comment-page-1/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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