4

Let's say I have several custom templates in my solution that are managed via a TDS project. There's also code generation set up that creates strongly-typed Glass models based on GlassV3Item.tt.

I want to be able to add custom [SitecoreField] attribute values for some of my template fields.

For example, one of the fields is generated like this:

[SitecoreField(SomeTemplateConstants.TagsFieldName)]
IEnumerable<ITag> Tags { get; set; }

I want to disable lazy loading for this field, so that it's generated like this instead:

[SitecoreField(SomeTemplateConstants.TagsFieldName, Setting = SitecoreFieldSettings.DontLoadLazily)]
IEnumerable<ITag> Tags { get; set; }

I'd like this custom field setting to be configurable via the TDS project.

How can I achieve this?

7

You can update the GlassV3Item.tt file and make use of the Custom Data property in TDS to set whether a field should not be lazy loaded.

Let's add some comments to keep this clean. Add this near the top of the file with the rest of the setting comments (Line 38 on clean tt file)

/*
dontloadlazy=true
    Sets the field setting to DontLoadLazily
*/

Add a new method to the end of the file which will check whether the field has the above property set on it in TDS (Line 240 on a clean file):

public static string GetGlassFieldLoadLazily(SitecoreField field)
{   
    string loadLazy = GetCustomProperty(field.Data, "dontloadlazy");
    bool isDontLoadLazy;
    bool.TryParse(loadLazy, out isDontLoadLazy);

    if (isDontLoadLazy)
        return ", Setting = SitecoreFieldSettings.DontLoadLazily";

    return string.Empty;
}

Next update the code generation to reference your method and pass through the field settings:

Line 75:

[SitecoreField(<#= AsInterfaceName(template.Name) #>Constants.<#= GetPropertyName(field) #>FieldName <#= GetGlassFieldLoadLazily(field) #>)]

Line 119:

[SitecoreField(<#= AsInterfaceName(template.Name) #>Constants.<#= GetPropertyName(field) #>FieldName <#= GetGlassFieldLoadLazily(field) #>)]

For every field which you wish the setting to apply to, update the properties for that field in TDS and set dontloadlazy=true in Custom Data.

TDS Don't Lazily Load

Generate the code for your project and then property will now be added to the generated code.

|improve this answer|||||
1

1) Extend /sitecore/templates/System/Templates/Template field {455A3E98-A627-4B40-8035-E683A0331AC7} with your field, e.g. checkbox "Not lazy"

2) Change T4 template(line 119) to use this property for generation different code

    <#if(GetValue(field.SitecoreFields, "Not lazy")){#>
    [SitecoreField(<#= AsInterfaceName(template.Name) #>Constants.<#=GetPropertyName(field) #>FieldName), Setting = SitecoreFieldSettings.DontLoadLazily]
    <#}#>
    <#else{#>
    [SitecoreField(<#= AsInterfaceName(template.Name) #>Constants.<#=GetPropertyName(field) #>FieldName)]
    <#}#>
    public virtual <#=GetGlassFieldType(field)#> <#= GetPropertyName(field) #>  {get; set;}

I know that it is not too cool to change Sitecore system items, but I don't see another way to do it. Probably also you could try to use "Custom Data" property in VS(but it seems that it could require modification of TDS code that call T4 template execution).

|improve this answer|||||
  • 2
    Anton, thank you for the answer, and welcome to the site! It appears that it is really easy to get Custom Data from TDS item settings. We can just use GetCustomProperty(), as jammykam demonstrated in his answer, so I prefer this to creating new item fields. I appreciate your solution nevertheless! I hope you'll continue posting quality answers on this site :) – Dmytro Shevchenko Dec 14 '16 at 8:48
  • 1
    I didn't know about function GetCustomProperty, but felt that "Custom Data" could be somehow used. And answer of jammykam in general is the same, but more with knowledge about GetCustomProperty and more advanced. – Anton Dec 14 '16 at 9:02

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.