Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible to register blocks that doesn't inherit Piranha.Extend.Block #1587

Closed
Neteklab opened this issue Apr 14, 2021 · 4 comments
Closed
Assignees
Milestone

Comments

@Neteklab
Copy link

Neteklab commented Apr 14, 2021

Implementing a site with tons of blocks and sometimes forgetting to register a block and then debugging the error for hours so I'm trying to implement the subject in the Sturtup.Configure method:

         foreach( Type block in typeof( Startup ).Assembly.GetTypes( ).Where( 
            t => typeof( Piranha.Extend.Block ).IsAssignableFrom( t ))){ 

            MethodInfo method = typeof( AppBlockList ).GetMethod(
               nameof( AppBlockList.Register ) );

            MethodInfo generic = method.MakeGenericMethod( block );
            generic.Invoke( App.Blocks, null );
         }

it seems working as viewing already existing pages renders its blocks, but in the manager, when trying to add a block to a page, there are no blocks at all, not even native ones, like Content, Image and so on.

After returning to traditional registration it throws the following exception when trying to load a content page:

InvalidOperationException: Block parent is missing

Piranha.Services.ContentService<TContent, TField, TModelBase>.TransformBlocks(IEnumerable<Block> blocks)
Piranha.Repositories.PageRepository.ProcessAsync<T>(Page page, T model)
Piranha.Services.ContentService<TContent, TField, TModelBase>.TransformAsync<T>(TContent content, ContentTypeBase type, Func<TContent, T, Task> process, Nullable<Guid> languageId)
Piranha.Repositories.PageRepository.GetStartpage<T>(Guid siteId)
Piranha.Services.PageService.GetStartpageAsync<T>(Nullable<Guid> siteId)
Piranha.AspNetCore.PiranhaMiddleware.Invoke(HttpContext context, IApi api, IApplicationService service)
Piranha.AspNetCore.Security.SecurityMiddleware.InvokeAsync(HttpContext ctx, IApplicationService service)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Only database restore is helping in this case.
Also tried to invoke

method = typeof( AppDataList< BlockGroupBase, AppBlock >).GetMethod( 
 nameof( AppDataList< BlockGroupBase, AppBlock >.Register ));

without result.

@Neteklab
Copy link
Author

The problem seemed to be a basic block with no attributes that didn't need to be registered before switching to auto-registration. After adding the BlockGroupType attribute to this block, Piranha was able to handle it correctly.

@tidyui
Copy link
Member

tidyui commented Apr 16, 2021

@Neteklab So the issue here is really that it is possible to register a Block that doesn't inherit Piranha.Extend.Block?

@tidyui tidyui changed the title Automatic block registration Possible to register blocks that doesn't inherit Piranha.Extend.Block Apr 16, 2021
@tidyui tidyui added this to the Version 9.1 milestone Apr 16, 2021
@Neteklab
Copy link
Author

Not quite. There was a non-abstract base class BlockGroupBase without any BlockGroupType attributes like BlockGroupType, inherited from Piranha.Extend.BlockGroup, which inherits from Piranha.Extend.Block . There was no necessity to register it with Piranha.App.Blocks.Register. After filling the database with some inherited from BlockGroupBase blocks and then switching to automatic block registration, BlockGroupBase was accidentally registered. After that, the problem started to appear. After adding the BlockGroupType attribute to the BlockGroupBase class, the problem disappeared. Perhaps it should be excluded from automatic registration with attribute filtering.

@tidyui
Copy link
Member

tidyui commented Apr 29, 2021

@Neteklab Thanks for the detailed information about the issue!

@tidyui tidyui self-assigned this Apr 29, 2021
@tidyui tidyui closed this as completed in e397fef Apr 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants