This project is read-only.

Problem about ActivatingFilter<T>

Topics: Writing modules
Dec 31, 2012 at 11:50 AM

Hi When I have read Orchard documents I didn't understand ActivatingFilter can anyone explain it more? ( I'm new in Orchard sorry!...)

Jan 2, 2013 at 1:48 AM

It just attaches a part to a content item.  For example, in the Orchard.Roles module they attach the UserRolesPart to the "User" content type...

    public class UserRolesPartHandler : ContentHandler {
        private readonly IRepository<UserRolesPartRecord> _userRolesRepository;

        public UserRolesPartHandler(IRepository<UserRolesPartRecord> userRolesRepository) {
            _userRolesRepository = userRolesRepository;

            Filters.Add(new ActivatingFilter<UserRolesPart>("User"));
            OnLoaded<UserRolesPart>((context, userRoles) => {
                                    userRoles.Roles = _userRolesRepository
                                        .Fetch(x => x.UserId == context.ContentItem.Id)
                                        .Select(x => x.Role.Name).ToList();

Jan 2, 2013 at 3:20 AM

Thank you but what is the difference between this and using migration class I have seen some third party modules use both of them for attaching a contentpart. for example for attaching UserPart module they first add it to a contenttype in migration class then use this filter to add it again.

Jan 3, 2013 at 11:23 PM
Edited Jan 3, 2013 at 11:53 PM

The migration way, as I understand it, would just be a more data-driven approach.  With the ActivatingFilter, you could apply some more logic to it if you wanted.  You don't need to do both though.  That wouldn't be necessary as far as I can tell.  For example, in the Orchard.Users module they only use the filter method. 

Jan 4, 2013 at 7:38 AM

They add UserPart with ActivatingFilter because UserPart doesn't implement driver interface is it true? Also  can I imagin that both approch have the same result but they do it in two different ways? Or in specific situation I have to choose one of them

Jan 4, 2013 at 8:46 PM

The UserPart has a driver, but it's not using display methods for another reason.  I think both approaches will accomplish the same end result.  One approach is just data-driven and the other attaches the part on the fly as the item is being activated.