U4-9997 - .IsDocumentType(docTypeAlias,) on IPublishedContent does not work with folders

Created by Harvey Williams 08 Jun 2017, 14:06:36 Updated by Sebastiaan Janssen 17 Jul 2017, 20:06:40

Tags: PR

  • Create a folder, called 'Folder', under the Document Types in the Settings section
  • Under that folder, create a new document type called 'Page'
  • On the template for 'Page', we will check to see if 'Page' inherits document type from Document Type 'Base' (it doesn't, we just need to test)
  • Load the page - it will error with a null reference

The code to test:

if (Model.Content.IsDocumentType('base', true')) {
    // Our page inherits from that document type.
}

The error occurs here: https://github.com/umbraco/Umbraco-CMS/blob/1b844421fc7a8009dc5064ed193cefd0b67915e5/src/Umbraco.Web/PublishedContentExtensions.cs#L641

The issue is that the .ParentId can be the ID of a folder, not just a Document Type. The method which then gets the Document Type then returns null.

Either this is an issue with .GetContentType() or with .IsDocumentTypeRecursive().

.IsDocumentType() can be fixed by chaning .IsDocumentTypeRecursive():

var contentTypeService = UmbracoContext.Current.Application.Services.ContentTypeService;
var type = contentTypeService.GetContentType(content.DocumentTypeAlias);
while (type != null && type.ParentId > 0)
{
	type = contentTypeService.GetContentType(type.ParentId);
	if (type != null && type.Alias.InvariantEquals(docTypeAlias))
		return true;
}
return false;

Or it could be changed to use the newer .IsComposedOf() method:

var contentTypeService = UmbracoContext.Current.Application.Services.ContentTypeService;
var type = contentTypeService.GetContentType(content.DocumentTypeAlias);
if (type.Alias.InvariantEquals(docTypeAlias) || content.IsComposedOf(docTypeAlias))
    return true;
return false;

Comments

Shannon Deminick 12 Jul 2017, 09:19:47

PR: https://github.com/umbraco/Umbraco-CMS/pull/1989


Priority: Normal

Type: Bug

State: Fixed

Assignee:

Difficulty: Normal

Category: Architecture

Backwards Compatible: True

Fix Submitted: Pull request

Affected versions: 7.5.4

Due in version: 7.6.5

Sprint: Sprint 63

Story Points:

Cycle: 3