U4-9940 Make it easy to get the integer Id of an item from it's Udi
This commit is contained in:
@@ -36,8 +36,30 @@ namespace Umbraco.Web.Extensions
|
||||
return umbracoHelper.TypedMember(memberAttempt.Result);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An extension method to easily acquire the integer Id for a given udi
|
||||
/// </summary>
|
||||
/// <param name="udi"></param>
|
||||
/// <returns>An <see cref="int"/> identifier if the item is found, -1 otherwise</returns>
|
||||
public static int ToIntId(this Udi udi)
|
||||
{
|
||||
Udi identifier;
|
||||
if (Udi.TryParse(udi.ToString(), out identifier) == false)
|
||||
return -1;
|
||||
|
||||
var guidUdi = GuidUdi.Parse(udi.ToString());
|
||||
var umbracoType = Constants.UdiEntityType.ToUmbracoObjectType(identifier.EntityType);
|
||||
|
||||
var entityService = ApplicationContext.Current.Services.EntityService;
|
||||
var entity = entityService.GetByKey(guidUdi.Guid, umbracoType);
|
||||
if (entity == null)
|
||||
return -1;
|
||||
|
||||
return entity.Id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user