yurj
(Yuri)
1
Hi!
in this guide: Tutorial: Overriding Viewlets โ Plone Documentation v5.2
the code is:
class TitleViewlet(ViewletBase):
index = ViewPageTemplateFile('title.pt')
def update(self):
[...]
while in plone 5.2 and plone 6 the original code is:
class LogoViewlet(ViewletBase):
index = ViewPageTemplateFile("logo.pt")
def update(self):
super(LogoViewlet, self).update()
[...]
What is the right approach? Is the super part mandatory?
zopyx
(Andreas Jung)
2
Possibly not since you derive from the same base class.
yurj
(Yuri)
3
Yes, but calling .update() on the base class set some local properties. If I remove it:
AttributeError: 'LogoViewlet' object has no attribute 'portal_state'
the super update method does this:
def update(self):
self.portal_state = getMultiAdapter(
(self.context, self.request), name=u"plone_portal_state"
)
self.site_url = self.portal_state.portal_url()
self.navigation_root_url = self.portal_state.navigation_root_url()
zopyx
(Andreas Jung)
4
Then you need to replicate the related code from the update()
implementation
1 Like
tmassman
(Thomas Massmann #BlackLivesMatter)
5
The TitleViewlet
should use super()
as well. It currently redefines portal_state
in plone.app.layout/common.py at cc88fde5ec40ccec8d5128ee5db17f226125b661 ยท plone/plone.app.layout ยท GitHub.
As a rule of thumb: if the base class has a method you want to extend or use a return value from it, use super()
; If you want to override it, don't.
2 Likes