Dynamic dashboard items

Hii there,

I’m trying to create a dashboard for my CMS.
Because I want the CMS to be modular, I want to be able to register the dashboard widgets on the fly like WordPress does.
I’m currently thinking where I “should” put the widgets their HTML (the layout) and PHP code (to get some stuff from the database and some APIs) and how it should come together.

How would you guys go about this?

Two options:

  1. Use elements. Have a “smart” dashboard controller that knows all the data that all the widgets need. Create dashboard controller action, either in a standalone controller (ie no related model entity) or as part of an entity which is in the “center” of the relevant data model if that makes sense. PHP controller code figures out all the data the registered widgets need, sets them as viewvars. Individual widgets implemented as Element CTP files. Controller action CTP code does the layout and hands the relevant data to the elements. This makes sense if the widgets share a lot of the same data since you can retrieve the data efficiently from the database (especially if you can put it into one big multi-table query) then parse/separate/segment it either in the controller or view code.
  2. Use ViewCells. Each cell has its own controller code and takes care of its own data retrieval, passing the data to its own ctp templates. The dashboard is then only responsible for layout of the view cells. This is much more modular, but runs the risk of redundant data retrieval since the view cell controllers don’t know what the other is doing. So it makes most sense if the widgets are disjoint data-wise.

I did not know about the existence of ViewCells, even though they can retrieve redundant data, it seems like the best way to do it as the plugins that can add their items to the dashboard are very loosely aware of one and another (unless explicitly coded to work together)