I have an existing v7 site. It was initially single site and in one of the doctypes there is prevalue datatype this has list of options lets say they are:
Option 1
Option 2
Option 3
There is now a 2nd site added which is clone of first one but all its content has been updated. The issue now is that in this 2nd site i need slightly different options. I cannot update doctypes etc and have new list of prevalues as there will be too many rippling changes out to quite a few places including outside Umbraco. So ideally what I want todo is have my prevalues set to:
Option 1
Option 2
Option 3
Option A
Option B
Option C
Then in backoffice when any content that is loaded or created using that prevalue get it to show either all option1 - 3 or for other site option a - c. I know that using WebApiHandler like https://github.com/jbreuer/Hybrid-Framework-for-Umbraco-v7-Best-Practises/blob/master/Umbraco.Extensions/Utilities/WebApiHandler.cs you can hook into backoffice and do stuff. In fact I have done this before where like the linked code i needed to set a startnode for a given property. So is it possible to fudge the list of prevalues presented in this way?
So I created a new datatype with the values I need. I then implemented the event like so:
private void EditorModelEventManager_SendingContentModel(HttpActionExecutedContext sender, EditorModelEventArgs<ContentItemDisplay> e)
{
if (e.Model.ContentTypeAlias == Alert.ModelTypeAlias)
{
/
var rootId = e.Model.Path.Split(',')[2];
var siteRoot = e.UmbracoContext.Application.Services.ContentService.GetById(int.Parse(rootId));
if (siteRoot.Name.ToLower().Contains("site2"))
{
//we need
var property = e.Model.Properties.FirstOrDefault(f => f.Alias == Alert.GetModelPropertyType(c=>c.Audience).PropertyTypeAlias);
property.DataTypeId = new Guid("5f91261b-035e-4609-8b12-3dd7e68638aa");
}
}
}
Basically I am setting the property audience to this new datatype which is dropdown prevalue but with different values. However I still see the other values. Am I missing something or is this even possible?
Ok so I now have something working, feels very dirty though.
private void EditorModelEventManager_SendingContentModel(HttpActionExecutedContext sender, EditorModelEventArgs<ContentItemDisplay> e)
{
if (e.Model.ContentTypeAlias == Alert.ModelTypeAlias)
{
var rootId = e.Model.Path.Split(',')[2];
var siteRoot = e.UmbracoContext.Application.Services.ContentService.GetById(int.Parse(rootId));
if (siteRoot.Name.ToLower().Contains("site2"))
{
//we need
//will probably also need to fix up audience in grahpql??
var property = e.Model.Properties.FirstOrDefault(f => f.Alias == Alert.GetModelPropertyType(c=>c.Audience).PropertyTypeAlias);
var datatypeService = e.UmbracoContext.Application.Services.DataTypeService;
var all = datatypeService.GetAllDataTypeDefinitions();
var myAudience = all.First(x => x.Name.Equals("Alert Audience"));
property.DataTypeId = myAudience.Key;
var dataType = datatypeService.GetDataTypeDefinitionById(myAudience.Key);
var preValues = datatypeService.GetPreValuesCollectionByDataTypeId(dataType.Id);
Dictionary<int, Dictionary<string,string>> items = BuildItems(preValues);
property.Config["items"] = items;
}
}
}
private Dictionary<int, Dictionary<string,string>> BuildItems(PreValueCollection preValues)
{
var newDropDownItems = new Dictionary<int, Dictionary<string, string>>();
foreach (var item in preValues.PreValuesAsDictionary.Where(k=>k.Key!="multiple"))
{
var values = new Dictionary<string, string> {{"value", item.Value.Value},{"sortOrder",item.Value.SortOrder.ToString()}};
newDropDownItems.Add(item.Value.Id,values);
}
return newDropDownItems;
}
Works saves fine and when you view item in backoffice it restores value correctly. I am having issues on frontend which in my case is graphql may need to tap into the render with a resolver.
Modify pre values data in backoffice
I have an existing v7 site. It was initially single site and in one of the doctypes there is prevalue datatype this has list of options lets say they are:
There is now a 2nd site added which is clone of first one but all its content has been updated. The issue now is that in this 2nd site i need slightly different options. I cannot update doctypes etc and have new list of prevalues as there will be too many rippling changes out to quite a few places including outside Umbraco. So ideally what I want todo is have my prevalues set to:
Then in backoffice when any content that is loaded or created using that prevalue get it to show either all option1 - 3 or for other site option a - c. I know that using WebApiHandler like https://github.com/jbreuer/Hybrid-Framework-for-Umbraco-v7-Best-Practises/blob/master/Umbraco.Extensions/Utilities/WebApiHandler.cs you can hook into backoffice and do stuff. In fact I have done this before where like the linked code i needed to set a startnode for a given property. So is it possible to fudge the list of prevalues presented in this way?
Regards
Ismail
So thanks for Callum for this pointer and Paul for the blog post https://codeshare.co.uk/blog/how-to-add-default-values-to-umbraco-properties-in-the-backoffice/ I have the event working but still unsure how to swap or setup the dropdown of prevalues I want to show.
So I created a new datatype with the values I need. I then implemented the event like so:
Basically I am setting the property audience to this new datatype which is dropdown prevalue but with different values. However I still see the other values. Am I missing something or is this even possible?
Ok so I now have something working, feels very dirty though.
Works saves fine and when you view item in backoffice it restores value correctly. I am having issues on frontend which in my case is graphql may need to tap into the render with a resolver.
Regards
Ismail
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.