Should display drivers should return null if the item is logically empty? (Layout Placement)

Topics: Troubleshooting
Feb 26, 2013 at 2:47 AM
I think so, and I was about to add an issue for this, but perhaps there's something I'm missing about Layout Placement, so I'm checking here first:

For most fields & parts, a non-null shape is returned from ContentFieldDriver<>.Display(), and no check is made that the field or part is "logically empty". Examples are MediaPickerFieldDriver and TagsPartDriver. This results in un-needed markup being emitted when the field is placed into an otherwise empty zone using Layout Placement, making it difficult to work with responsive/grid layouts.

To be specific, my scenario is that I'm using Layout Placement to place a media picker field into AsideFirst. I see this as an easier alternative to widgets, for editors, & it's nice to not have to create extra layers for every page that's got an image in AsideFirst. The media picker field is added to the Page content type; if an image is provided, AsideFirst should be emitted, if not, the AsideFirst wrapper divs should not be emitted (assuming nothing else is assigned to AsideFirst). This works perfectly with widgets.

But with Layout Placement, the shape is being added regardless. This happens without LayoutPlacement too, but in the case of Layout Placement, an extra wrapper div corresponding to the parent zone is also being emitted un-necessarily.

If I check for an empty url in the MediaPickerFieldDriver & return null (the field is logically empty), the problem goes away.
Feb 26, 2013 at 6:02 AM
Yes, returning null could be the right thing to do in such cases, except if you want to let the template decide how to render an empty field: the template is easier to override than the driver. It's probably a decision that the author of the field should make on a case by case basis.

In your case, you should be able to suppress the rendering of the zone and move the markup that you need entirely into the field's template. This way, you should be able to get exactly the markup you need.
Apr 19, 2013 at 4:25 AM
planetClaire, chance you could post what you did to make this go away? I would be interested as I'm trying to fix exactly what you discussed. Thanks!
Apr 19, 2013 at 4:56 AM
Hi Schoe311, I took the easy way out went with widgets instead.
In my case, AsideFirst was a sidebar & I was trying to calculate how many columns the main content should take up depending on the existence of the MediaPickerField/AsideFirst.
I'm not sure that the answer to my original question is yes, because (as I think Bertrand was saying) there may be the case where you want something non-empty in the template for an empty field, like some kind of indication of emptiness.