How to render a new menu to display a menu bar in moodle

156 Views Asked by At

I am trying to custom the classic theme to a theme (called theant theme) as a child theme of the theme boost.

I want to get all courses with their links in my moodle to display them as a menu in all of site. It looks like as the black menu bar in the picture. (https://i.stack.imgur.com/V46n[enter image description here](https://i.stack.imgur.com/oW8RI.png)t.png)

The menu bar is defined in the file 'theme/theant/templates/navbar.mustache' as follows

<nav class="fixed-top navbar navbar-bootswatch navbar-expand moodle-has-zindex">

    <div class="fixed-subtop navbar" style="background-color: #333333; color: white;">

        <ul>

        {{#allcourses_menu}}

            <li><a href="{{courselink}}"> "{{coursename}}"</a></li>

        {{/allcourses_menu}}

      </ul>

     </div>

and the navbar.mustache is called by the template file 'theme/theant/templates/column.mustache' by code:

{{> theme_theant/navbar }}

In file 'theme/theant/renderers.php', I wrote (basing on the tutorial https://docs.moodle.org/dev/Extending_the_theme_custom_menu)

class theme_theant_core_renderer extends \core_renderer {

protected function render_allcourses_menu($allcourse_menu = null) {
      global $CFG;
      require_once($CFG->dirroot.'/lib/datalib.php');
      $allcourses = get_courses();
      $allcourses_menu = array();
       foreach ($allcourses as $course) {
                     array_push($allcourses_menu, array("coursename"=>($course->shortname),
                      "courselink"=>(new moodle_url('/course/view.php', array('id' => $course->id)))));
         }
         return $allcourses_menu;
     }
}

However, I don't know how to render $allcourses_menu to the template call it.

I try to put it in layout file 'theme/theant/layout/column.php' as follows

$allcourse_menu = $theme_theant_core_renderer::render_allcourses_menu();
....
$templatecontext = [
..........
'allcourses_menu'=> $allcourse_menu,
];
echo $OUTPUT->render_from_template('theme_theant/columns', $templatecontext);

But, I get a errors.

My questions are:

  1. There is any wrong in my code in 'theme/theant/renderers.php'? Is the code right location?
  2. How can I render $allcourses_menu to display as I need?

Thanks for your help. Cheers

Chon.

1

There are 1 best solutions below

1
On
  1. Could you please add the errors you are getting?
  2. Adding an object from class moodle_url to the array you want to render will not stringify the object automatically. This means that you are sending the entire object to the mustache template instead of the url itself. I usually do something like this "courselink"=>(new \moodle_url('/course/view.php', array('id' => $course->id))) . ''
  3. In you code you have $allcourse_menu = $theme_theant_core_renderer::render_allcourses_menu(); and I'm not sure how you've declared $theme_theant_core_renderer but I would suggest using the class name itself directly if you want to call a static method just to minimize the code and clean it up.