·

photo credit: Manu_H via photopin cc

Dependencies in Magento Widgets

Did you know that when you’re creating a Magento Widget, you can show or hide specific fields according to the values of other fields? These are called dependencies, and are actually pretty easy to implement.

Our Magento widget

Let’s say we have a widget that allows your administrator to insert some content into your page. This content is optional, so your administrator is not required to fill these fields in. The basic approach could be something like this:

Add the dependencies

Now the thing with this, is that even though your administrator is not required to fill in this content, the fields still are shown. We can make this widget more user-friendly by adding a <depends> -node to some of the fields:

Now this is a very basic example, but when source models get more complex, this solution can can come in quite handy. If you have a source model that returns more options for example, you can create whole forms with dependencies, giving your administrator a more clean and intuitive way to implement their widgets.

In conclusion

Widgets are a great way to allow your content editors in Magento to embed rich HTML content without worrying that they break your HTML layout. Taken in mind that you can also easily remove the wrapping paragraph around widgets they really are an underrated feature in Magento. Widget dependencies are just a small feature that make this even more simple for them.

Visitors give this article an average rating of 5.0 out of 5.

How would you rate this article?

2 thoughts on “Dependencies in Magento Widgets”

  1. Just wanted to add a note that a casual effort at implementing ‘depends’ with radio buttons didn’t seem to work.

    Upon switching the type from ‘radios’ to ‘dropdown’ however everything works as expected.

  2. tommykolkman says:

    Have you played around with this in Magento 2 already as well? It seems that the tag only works on text parameters there. Any idea? (Might be a bit off topic, but I thought I’d try it anyway).

Leave a Reply