Skip to content

Instantly share code, notes, and snippets.

@nathanwoulfe
Created October 19, 2022 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathanwoulfe/aaa07b01d3e042d9559a2f1a15961602 to your computer and use it in GitHub Desktop.
Save nathanwoulfe/aaa07b01d3e042d9559a2f1a15961602 to your computer and use it in GitHub Desktop.
Inheritance to composition
-- CREATES THE COMPOSITION FROM THE INHERITED TYPE
DECLARE @CompositionId int;
DECLARE @CompositionsFolder int;
DECLARE @PropertyTab int;
DECLARE @CompositionGUID uniqueidentifier;
-- Generate a unique guid for the metadata composition
SET @CompositionGUID = 'f583bcf5-b007-47d4-992e-c193f225f76c'
DECLARE @PropertyTagGUID uniqueidentifier;
-- Generate a unique guid for the property grouping
SET @PropertyTagGUID = 'afcd44ea-4cfd-4101-83ee-7c648cb8a542'
--Get Composition Folder Id - likely you have a folder for creating compositions in, this get the id and sets a variable
SET @CompositionsFolder = (Select [id] from umbracoNode where [text] = 'Compositions' and nodeObjectType = '2F7A2769-6B0B-4468-90DD-AF42D64F7F16')
-- Create new Metadata Composition
-- Create in umbracoNode
IF NOT EXISTS (Select 1 from umbracoNode where [text] = 'Metadata' and nodeObjectType = 'A2CB7800-F571-4787-9638-BC48539A0EFB')
BEGIN
INSERT into umbracoNode (trashed, parentId, nodeUser, [level], [path], sortOrder, uniqueId, [text],nodeObjectType,createDate)
Values(0, @CompositionsFolder, 0, 2, CONCAT('-1,', @CompositionsFolder), 0, @CompositionGUID, 'Metadata', 'A2CB7800-F571-4787-9638-BC48539A0EFB', GetDate())
----- update Path
SET @CompositionId = SCOPE_IDENTITY();
UPDATE umbracoNode Set [path] = ConCat('-1,', @CompositionsFolder, ',', @CompositionId) WHERE uniqueId = @CompositionGUID
----- add accompanying cmsContentType
INSERT INTO cmsContentType (nodeId, alias, icon, thumbnail, description, isContainer, allowAtRoot)
VALUES ( @CompositionId, 'metadata', 'icon-defrag color-pink','folder.png','Metadata',0,0)
----- create property type grouping for review date - use Metadata to land it on the existing tab
INSERT INTO cmsPropertyTypeGroup (contentTypeNodeId, text, sortOrder, uniqueId)
VALUES(@CompositionId, 'Metadata', 5, @PropertyTagGUID)
Set @PropertyTab = SCOPE_IDENTITY();
----- add new composition to all the content types that inherit master doc type (id = 1197)
INSERT INTO cmsContentType2ContentType (parentContentTypeId, childContentTypeId)
Select @CompositionId, childContentTypeId from cmsContentType2ContentType where parentContentTypeId = 1197
-- finally lets move the properties from the master document type metadata tab to the new Composition
Update cmsPropertyType SET contentTypeId = @compositionId, propertyTypeGroupId = @PropertyTab Where id in ( SELECT [id] FROM [cmsPropertyType] where alias = 'keywords' and contentTypeId = 1197)
Update cmsPropertyType SET contentTypeId = @compositionId, propertyTypeGroupId = @PropertyTab Where id in ( SELECT [id] FROM [cmsPropertyType] where alias = 'description' and contentTypeId = 1197)
Update cmsPropertyType SET contentTypeId = @compositionId, propertyTypeGroupId = @PropertyTab Where id in ( SELECT [id] FROM [cmsPropertyType] where alias = 'umbracoUrlAlias' and contentTypeId = 1197)
Update cmsPropertyType SET contentTypeId = @compositionId, propertyTypeGroupId = @PropertyTab Where id in ( SELECT [id] FROM [cmsPropertyType] where alias = 'umbracoRedirect' and contentTypeId = 1197)
Update cmsPropertyType SET contentTypeId = @compositionId, propertyTypeGroupId = @PropertyTab Where id in ( SELECT [id] FROM [cmsPropertyType] where alias = 'bannerExpiryDate' and contentTypeId = 1197)
END
-- now we need to relate all these children items to the parent of the parent Document type, and remove their direct relation to parent - and move them up a ‘level’
-- this will likely vary based on how the site is structured - might be more/fewer levels of inheritance
BEGIN
-- if our Master Document Type has id 1197
Update umbracoNode Set level = 1, parentId=-1, path=Replace(path, ',1197','') where [id] in (Select childContentTypeId FROM cmsContentType2ContentType where parentContentTypeId = 1197) and nodeObjectType = 'A2CB7800-F571-4787-9638-BC48539A0EFB'
-- now relate the children of Pages Doc Type to the Parent of the parent Doc Type
INSERT INTO cmsContentType2ContentType (parentContentTypeId, childContentTypeId)
Select -1, childContentTypeId from cmsContentType2ContentType where parentContentTypeId = 1197
--- Now remove the direct relationship with the parent document type
DELETE FROM cmsContentType2ContentType where parentContentTypeId = 1197
UPDATE umbracoNode set [text] = '_CanDelete-MasterDocumentType' where id =1197
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment