Essential Theme - Items in Navbar

Essential Theme - Items in Navbar

by Saeed Amiri -
Number of replies: 42

Hi every one

I am interested to hide some menus and items in navigation bar after a user is logged in. Items such as "register, or "about", for instance, are not necessary to the registered user any longer after the login.

Can anyone help me with this.

Many thanks

Average of ratings: -
In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Danny Wahl -

you'll have to modify the renderer:

if(!isloggedin()) {output the login/register stuff....}

 

In reply to Danny Wahl

Re: Essential Theme - Items in Navbar

by Saeed Amiri -

Hi Danny

Thanks for the reply. Maybe I did not clarify myself well, but I want items such as "login", "register", etc. to appear only before the user logs in (I mean on the frontpage only), but I think adding if(!isloggedin()) {output the login/register stuff....} makes them appear after login. I have added commands in the custom menu of the theme's setting and now they seem to appear in all pages of the entire site. How can I make them appear only on the frontage?

Thanks in advance.

In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

if(!isloggedin()) -  the ! means 'is not' => 

if(!isloggedin()) {output the login/register stuff....} in plain language would be:

If the user is NOT logged in then output the login/register stuff...

But you will need to do this by overriding the custommenu renderer, so you could try reading this first http://docs.moodle.org/dev/Overriding_a_renderer

Richard

In reply to Richard Oelmann

Re: Essential Theme - Items in Navbar

by Saeed Amiri -

Hi Richard

Thank you very much for the explanation. I am new to coding and programming in general. I will read the linked page and do as instructed.

Thanks again

In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

You can change the menu using a simple setting that would allow you to have the elements you require before login by using this theme defined custom menu in the frontpage.php as definded in theme/essential/config.php using THIS SETTING

This creates a separate custom menu setting in your theme's custom settings page, Appearance > Themes > Essential general settings. So all the items listed in this 'theme based menu' is only seen on the front page. The code needed in the frontpage.php LOOKS LIKE THIS

If you do not add any content in the custom menu setting it uses the one set in the normal way from those items added in Appearance > Themes >Theme settings page which is where you are adding the items now.

All these changes can be found in a theme I made call Aaradyha which can be found HERE

Hope this helps?

Mary

In reply to Mary Evans

Re: Essential Theme - Items in Navbar

by Saeed Amiri -

Thanks Mary.

Great solution as ever.

I added the lines

// Theme overrides custom menu setting...
    $name = 'theme_aaradyha/custommenuitems';
    $title = get_string('custommenuitems', 'admin');
    $description = get_string('configcustommenuitems', 'admin');
    $default = '';
    $setting = new admin_setting_configtextarea($name, $title, $description, $default);
    $settings->add($setting);
 
to the theme's config.php and the two lines of 
$custommenu = $OUTPUT->custom_menu($PAGE->theme->settings->custommenuitems);
$hascustommenu = (empty($PAGE->layout_options['nocustommenu']) && !empty($custommenu));
 
 to the frontage.php in the layout folder.
 
But, It gives the following error
 
Fatal error: Call to undefined method stdClass::add() in .../theme/grace/config.php on line 189
 
Should I have made other modifications in other places?
 
Many thanks in advance.
 

 

In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Saeed Amiri -

By the way, I also changed the  'theme_aaradyha to the name of my own "child" theme.

In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Saeed,

The lines you have added to config are NOT config file lines they are part of a settings page (settings.php) and work within that settings page.

Without wishing to seem negative, I would suggest you take a look at the way some of the existing themes are structured and read some of the 'how to create a theme' docs which are linked from the top of this forum.

I realise you have stated you are new to programming and coding earlier in this thread, so please can I recommend a basic level of reading through the materials provided before trying to attempt some of the tasks you are attempting. We are all more than willing to help any newcomers to theming, but sometimes that help needs to be to remind you to start at the beginning - we've all been there, but trying to follow instructions such as Mary's without that background of knowing or recognising some basic php and some common structures for parts of the theme makes it very difficult.

In reply to Richard Oelmann

Re: Essential Theme - Items in Navbar

by Saeed Amiri -

Hi Richard,

Thanks for the hint and the advice.

In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi,

First of all you need to remove all the changes you made to your theme and start again.

This is what you need to do:

  1. ADD the following to your theme's settings.php
    at about this line...
    https://github.com/moodleman/moodle-theme_essential/blob/master/settings.php#L59

    // Theme overrides custom menu setting...
        $name = 'theme_themename/custommenuitems'; //where themename should be your theme's name
        $title = get_string('custommenuitems', 'admin');
        $description = get_string('configcustommenuitems', 'admin');
        $default = '';
        $setting = new admin_setting_configtextarea($name, $title, $description, $default);
        $settings->add($setting);

  2. NEXT add this line to top of frontpage.php where all similar code is added...
    $custommenu = $OUTPUT->custom_menu($PAGE->theme->settings->custommenuitems);
    $hascustommenu = (empty($PAGE->layout_options['nocustommenu']) && !empty($custommenu));

  3. NEXT add the highlighted code (see below) at this point in your frontpage.php...
    https://github.com/moodleman/moodle-theme_essential/blob/master/layout/frontpage.php#L165

        <div class="nav-collapse collapse">

     <?php echo $OUTPUT->custom_menu(); ?>    <- DELETE this

        <?php
     if ($hascustommenu) {
        echo $OUTPUT->custom_menu();                            <- ADD this
     } ?>
     
        <ul class="nav pull-right"> 

    Please note that the code in light grey represents the existing code & the DELETE & ADD are just pointers to try and explain what to do.

  4. Having done all the above. Go to your theme Custom settings page and add the actual menu items that you want to see on the front page.

Hopefully this should get ride of the errors. As Richard pointed out this is really part od Advanced codeing, but since you seem to know what you are doing I had assumed you would first check the files I referred you to, and after looking at the code and the name of the file they were in I would have assumed you would have copied that to your theme files of the same name.

Anyway one learns quicker making mistakes and we are all learners at the end of the day.

It also make it confusing when there are different solutions for the same question. The trick is choosing the one that best fits your needs.

Cheers

Mary

In reply to Mary Evans

Re: Essential Theme - Items in Navbar

by Saeed Amiri -

Hi Mary

Thank you very much for your detailed explanation.

I carefully followed your guidelines. But when I log in and click on "Site administration", a box appears with the title "SyntaxError", inside of which are the following errors.

 

parse@[native code]
ln: 23
/theme/yui_combo.php?rollup/3.13.0/yui-moodlesimple.js:37340
parse
ln: 38
/theme/yui_combo.php?m/1389690763/block_navigation/navigation/navigation-de
ajaxProcessResponse
ln: 40
/theme/yui_combo.php?rollup/3.13.0/yui-moodlesimple.js:26572
_notify
ln: 31
/theme/yui_combo.php?rollup/3.13.0/yui-moodlesimple.js:26601
notify
ln: 23
/theme/yui_combo.php?rollup/3.13.0/yui-moodlesimple.js:26264
_notify
ln: 43
/theme/yui_combo.php?rollup/3.13.0/yui-moodlesimple.js:26390
_procSubs
ln: 27
/theme/yui_combo.php?rollup/3.13.0/yui-moodlesimple.js:26357
fireSimple
ln: 39
/theme/yui_combo.php?rollup/3.13.0/yui-moodlesimple.js:26339
_fire
ln: 27
/theme/yui_combo.php?rollup/3.13.0/yui-moodlesimple.js:27492
fire
ln: 26
/theme/yui_combo.php?rollup/3.13.0/yui-moodlesimple.js:36549
_evt
ln: 18
/theme/yui_combo.php?rollup/3.13.0/yui-moodlesimple.js:36585
complete
http://.../theme/yui_combo.php?rollup/3.13.0/yui-moodlesimple.js:36831:28

Any ideas?

In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Well first of all it depends if you have made some changes to the theme you are working on, or have enabled some admin settings in Site Administration > Appearance/AJAX and Javascript?

That warning is nothing to do with the menu. In fact I have never come across this sort of thing at all.

You could try Purge all cache and see if that clears that warning?

In reply to Mary Evans

Re: Essential Theme - Items in Navbar

by Saeed Amiri -

Hi

You were right. I reinstalled the theme and the error was gone but the persistent error is

Fatal error: Call to a member function add() on a non-object in /Library/WebServer/Documents/connect/theme/essential/settings.php on line 66

with line 66 referring to  $settings->add($setting);

I have uploaded both the "setting.php" and "frontage.php".

Do you have any ideas?

In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Saeed Amiri -

Still getting this error after modifying the setting.php file.

Fatal error: Call to a member function add() on a non-object in ... /settings.php on line 187

with line 187 referring to  $settings->add($setting);

Any ideas?

In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

You are messing up the '$settings' variable somewhere probably with a typo.  Possibly on a '$setting' assignment line.  Please post the 'settings.php' file along with the layout file containing the layout changes Mary suggests.  Also when changing settings you need to update '$plugin->version' in 'version.php' so that the defaults for the setting are placed in the database by the upgrade process.

In reply to Gareth J Barnard

Re: Essential Theme - Items in Navbar

by Saeed Amiri -

Hi Gareth

Thanks  a lot for the reply.

I uninstalled the theme completely, purged all cache, and reinstalled it again. Then I re-applied the changes (copy-pasted them, actually). I also changed the '$plugin->version' in 'version.php' from 2014010600 to 2014011400. Still, I get the following error:

Fatal error: Call to a member function add() on a non-object in .../theme/essential/settings.php on line 66

with line 66 being

$settings->add($setting);

The theme's two file of setting.php and frontage.php are attached with Mary's changes applied in them.

Many Thanks

BTW, I have not touched the other files due to their being newly installed.

In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Oh, really easy to fix, on line 66, change '$settings' to '$temp'.  I forgot that Essential uses multiple settings pages within a bespoke settings category that is added directly to the $ADMIN global and hence on line 26 $settings is set to null and hence the issue.  Changing it to '$temp' will add it to the bespoke 'General' settings page that is instantiated on line 34.

And after that my gut says that:

echo $OUTPUT->custom_menu();

should be:

echo $custommenu;

because of:

$custommenu = $OUTPUT->custom_menu($PAGE->theme->settings->custommenuitems);

But just a hunch.

In reply to Gareth J Barnard

Re: Essential Theme - Items in Navbar

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Thanks for spotting that Gareth! My mistake...that's what comes of rushing and trying to be helpful!

In reply to Gareth J Barnard

Re: Essential Theme - Items in Navbar

by Saeed Amiri -

Thank you both Gareth and Mary. I'll make changes as explained and report back here.

In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Saeed Amiri -

Hi Mary and Gareth

I made the new modifications and now I have a second custom menu in the theme's general settings, but, unfortunately, there's still a problem. There are no errors any more but the items still appear as soon as I log in. They show up only in the home page though. In other places/pages, they do not appear. How can I get rid of them in the home page as well?

I really appreciate your taking the time to solve this problem.

By the way, to make sure I have done every thing correctly, these are the changes I made:

1. added the following to the theme's settings.php

// Theme overrides custom menu setting...
    $name = 'theme_themename/custommenuitems'; //changed themename to my theme's name
    $title = get_string('custommenuitems', 'admin');
    $description = get_string('configcustommenuitems', 'admin');
    $default = '';
    $setting = new admin_setting_configtextarea($name, $title, $description, $default);
    $temp->add($setting);
 
2. added the following line to top of frontpage.php 

$custommenu = $OUTPUT->custom_menu($PAGE->theme->settings->custommenuitems);
$hascustommenu = (empty($PAGE->layout_options['nocustommenu']) && !empty($custommenu));
 
3. Added only the highlighted line to theme's frontage.php
<div class="nav-collapse collapse">

 <?echo $custommenu; ?>  
 
    <ul class="nav pull-right"> 

 

In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hi Saeed,

I've lost the plot with where things are.  Please could you post a print screen of the issue.  The 'home' page should be using the frontpage.php layout file, so that is where the changes will be as the other pages use other layout files.

Gareth

In reply to Gareth J Barnard

Re: Essential Theme - Items in Navbar

by Saeed Amiri -

Hi Gareth

Here are the navabar screen shots for both when I am logged in and in the home page and when logged out. Like I said, this is when I insert the codes in the first css box, that is, the one near the top of the theme's general settings page. As for the second css box, when I enter the codes in it, nothing appears on the navbar at all.

 

 

Attachment logged in.png
In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Saeed Amiri -

And the logged out shot.

Attachment logged out.png
In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

OK...nearly there...

It IS working don't you see?

The two menu boxes are NOT both in the theme the MIAN one is the one you set up in Site Administration > Appearance > Themes > Theme settings where you can enable Theme Designer Mode, and other settings for Course themes and category themes as well as setting for detecting Mobile phone etc...

This is the Menu you will now need to alter.

So this is what you need to do...

Go To...

  1. Site Administration > Appearance > Themes > Theme settings and ADD the menu that you want to see in all course pages AFTER login
  2. Site Administration > Appearance > Themes > Essential > General settings and ADD the menu that you want people to see BEFORE login ON on the Front page

Since the changes are not finished yet for the Frontpage, although the menus are separate as explained above.

Cheers

Mary

In reply to Mary Evans

Re: Essential Theme - Items in Navbar

by Saeed Amiri -

Ok. I got it. I mixed up "Custom CSS" with "Custom menu items" in theme's general settings. That was my mistake. But still, when I enter the codes in Custom menu items in theme's general settings in the path Administration > Appearance > Themes > Essential > General settings, I get the same result. The items appear both before and after the login.

In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Nigel Irwin -

Mary,

 

Dosent he need to insert if(!isloggedin())  then the <?php echo $custommenu; ?>. Sorry I cant remember the exact syntact. Then the custom menu would onluy appear when not logged in which is what Saeed is asking for.

In reply to Nigel Irwin

Re: Essential Theme - Items in Navbar

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Yes that is part of the fix...but its more complicated because of the two different menus.

We are dealing with this however, thanks for your concern.

In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi Saeed,

Almost there...

 <?php echo $custommenu; ?>

is what you should have. This then makes the 2nd menu work in the frontpage so you need to add only the items into the General settings page menu that you want on the Frontpage only!!!

if you do not want any at all, then all this has been for nothing, as I am getting the feeling that you only wanted Login and nothing else...LOL

confused!

Mary

In reply to Mary Evans

Re: Essential Theme - Items in Navbar

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Just so as not to lose the plot I am working on a solution...which should work and may be a novel idea for other themes which Gareth may want to streamline as he is better with PHP than I am.  duh!

Average of ratings: Useful (2)
In reply to Mary Evans

Re: Essential Theme - Items in Navbar

by Saeed Amiri -

Hi Mary,

 <?php echo $custommenu; ?> is the line I had there, but to no avail! sad

In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Saeed Amiri -

Hi

Maybe I've been putting the codes in the wrong css box. Do I have to put them in the box towards the top or the one down the page? If the latter, nothing appears on the navbar, either before the login or after it. And as for the former, the items appear both before the login and after it in the homepage.

 

 

In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Nigel Irwin -

I believe what Saeed required  is for the custommenu items only to appear before people log in and not appear after they login.

In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi Saeed,

Can you email me a copy of the theme you are working on? That way I can add the changes, and test them and then send it back to you?

Cheers

Oh and sorry for the confusion!

Mary

In reply to Mary Evans

Re: Essential Theme - Items in Navbar

by Saeed Amiri -

Hi Mary

That would be really kind of you. I just wonder where I might have made a mistake in my modifications. They were mainly copy-pasting.

Many thanks

In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Sorry all, I'm up to my **** in stuff to do.  Will look at when I can.

In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Saeed there is only ONE menu setting in the theme the other is in Moodle.

Please my previous comment here

https://moodle.org/mod/forum/discuss.php?d=251846#p1093385

In reply to Mary Evans

Re: Essential Theme - Items in Navbar

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

For anyone watching this thread MDL-31043 is how the theme based menu came to be.

This works great if you add the normal menu to your frontpage.php and the theme based one to general.php.

However, trying to make this work as we want it to on one page is proving very difficult.

I think I have fixed this with Gareth's help, to whom I am greatly indebted.

@Saeed,

I will email you the changes.

Thanks for your patience

Mary

In reply to Mary Evans

Re: Essential Theme - Items in Navbar

by Saeed Amiri -
Hi Mary and Gareth Thank you very much for solving that issue. The menus now only appear on the frontage. In my mind, it can be a worthy update to be included in the later versions of the theme. Some menus need not be shown in the entire site and with this change one can hide them after logging in. BTW, moving the menu box to the theme's custom menu was also a great idea. Many thanks!
In reply to Saeed Amiri

Re: Essential Theme - Items in Navbar

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

You are very welcome Saeed. So pleased it works OK.

As you will be using this theme in RTL Language you may experience some display issues which will need reporting in Moodle Tracker. If this is the case ask on this forum first than we can check it out.

Cheers

Mary

In reply to Mary Evans

Re: Essential Theme - Items in Navbar

by Saeed Amiri -

Hi Mary

You are right. I have already encountered some issues in RTL mode and will post them here in this forum.

I really appreciate every one's support and efforts.

Regards

Saeed