Hiding a Block based on capability: Moodle 3.9+

Hiding a Block based on capability: Moodle 3.9+

by Dave Emsley -
Number of replies: 1

Hi there,

I've created a block which works fine but I want it only available based on a capability.  I've set up the capability in <<myblock>>/db/access.php

$capabilities = array(
'block/<<myblock>>:useblock' => array(
        'captype'      => 'read',
        'contextlevel' => CONTEXT_SYSTEM,
        'archetypes'   => array(
            'teacher'        => CAP_ALLOW,
            'editingteacher' => CAP_ALLOW,
            'manager'          => CAP_ALLOW
        )
    ),
);

However I'm confused as to how I check that the user has that capability before displaying the block.  If anyone could point me in the right direction I'd be grateful.

Cheers

Dave

Average of ratings: -
In reply to Dave Emsley

Re: Hiding a Block based on capability: Moodle 3.9+

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
You use has_capability to check permissions. Look for some examples in the code of core blocks.
Average of ratings: Useful (1)