2
votes

I have two similar problems that I suspect have a common solution.

1) I'd like to create custom Parts that are Attachable, but only to specific content types, only Taxonomies for example. It would be really cool if that was possible out of the box through migrations e.g something like .Attachable(cfg => cfg.ToType("Taxonomy")) but I don't think it is.

Currently, to prevent my custom Part from being used on content that it's not intended for, I just write checks in the driver methods:

protected override DriverResult Editor(CustomPart part, dynamic shapeHelper)
{
    if (part.ContentItem.ContentType != "Taxonomy") return null;

    return ContentShape("Parts_Custom_Edit", ...
}

Is this a good way to go about it? Would the Handler be better fit for this kind of logic?

2) Similarly, I'd like to be able to conditionally attach different Parts to different individual Content Items. For example, I would like only first level parent Terms in a Taxonomy to have some fields while child Terms have some others.

The best way I can currently come up with to handle this is to just create one Part that holds all fields and run similar checks to the one above in its Driver methods to return different models depending on its container. Then in the template View I check which fields to render:

@if (Model.ThisField != null) {
    <div>@Html.EditorFor(m => m.ThisField)</div>
}
else {
    <div>@Html.EditorFor(m => m.ThatField)</div>
}

Ideally I'd like to create one attachable Part that's capable of adding several non-attachable secondary Parts to existing Content Items when it is attached to a Type and to new Content Items when they are created or updated. Is there a painless way to do this? I think 'Welding' might be what I need but I haven't been able to find any documentation or tutorials that can explain Welding to me like I'm five.

1
Would you like them to be able to attach to certain types, or always attached to certain typesdevqon
I'd like to be able to. Otherwise I could make them unattachable and attach them in migration. In the case of attaching to specific Content Items though, I figure that's always going to have to happen programmatically.Lawyerson
I have to re-read your issue once again later, but from what you describe I think the best way to do this is welding on the parts in the content handler. You're right when it comes to docs about welding, I suggest you just step through the Weld<TPart>() Method to figure out how it works, it's just a couple of lines.Xceno
Thank you @Xceno I'll look into that.Lawyerson

1 Answers

0
votes

I think you need to implement a dynamic welding approach. I had to solve a similar issue, it is posted here. Hope this helps.