Skip to content

Commit

Permalink
MDL-72720 editor: Implement enable_plugin() method
Browse files Browse the repository at this point in the history
  • Loading branch information
sarjona committed Oct 15, 2021
1 parent e62f8ed commit e8c60cf
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
27 changes: 10 additions & 17 deletions admin/editors.php
Expand Up @@ -39,22 +39,19 @@
redirect($returnurl);
}


$return = true;
switch ($action) {
case 'disable':
// remove from enabled list
$key = array_search($editor, $active_editors);
unset($active_editors[$key]);
add_to_config_log('editor_visibility', '1', '0', $editor);
// Remove from enabled list.
$class = \core_plugin_manager::resolve_plugininfo_class('editor');
$class::enable_plugin($editor, false);
break;

case 'enable':
// add to enabled list
// Add to enabled list.
if (!in_array($editor, $active_editors)) {
$active_editors[] = $editor;
$active_editors = array_unique($active_editors);
add_to_config_log('editor_visibility', '0', '1', $editor);
$class = \core_plugin_manager::resolve_plugininfo_class('editor');
$class::enable_plugin($editor, true);
}
break;

Expand All @@ -68,6 +65,8 @@
$active_editors[$key] = $active_editors[$key + 1];
$active_editors[$key + 1] = $fsave;
add_to_config_log('editor_position', $key, $key + 1, $editor);
set_config('texteditors', implode(',', $active_editors));
core_plugin_manager::reset_caches();
}
}
break;
Expand All @@ -82,6 +81,8 @@
$active_editors[$key] = $active_editors[$key - 1];
$active_editors[$key - 1] = $fsave;
add_to_config_log('editor_position', $key, $key - 1, $editor);
set_config('texteditors', implode(',', $active_editors));
core_plugin_manager::reset_caches();
}
}
break;
Expand All @@ -90,14 +91,6 @@
break;
}

// at least one editor must be active
if (empty($active_editors)) {
$active_editors = array('textarea');
}

set_config('texteditors', implode(',', $active_editors));
core_plugin_manager::reset_caches();

if ($return) {
redirect ($returnurl);
}
36 changes: 36 additions & 0 deletions lib/classes/plugininfo/editor.php
Expand Up @@ -51,6 +51,42 @@ public static function get_enabled_plugins() {
return $enabled;
}

public static function enable_plugin(string $pluginname, int $enabled): bool {
global $CFG;

$haschanged = false;
if (!empty($CFG->texteditors)) {
$plugins = array_flip(explode(',', $CFG->texteditors));
} else {
$plugins = [];
}

// Only set visibility if it's different from the current value.
if ($enabled && !array_key_exists($pluginname, $plugins)) {
$plugins[$pluginname] = $pluginname;
$haschanged = true;
} else if (!$enabled && array_key_exists($pluginname, $plugins)) {
unset($plugins[$pluginname]);
$haschanged = true;
}

// At least one editor must be active.
if (empty($plugins)) {
$plugins['textarea'] = 'textarea';
$haschanged = true;
}

if ($haschanged) {
$new = implode(',', array_flip($plugins));
add_to_config_log('editor_visibility', !$enabled, $enabled, $pluginname);
set_config('texteditors', $new);
// Reset caches.
\core_plugin_manager::reset_caches();
}

return $haschanged;
}

public function get_settings_section_name() {
return 'editorsettings' . $this->name;
}
Expand Down

0 comments on commit e8c60cf

Please sign in to comment.