Piranha.core: Adding new fields to a block crashes old pages that have that block

Created on 10 Feb 2019  路  2Comments  路  Source: PiranhaCMS/piranha.core

Hi there,

Every time I add a new field to a block, old pages crashes with this error.
Not just in frontend but in manager edit page also.

NullReferenceException: Object reference not set to an instance of an object.

AspNetCore.Areas_Manager_Views_Shared_EditorTemplates_GalleryBlock.ExecuteAsync() in GalleryBlock.cshtml

<div class="form-group">
    <label>Image #16</label>
    @Html.EditorFor(m => m.Image16) *** OLD FIELD JUST FINE ***
</div>
<div class="form-group">
    <label>Image #17</label>
    @Html.EditorFor(m => m.Image17) *** NEW FIELD NOT SO GOOD ***
</div>
<div class="form-group">

My come-up crazy solution for this;

  • Remove new field for now
  • Remove every block from every page that have it
  • Re-add new field
  • Re-add that block

Of course all my old data have to be removed and need to re-add also.

question wontfix

Most helpful comment

Hi there. When a new block is created it is created with the method IContentService.CreateBlock. This methods instantiates all properties that are inherited from IField. When an existing block is loaded it is just deserialized which means that non-existing fields will be null. Performing an automatic initialization of every null field on load would be a negative performance hit as this is a runtime operation and not an admin operation.

To solve this problem (without loosing your data), just instantiate your fields in your class, like so.

public class MyBlock : Block
{
    public StringField Title { get; set; } = new StringField();
    public TextField Body { get; set; } = new TextField();
}

Regards

All 2 comments

Hi there. When a new block is created it is created with the method IContentService.CreateBlock. This methods instantiates all properties that are inherited from IField. When an existing block is loaded it is just deserialized which means that non-existing fields will be null. Performing an automatic initialization of every null field on load would be a negative performance hit as this is a runtime operation and not an admin operation.

To solve this problem (without loosing your data), just instantiate your fields in your class, like so.

public class MyBlock : Block
{
    public StringField Title { get; set; } = new StringField();
    public TextField Body { get; set; } = new TextField();
}

Regards

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

peppelorum picture peppelorum  路  3Comments

MehranDHN picture MehranDHN  路  3Comments

codesoroush picture codesoroush  路  6Comments

aneff-official picture aneff-official  路  4Comments

mtlc1402 picture mtlc1402  路  5Comments