Try our conversational search powered by Generative AI!

Custom property caption at runtime in EditMode

Vote:
 

Hi guys,

In edit mode, when all properties panel is displayed, each property show caption. Is there anyway to change properties caption at runtime? In example, page Apple is child page of page Fruit. In page Apple, there is property with caption "Lorem ipsum {0}", at runtime, I want to replace {0} by Fruit. The caption will look like "Lorem ipsum Fruit".

Here what I have done until now

[InitializableModule]
public class MetadataExtenderModule : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
        var registry = context.Locate.Advanced.GetInstance();
        registry.RegisterMetadataHandler(typeof(ContentData), new ApplePageMetadataExtender());
    }
}
public class ApplePageMetadataExtender : IMetadataExtender
{
	IContentRepository _contentRepository;
	public ApplePageMetadataExtender()
	{
		_contentRepository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance();
	}

	private FruitPage GetClosestFruitPage(PageReference pageRef)
	{
		PageData page = null;
		_contentRepository.TryGet(pageRef, out page);

		var closestFruitPage = page as FruitPage;
		if (closestFruitPage == null)
		{
			GetClosestFruitPage(page.ParentLink);
		}
		return closestFruitPage;
	}

	public void ModifyMetadata(ExtendedMetadata metadata, IEnumerable attributes)
	{
		var contentDataMetadata = (ContentDataMetadata)metadata;
		var applePage = contentDataMetadata.Model as ApplePage;
		if (applePage != null)
		{
			foreach (var propertyMetadata in metadata.Properties)
			{
				//Change display name only when property name is "loremIpsum"
				//Display name of "loremIpsum" property is "Lorem Ipsum {0}"
				//I want to display caption like "Lorem Ipsum Fruit"
				if (propertyMetadata.PropertyName == "loremIpsum"){
					FruitPage parentPage = GetClosestFruitPage(applePage.ParentLink);
					propertyMetadata.DisplayName = string.Format(propertyMetadata.DisplayName, parentPage.Name);
				}				
			}
		}
	}
}

But in edit mode, property "loremIpsum" still display caption "Lorem Ipsum {0}".

Could you please to help me to fix this? Where did I do wrong?

Thank in advance,

Linh

#198658
Nov 02, 2018 9:40
Vote:
 

At first glace the code all looks good. Your initialisation module may be initilaising too early though, so try adding the following depedency onto your init module:

[InitializableModule]
[ModuleDependency(typeof(EPiServer.Cms.Shell.InitializableModule))]
public class MetadataExtenderModule : IInitializableModule
{
 ...
#198667
Nov 02, 2018 11:14
Vote:
 

Thank David Knipe,

My current codes can replace caption of properties with type IList<string>, the other property with types, which are int, bool, string are not able to replace. I don't know why.

I added your code to project but it doesn't replace other properties

[ModuleDependency(typeof(EPiServer.Cms.Shell.InitializableModule))]
#198678
Nov 02, 2018 13:22
Vote:
 

Hi David Knipe,

You was right. My initialisation module is initilaised too early. After I changed value PropertyMetadata.DisplayName, at some point, my PropertyMetadata.DisplayName (changed value) was replaced by translation text in resource file. Therefore I went to different approach by creating EditorDescriptor like below code but it is not help. The EditorDescriptor can display closest Startpage name if there is no translation text of that property in resource file. If there is translation text, Display name will be replaced by translation text. I thought it was EditorDescriptor but it is not. Do you know where final DisplayName is set?

[EditorDescriptorRegistration(TargetType = typeof(String), UIHint = Global.SiteUIHints.String, EditorDescriptorBehavior = EditorDescriptorBehavior.PlaceLast)]
    public class InheritParentNameEditorDescriptor : EditorDescriptor
    {
        IContentRepository _contentRepository;
        public InheritParentNameEditorDescriptor()
        {
            _contentRepository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
        }

        public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
        {
            base.ModifyMetadata(metadata, attributes);

            var contentDataMetadata = (ContentDataMetadata)metadata;
            var standardPage = contentDataMetadata.Parent.Model as StandardPage;
            if (standardPage != null)
            {
                StartPage parentPage = GetClosestStartPage(standardPage.ParentLink);
                metadata.DisplayName = string.Format(metadata.DisplayName, parentPage.Name); //string.Format("Lorem Ipsum {0}", "Fruit") => "Lorem Ipsum Fruit"
            }
        }

        private StartPage GetClosestStartPage(PageReference pageRef)
        {
            PageData page = null;
            _contentRepository.TryGet(pageRef, out page);

            var closestStartPage = page as StartPage;
            if (closestStartPage == null)
            {
                GetClosestStartPage(page.ParentLink);
            }
            return closestStartPage;
        }
    }
public class StandardPage : SitePageData
    {
        [UIHint(Global.SiteUIHints.String)]
        public virtual string TestString { get; set; }
    }
#198930
Nov 09, 2018 5:59
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.