Conditionally override block
Archived from the Xataface Users forum.
Gershy — Tue Jan 29, 2013 2:03 pm
Hi everyone!
The users of my application have an authority level. All levels ought to be able to view a table called “Applications” but for some levels I would like to override the main column block to display different information within the same table tab.
The behaviour I’m looking for is comparable to delegating to a superclass, however this isn’t feasible as the table delegate class has no superclass. Here’s what I mean:
- Code: Select all
- `function block__main_column() {
$user = Dataface_AuthenticationTool::getInstance()->getLoggedInUser();
$authority = $user->val(‘Authority’);if ($authority == ‘Overridden Behaviour’) {
//Perform custom main_column decoration here
} else if ($authority == ‘Normal Behaviour’) {
parent::block__main_column();
}
}`
Considering that the “parent::block__main_column();” call cannot be performed (due to lack of a superclass), how ought I go about imitating the above mechanism?
Thanks in advance!
-Gersh
shannah — Tue Jan 29, 2013 2:17 pm
If you return a PEAR_Error from the block method, then it will ignore your block and use the default.
e.g.
- Code: Select all
function block__myblock(){ if ( /*some condition*/ ){ echo "My block content"; } else { return PEAR::raiseError("Just use the default block"); } }
Gershy — Tue Jan 29, 2013 2:33 pm
Terrific. I would have never guessed that. Thanks for the quick response!
-Gershy
Gershy — Thu Jan 31, 2013 5:34 pm
Also I presume that if I want to conditionally APPEND the default behaviour (as opposed to just overriding it) I ought to use write a block__after_main_column method in the delegate class, correct?…
shannah — Mon Feb 04, 2013 9:37 am
Yes. I can’t think of an easy way to “append” to a block, rather than override it. You would have to look for a different block. In many cases there are after_xxx blocks that accompany the xxx block.
-Steve