MDL-55410 theme_noname: Convert custom menu renderer to render bootstrap4 menus.
Part of MDL-55071
This commit is contained in:
committed by
Dan Poltawski
parent
bbe79e0e37
commit
f130c411d9
@@ -2645,7 +2645,7 @@ class block_move_target {
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class custom_menu_item implements renderable {
|
||||
class custom_menu_item implements renderable, templatable {
|
||||
|
||||
/**
|
||||
* @var string The text to show for the item
|
||||
@@ -2832,6 +2832,37 @@ class custom_menu_item implements renderable {
|
||||
public function set_url(moodle_url $url) {
|
||||
$this->url = $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export this data so it can be used as the context for a mustache template.
|
||||
*
|
||||
* @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
|
||||
* @return array
|
||||
*/
|
||||
public function export_for_template(renderer_base $output) {
|
||||
global $CFG;
|
||||
|
||||
require_once($CFG->libdir . '/externallib.php');
|
||||
|
||||
$syscontext = context_system::instance();
|
||||
|
||||
$context = new stdClass();
|
||||
$context->text = external_format_string($this->text, $syscontext->id);
|
||||
$context->url = $this->url ? $this->url->out() : null;
|
||||
$context->title = external_format_string($this->title, $syscontext->id);
|
||||
$context->sort = $this->sort;
|
||||
$context->children = array();
|
||||
if (preg_match("/^#+$/", $this->text)) {
|
||||
$context->divider = true;
|
||||
}
|
||||
$context->haschildren = !empty($this->children) && (count($this->children) > 0);
|
||||
foreach ($this->children as $child) {
|
||||
$child = $child->export_for_template($output);
|
||||
array_push($context->children, $child);
|
||||
}
|
||||
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,6 +23,7 @@ use custom_menu_item;
|
||||
use custom_menu;
|
||||
use block_contents;
|
||||
use stdClass;
|
||||
use moodle_url;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
@@ -111,7 +112,7 @@ class core_renderer extends \core_renderer {
|
||||
}
|
||||
|
||||
if ($haslangmenu) {
|
||||
$strlang = get_string('language');
|
||||
$strlang = get_string('language');
|
||||
$currentlang = current_language();
|
||||
if (isset($langs[$currentlang])) {
|
||||
$currentlang = $langs[$currentlang];
|
||||
@@ -124,69 +125,12 @@ class core_renderer extends \core_renderer {
|
||||
}
|
||||
}
|
||||
|
||||
$content = '<ul class="nav">';
|
||||
foreach ($menu->get_children() as $item) {
|
||||
$content .= $this->render_custom_menu_item($item, 1);
|
||||
}
|
||||
|
||||
return $content.'</ul>';
|
||||
}
|
||||
|
||||
/*
|
||||
* This code renders the custom menu items for the
|
||||
* bootstrap dropdown menu.
|
||||
*/
|
||||
protected function render_custom_menu_item(custom_menu_item $menunode, $level = 0 ) {
|
||||
static $submenucount = 0;
|
||||
|
||||
$content = '';
|
||||
if ($menunode->has_children()) {
|
||||
|
||||
if ($level == 1) {
|
||||
$class = 'dropdown';
|
||||
} else {
|
||||
$class = 'dropdown-submenu';
|
||||
}
|
||||
|
||||
if ($menunode === $this->language) {
|
||||
$class .= ' langmenu';
|
||||
}
|
||||
$content = html_writer::start_tag('li', array('class' => $class));
|
||||
// If the child has menus render it as a sub menu.
|
||||
$submenucount++;
|
||||
if ($menunode->get_url() !== null) {
|
||||
$url = $menunode->get_url();
|
||||
} else {
|
||||
$url = '#cm_submenu_'.$submenucount;
|
||||
}
|
||||
$content .= html_writer::start_tag('a', array('href'=>$url, 'class'=>'dropdown-toggle', 'data-toggle'=>'dropdown', 'title'=>$menunode->get_title()));
|
||||
$content .= $menunode->get_text();
|
||||
if ($level == 1) {
|
||||
$content .= '<b class="caret"></b>';
|
||||
}
|
||||
$content .= '</a>';
|
||||
$content .= '<ul class="dropdown-menu">';
|
||||
foreach ($menunode->get_children() as $menunode) {
|
||||
$content .= $this->render_custom_menu_item($menunode, 0);
|
||||
}
|
||||
$content .= '</ul>';
|
||||
} else {
|
||||
// The node doesn't have children so produce a final menuitem.
|
||||
// Also, if the node's text matches '####', add a class so we can treat it as a divider.
|
||||
if (preg_match("/^#+$/", $menunode->get_text())) {
|
||||
// This is a divider.
|
||||
$content = '<li class="divider"> </li>';
|
||||
} else {
|
||||
$content = '<li>';
|
||||
if ($menunode->get_url() !== null) {
|
||||
$url = $menunode->get_url();
|
||||
} else {
|
||||
$url = '#';
|
||||
}
|
||||
$content .= html_writer::link($url, $menunode->get_text(), array('title' => $menunode->get_title()));
|
||||
$content .= '</li>';
|
||||
}
|
||||
foreach ($menu->get_children() as $item) {
|
||||
$context = $item->export_for_template($this);
|
||||
$content .= $this->render_from_template('core/custom_menu_item', $context);
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
{{^divider}}
|
||||
{{#haschildren}}
|
||||
<span class="dropdown nav-item">
|
||||
<a class="dropdown-toggle nav-link p-x-1" id="drop-down-{{uniqid}}" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" href="#">
|
||||
{{text}}
|
||||
</a>
|
||||
<div class="dropdown-menu" aria-labelledby="drop-down-{{uniqid}}">
|
||||
{{#children}}
|
||||
{{^divider}}
|
||||
<a class="dropdown-item" href="{{{url}}}" {{#title}}title="{{{title}}}"{{/title}}>{{text}}</a>
|
||||
{{/divider}}
|
||||
{{#divider}}
|
||||
<div class="dropdown-divider"></div>
|
||||
{{/divider}}
|
||||
{{/children}}
|
||||
</div>
|
||||
</span>
|
||||
{{/haschildren}}
|
||||
{{^haschildren}}
|
||||
<a class="nav-item nav-link" href="{{{url}}}" {{#title}}title="{{{title}}}"{{/title}}>{{text}}</a>
|
||||
{{/haschildren}}
|
||||
{{/divider}}
|
||||
Reference in New Issue
Block a user