Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • AbsolutelyN 37 posts 265 karma points
    Mar 01, 2019 @ 08:22
    AbsolutelyN
    0

    How do you get content CompositionAliases in IComponent?

    Hi

    I've just been looking at the new v8 version of the ApplicationEventHandler for porting a website to v8.

    Once of the things the ApplicationEventHandler I want to convert does it check if the content is composed of a specific document type. It does this like this:

            foreach (var node in args.SavedEntities)
            {
                var compositionAliases = node.GetContentType().CompositionAliases();
                if (compositionAliases.InvariantContains("ICompositionDocumentTypeAlias"))
                {
                           // do something with content if it is composed of specific document type ...
                }
            }
    

    I've found the example of the new IComponent method for events but I cannot see how you would achieve the above as there doesn't seem to be any methods for querying composition. I'm sure it must be possible but I can't figure it out or see anything in the docs. Any ideas?

    public class MyComponent : IComponent
    {
        // initialize: runs once when Umbraco starts
        public void Initialize()
        {
            ContentService.Saving += ContentService_Saving;
        }
    
        // terminate: runs once when Umbraco stops
        public void Terminate()
        {
        }
    
        private void ContentService_Saving(IContentService sender, ContentSavingEventArgs e)
        {
            foreach (var content in e.SavedEntities
                //Check if the content item type has a specific alias
                .Where(c => c.ContentType.Alias.InvariantEquals("MyContentType")))
            {
                //Do something if the content is using the MyContentType doctype
            }
        }
    }
    

    Cheers.

  • Sebastiaan Janssen 4894 posts 14638 karma points MVP admin hq
    Mar 01, 2019 @ 09:04
    Sebastiaan Janssen
    100

    We have made the models a bit lighter and not tightly coupled to all other kinds of models. So you'll have to retrieve the content type and iterate over the composition aliases, this seems to work for me:

    public class MyComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.Components().Append<MyComponent>();
        }
    }
    
    public class MyComponent : IComponent
    {
        private readonly IContentTypeService _contentTypeService;
    
        public MyComponent(IContentTypeService contentTypeService)
        {
            _contentTypeService = contentTypeService;
        }
        // initialize: runs once when Umbraco starts
        public void Initialize()
        {
            ContentService.Saving += ContentService_Saving;
        }
    
        // terminate: runs once when Umbraco stops
        public void Terminate()
        {
        }
    
        private void ContentService_Saving(IContentService sender, ContentSavingEventArgs e)
        {
            foreach (var content in e.SavedEntities)
            {
                var contentType = _contentTypeService.Get(content.ContentTypeId);
                var compositionAliases = contentType.CompositionAliases();
                if (compositionAliases.InvariantContains("MyContentType"))
                {
                    //Do something if the content is using the MyContentType doctype
                }
            }
        }
    }
    
  • AbsolutelyN 37 posts 265 karma points
    Mar 01, 2019 @ 09:17
    AbsolutelyN
    0

    Brilliant - thank you!

  • Jonathan Roberts 372 posts 1005 karma points
    May 19, 2020 @ 12:33
    Jonathan Roberts
    0

    Any idea how you get Current.Response.Cookies in IComponent too?

Please Sign in or register to post replies

Write your reply to:

Draft