Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions classes/local/data_source/data_source_factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,27 @@ public static function build_data_source($identifier, \context $context) {
return new $identifier($context);
}

/**
* Whether a registered data source should be offered to the current user in
* the given context.
*
* A registering plugin may attach a `'visible' => callable($identifier,
* $context): bool` entry to its registry record to gate the data source by
* context/role at authoring time (e.g. dashaddon_repository's per-preset
* block-builder audience). Entries without a callback are always visible.
*
* @param string $identifier
* @param \context $context
* @return bool
*/
public static function is_visible_in_context($identifier, \context $context) {
$info = self::get_data_source_info($identifier);
if ($info && isset($info['visible']) && is_callable($info['visible'])) {
return (bool) call_user_func($info['visible'], $identifier, $context);
}
return true;
}

/**
* Get options array for select form fields.
*
Expand Down
4 changes: 2 additions & 2 deletions edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public static function dash_features_list(&$mform, $context, $page) {

$group[] = $mform->createElement('html', html_writer::start_div('datasource-content'));
foreach ($datasources as $id => $source) {
if (block_dash_visible_addons($id)) {
if (block_dash_visible_addons($id) && data_source_factory::is_visible_in_context($id, $context)) {
$group[] = $mform->createElement('html', html_writer::start_div('datasource-item'));
$group[] = $mform->createElement('radio', 'config_data_source_idnumber', '', $source['name'], $id);
if ($help = $source['help']) {
Expand All @@ -457,7 +457,7 @@ public static function dash_features_list(&$mform, $context, $page) {
);
$widgets[] = $mform->createElement('html', html_writer::start_div('datasource-content'));
foreach ($widgetlist as $id => $source) {
if (block_dash_visible_addons($id)) {
if (block_dash_visible_addons($id) && data_source_factory::is_visible_in_context($id, $context)) {
$widgets[] = $mform->createElement('html', html_writer::start_div('datasource-item'));
$widgets[] = $mform->createElement('radio', 'config_data_source_idnumber', '', $source['name'], $id);
if ($source['help']) {
Expand Down
Loading