2 Settings Pages?!? Moodle 2.7.2

2 Settings Pages?!? Moodle 2.7.2

by Mike Brinkman -
Number of replies: 22

I followed the theme settings page tutorial all the way up to this section. After I finished creating the settings page, and refreshed my page, I now have two settings pages for my theme. One has all of the info on it, and the other one is blank.

Compounding the mystery is that when I changed the logo code from:

// Logo file setting
$name = 'theme_albion/logo';
$title = get_string('logo','theme_albion');
$description = get_string('logodesc', 'theme_albion');
$setting = new admin_setting_configtext($name, $title, $description, '', PARAM_URL);
$temp->add($setting);

to this:

// Logo file setting
$name = 'theme_albion/logo';
$title = get_string('logo','theme_albion');
$description = get_string('logodesc', 'theme_albion');
$setting = new admin_setting_configstoredfile($name, $title, $description, 'logo');
$setting->set_updatedcallback('theme_reset_all_caches');
$settings->add($setting);

The stored file box ends up on the second theme page that was previously empty! Has anyone seen this type of behavior before when creating a settings page?

Average of ratings: -
In reply to Mike Brinkman

Re: 2 Settings Pages?!? Moodle 2.7.2

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

You have a coding error:

$temp->add($setting);

changed to:

$settings->add($setting);

And if you are creating separate sub-pages for the theme then you need to remove the root page in settings.php with:

$settings = null;
Average of ratings: Useful (2)
In reply to Gareth J Barnard

Re: 2 Settings Pages?!? Moodle 2.7.2

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

Just to add to what Gareth wrote - you need to be consistent through the whole file - not just in the code you have posted for the logo, but right the way through. You can use either $temp or $settings but it needs to be the same right through the file.

