Moodle块的正确上下文ID是什么 [英] What is the correct context id of Moodle block
问题描述
面条块 block_html
的正确context id
是什么.
案例1
在block_html.php
文件
$this->context->id
给出 122
案例2
我创建一个名为view.php
的新页面.在这里我尝试
I create a new page named view.php
. Here I try
$instance = $DB->get_record('block_instances', array('id' => 57));
$blockname = 'html';
$block = block_instance($blockname, $instance);
echo $block->context->id;
这给出了 98 .
案例3
$context = context_block::instance($cmid);
echo $context->id
给出 7
我应该用什么作为block_html
的context id
.我需要确切的context id
.
What should I use as context id
of block_html
. I need the exact context id
.
注意:对于所有块,我都遇到上述问题.我怎么了?
Note: For all blocks I am getting the issue described above. What is the wrong I used?
推荐答案
案例1和2 应该正确.
在block::get_content()
中,可以肯定地使用$this->context
获得块的正确上下文.
In block::get_content()
you get the right context of the block using $this->context
, for sure.
要手动获取块的上下文,请执行context_block::instance($instanceid)
.我怀疑在您的示例中,您错误地使用了错误的块实例ID.您可以使用$this->context->instanceid
从get_content
获取块实例ID.
To manually get the context of a block you do context_block::instance($instanceid)
. I suspect is that in your example you mistakenly used the wrong block instance ID. You can get the block instance ID from get_content
using $this->context->instanceid
.
这篇关于Moodle块的正确上下文ID是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!