Kentico 12 MVC Widgets - Identify which "Section" Widget was added to in Contorller and/or View?

Saul Sheehan asked on February 25, 2019 09:45

I have a widget that I'd like to behave differently dependent on the MVC Section it's been added to (i.e. single or two column section).. Is it possible to identify the section in the widget controller (and/or view if needed) to allow this?

Recent Answers


Trevor Fayas answered on February 25, 2019 14:58 (last edited on February 25, 2019 14:58)

I think the best you can do is have a property on the widget that you set how it should behave, I do not see any way for the widget to know exactly what section it is in aside from looking at the mvc widget json on the cms_document table row for the current document. You could use that to try to locate the current widget and look at the parent section but that's a lot of run around. I don't suppose you can just use css to simply make your markup look different depending on it's width, because that's usually how it's done since a two column can become one column on mobile.

0 votesVote for this answer Mark as a Correct answer

Dragoljub Ilic answered on February 25, 2019 15:40

Hi,

As addition, you can also create two widgets and restrict them by identifier directly for specific section. Solution is not ideal, but it's clean and approach is suggested by Kentico documentation. Here is code sample how to achieve that:

<div>
    @{
        string[] allowedWidgets = { Kentico.Forms.Web.Mvc.Widgets.KenticoFormWidgetController.WIDGET_IDENTIFIER,
                                    "LearningKit.Widgets.NumberWidget",
                                    "LearningKit.Widgets.SomeOtherWidget" };
    }
    @Html.Kentico().EditableArea("limitedArea", allowedWidgets: allowedWidgets)
</div>

You can find more on this link.

0 votesVote for this answer Mark as a Correct answer

Saul Sheehan answered on February 26, 2019 10:03

Thanks for the replies. I am already using the allowedWidgets restrictions. I could in theory create two identical widgets, with the display same name (but different class name), and set the allowed widget to use the appropriate one on each place holder. I want to avoid unnecessary duplicate code, but should be able to share the basic logic between the controllers.

I'll look into the widget JSON (I am already serialising this elsewhere), but I'm not sure I'll be able to identify the same widget between placeholders.

Regarding using CSS - this sadly isn't possible in my case - We're building an AMP website (https://www.ampproject.org/), and using the amp-image tag. This needs width and height properties which differ depending which place holder they're added to.

Right now, I've just added a property that the editor must change depending which place holder they added it to. I can then use this in the controller to create the Viewmodel as required.

0 votesVote for this answer Mark as a Correct answer

David te Kloese answered on February 26, 2019 10:38

Keep in mind that the limiting Dragoljub mentioned is for the whole editable area, not for a section or widget zone in in that area.

Also remember that an editor can change the sections itself and widgets will just be moved to the next available widget zone.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.