I had the same error as you a while back and that was the cause (I'd been careless copying and pasting from two of my other themes which were different! One used $temp and the other $settings)

Average of ratings: Useful (1)
In reply to Gareth J Barnard

Re: 2 Settings Pages?!? Moodle 2.7.2

by Mike Brinkman -

Ye,s that makes sense now. I actually copied the code for the configstoredfile from another theme, because I liked it better than the text box. However, there's still something I don't understand. Without the $settings = null; statement, I get 2 settings pages listed under Themes.

I made a copy of my settings.php page as settings2.php, then deleted settings.php. Now when I look under Themes, there is nothing listed. So why is it when I'm assigning values to $settings instead of setting $settings to null, that I get 2 pages? This forces me to rename all of my $settings to $temp. I don't see any other themes that are doing that when I've looked through the code.

In reply to Mike Brinkman

Re: 2 Settings Pages?!? Moodle 2.7.2

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

Ok:

  1. settings.php is not connected to $settings.
  2. Deleting settings.php will cause you to have no settings because the core code looks for settings.php.
  3. By default when settings.php is loaded there is already an object contained in the $settings variable being an instance of 'admin_settingpage'.  So adding stuff to it will add stuff on the default page.
  4. Setting $settings to null deletes the default.
  5. Using $temp is useful when you have more than one sub-setting page (like Essential and Shoehorn themes).  The variable is reused over and over again.  It is added to the system with '$ADMIN->add'.
  6. If you have only one page, stick with $settings, if more then use your own variable (which could be $temp or $crazyharry or $statler or $waldorf etc...) and add each new 'admin_settingpage' instance with '$ADMIN->add' and nullify $settings.

Average of ratings: Useful (2)
In reply to Gareth J Barnard

Re: 2 Settings Pages?!? Moodle 2.7.2

by Mike Brinkman -
By default when settings.php is loaded there is already an object contained in the $settings variable being an instance of 'admin_settingpage'. So adding stuff to it will add stuff on the default page.

Yes, I think I understand that. The problem is that when my variable was called $settings and I was adding things to it, I was still getting two pages, one with information, and the other one blank. That's my source of frustration.

In reply to Mike Brinkman

Re: 2 Settings Pages?!? Moodle 2.7.2

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

That's because you were using another variable $temp and adding that with '$ADMIN->add' at the same time within the settings.php file.

Average of ratings: Useful (1)
In reply to Gareth J Barnard

Re: 2 Settings Pages?!? Moodle 2.7.2

by Mike Brinkman -
That's because you were using another variable $temp and adding that with '$ADMIN->add' at the same time within the settings.php file.

After I saw your original post, I went through the settings.php file and replaced all instances of $temp with $settings. The problem still persisted.

In reply to Mike Brinkman

Re: 2 Settings Pages?!? Moodle 2.7.2

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

Ok, I no longer know what you have done wrong.  Please post the problem file.

Average of ratings: Useful (1)
In reply to Gareth J Barnard

Re: 2 Settings Pages?!? Moodle 2.7.2

by Mike Brinkman -

Thanks, here it is. It's probably just a really dumb error on my part, but I'm just getting started. I'm sure there will be many more dumb errors in the future.

// Create our admin page
$settings = new admin_settingpage('theme_test', get_string('configtitle','theme_test'));
 
// Background colour setting
$name = 'theme_test/backgroundcolor';
$title = get_string('backgroundcolor','theme_test');
$description = get_string('backgroundcolordesc', 'theme_test');
$default = '#DDD';
$setting = new admin_setting_configtext($name, $title, $description, $default, PARAM_CLEAN, 12);
$settings->add($setting);
 
// Logo file setting
$name = 'theme_test/logo';
$title = get_string('logo','theme_test');
$description = get_string('logodesc', 'theme_test');
$setting = new admin_setting_configtext($name, $title, $description, '', PARAM_URL);
$settings->add($setting);
 
// Block region width
$name = 'theme_test/regionwidth';
$title = get_string('regionwidth','theme_test');
$description = get_string('regionwidthdesc', 'theme_test');
$default = 200;
$choices = array(150=>'150px', 170=>'170px', 200=>'200px', 240=>'240px', 290=>'290px', 350=>'350px', 420=>'420px');
$setting = new admin_setting_configselect($name, $title, $description, $default, $choices);
$settings->add($setting);
 
// Foot note setting
$name = 'theme_test/footnote';
$title = get_string('footnote','theme_test');
$description = get_string('footnotedesc', 'theme_test');
$setting = new admin_setting_confightmleditor($name, $title, $description, '');
$settings->add($setting);
 
// Custom CSS file
$name = 'theme_test/customcss';
$title = get_string('customcss','theme_test');
$description = get_string('customcssdesc', 'theme_test');
$setting = new admin_setting_configtextarea($name, $title, $description, '');
$settings->add($setting);
 
// Add our page to the structure of the admin tree
$ADMIN->add('themes', $settings);
In reply to Mike Brinkman

Re: 2 Settings Pages?!? Moodle 2.7.2

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

I need to check this out but it looks like the old way we used to write settings pages...which does list them in duplicate.

just wondering where you copied this settings page from?

 

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

Re: 2 Settings Pages?!? Moodle 2.7.2

by Mike Brinkman -

Hi Mary, it was taken straight from the Moodle Theme Settings Page tutorial, here. I really appreciate all the help everyone has given me so far!

In reply to Mike Brinkman

Re: 2 Settings Pages?!? Moodle 2.7.2

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

Hi Mike, 

I have just updated that Settings page Tutorial

I hope someone can check it for errors!

cheers

Mary

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

Re: 2 Settings Pages?!? Moodle 2.7.2

by Mike Brinkman -

Thanks Mary!

I'll be sure to check it out & update my settings page. I'm usually pretty good at discovering mistakes because I make a lot of them myself, so I'll leave you feedback if there's anything else I run into. By the way, you might want to update the Creating a theme tutorial as well, as it contains a couple of errors that threw me for a loop. The first one is in the Configuring our theme section:

    // The site home page.
    'frontpage' => array(
        'file' => 'frontpage.php',

All of the other pages use general.php. The second one is in the in the page content section:

In regards to PHP this section is very easy. There are only three lines for the whole section one to get the main content and one for each block region.
<?php echo core_renderer::MAIN_CONTENT_TOKEN ?>
This line prints the main content for the page. PLEASE NOTE: In Moodle 2.2 onwards "core_renderer::MAIN_CONTENT_TOKEN" changed to "$OUTPUT->main_content()".

I missed that the first time through, so it might be better to change it to have the proper method post Moodle 2.1 in the code, with a note about how to do it pre-Moodle 2.2 instead. Just my $.02.

In reply to Mike Brinkman

Re: 2 Settings Pages?!? Moodle 2.7.2

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

The reason there is a 'frontpage.php' is because it is actually different from the general.php. It does not have the breadcrumb for a start, also it is easier to customise when you want to keep the Frontpage different than the rest of the site.

Thanks for the heads up on that tutorial.

cheers

Mary

In reply to Mary Evans

Re: 2 Settings Pages?!? Moodle 2.7.2

by Mike Brinkman -

While that may be true in general, the tutorial specifically only creates one layout file:

The excitement theme has just one layout file.

The downside of this is that I have to make the layout file do everything I want which means I need to make use of some options (as defined in the layouts in config.php).
In reply to Mike Brinkman

Re: 2 Settings Pages?!? Moodle 2.7.2

by Danny Wahl -

you totally can make a theme with just one layout file- or with 20 layout files if you like.  the ability to specify which layout for which pagetype just makes it easier than having to try to sort it out with fancy login within the layout file itself.  My old 'zebra' theme had only 2 layout files 'general' and 'popup' and it worked just fine.

Average of ratings: Useful (1)
In reply to Danny Wahl

Re: 2 Settings Pages?!? Moodle 2.7.2

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

Following on from what Danny says, whatever the name of your layout files the important thing to remember is that the the have to contain the elements declared in the list in the theme's config file. For example, if only one block region I'd needed, then that block region needs to be in the layout file, otherwise it will throw an error that will break the theme.

Average of ratings: Useful (1)
In reply to Mike Brinkman

Re: 2 Settings Pages?!? Moodle 2.7.2

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

The reason there is a 'frontpage.php' is because it is actually different from the general.php. It does not have the breadcrumb for a start, also it is easier to customise when you want to keep the Frontpage different than the rest of the site.

Thanks for the heads up on that tutorial.

cheers

Mary

In reply to Mike Brinkman

Re: 2 Settings Pages?!? Moodle 2.7.2

by Danny Wahl -

you don't need to create a page or add it to the admin tree (the first and last lines respectively).  that's the source of the duplicate. the SETTINGS object will automatically create one for you when you start creating theme_settings, then you create one with the same name.

In reply to Danny Wahl

Re: 2 Settings Pages?!? Moodle 2.7.2

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

Hi Danny, 

The settings page tutorial was wrong. It was the same for us all at the beginning in 2010 when Sam Hemelryk wrote that tutorial. Do you not remember the duplicated theme name?

anyway I have updated the tutorial. I would be happy if you could glance at it and see if I missed off something, as I did it all on me iPad!

thanks

Mary

Average of ratings: Useful (1)
In reply to Mike Brinkman

Re: 2 Settings Pages?!? Moodle 2.7.2

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

Hi Mike,

I'm going to take a bit of a wild stab in the dark over this one, but I think it is because of this line:

$settings = new admin_settingpage('theme_test', get_string('configtitle','theme_test'));

When it is reached '$settings' already contains a pointer / reference to a memory location (a guess) containing an instance of 'admin_settingpage', which has already been added to the list of settings pages with something similar to:

$ADMIN->add('themes', $settings);

Now, because in computer languages the nature of the assignment operator (the '=') can vary according to the type, then I suspect that when a new instance is created with 'new' that the '$settings' variable gets a new pointer / reference value.  But when assigned 'null' with '$settings = null;', then what the existing pointer / reference actually refers to is set to 'null' effectively causing its destruction.  So, as $ADMIN already has another pointer / reference to the core created 'admin_settingpage' instance then that is deleted when '$settings' is set to 'null', but kept when set to a new instance of 'admin_settingpage'.  Therefore when you:

$ADMIN->add('themes', $settings);

without:

$settings = null;

Then you get two settings pages.  The original core generated one and the one you created.

Cheers,

Gareth

Average of ratings: Useful (1)