Context of a block inside "my" page.

Context of a block inside "my" page.

by Eric Gagnon -
Number of replies: 3

Hello everyone, first post here.

I have read the documentation  (https://docs.moodle.org/27/en/Context) and used context in real code but I'm not sure to really understand the context hierarchy of a (custom) block inside "my" page.

- From within the block, $this->context is "block". That's fine.

- I would expect the parent context of "block" to be  "user" (logically not from the doc), but it is "system".

- Strangely, $this->page->context (which is in some ways the parent of the block widget?) return context user.

Can someone help me understand why block parent context  is system? Maybe I should always use the page context?

Thanks in advance,

Eric



Average of ratings: -
In reply to Eric Gagnon

Re: Context of a block inside "my" page.

by David Mudrák -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators
This may or may not help: https://docs.moodle.org/dev/Blocks/Appendix_A#.24this-.3Econtext

IIRC the parent context is where the block was originally created at, but I'm not quite sure. And it can also be affected by other settings. Such as "Display on entire site" makes the parent context to be the system context.
In reply to David Mudrák

Re: Context of a block inside "my" page.

by Eric Gagnon -

Thanks, that's the clarification I was looking for!

In reply to Eric Gagnon

Re: Context of a block inside "my" page.

by jon sharp -

There is a problem with this when editing user-specific settings for the block when on my page.

You can add the fields in specific_definition($mform), but blocks/edit_from.php does:

if (!$this->block->user_can_edit()) {
            $mform->hardFreezeAllVisibleExcept($pagefields);
}

which sadly does 'freeze' the fields from editing, as user_can_edit() does:

if (!empty($USER->id)
            && $this->instance->parentcontextid == $this->page->context->id   // Block belongs to this page
            && $this->page->context->contextlevel == CONTEXT_USER             // Page belongs to a user
            && $this->page->context->instanceid == $USER->id) {               // Page belongs to this user
            return has_capability('moodle/my:manageblocks', $this->page->context);
 }

and $this->instance->parentcontextid == $this->page->context->id   seems always to be unequal

$this->page->context->id is set to users context id, and $this->instance->parentcontextid is 1

Commenting this line out allows the input fields to be rendered.

Ref: https://docs.moodle.org/dev/Blocks/Appendix_A#.24this-.3Econtext '$this->context' para.

Not sure how to configure / code the block to get these to be equal?

This is in M3.0.3 - I think it worked in M2.6.