How do I get a mod to call its own admin template and languages file?

Started by Antechinus, September 04, 2021, 07:10:03 AM

Previous topic - Next topic

Antechinus

Which seems like a clear enough question. :)

I don't want to just rely on ManageSettings.php, because it doesn't allow enough otions for organising the output. Or at least, not as far as I know.

It'd be better if I can write my own template for action=admin;area=modsettings;sa=ants_latest_disaster, and while I'm at it I can't help thinking just shoving all the text strings into Modifications.english-utf8.php may not be the best option either.

So, something in Sources to hold all the $modSettings stuffz (216 inputs and associated labels). A template in Themes/default to make the settings digestible for humans.

A small languages file too, and a CSS file (which I imagine just gets dropped into default/css and sorts itself out).

ETA: Yes, have searched this board, and looked at docs. Still stumped.

d3vcho

I'm noob in SMF development, but I think the functions you're looking for are:

loadTemplate
loadCSSFile
loadLanguage

Probable someone more knowledgeable than me can give some proper guidance.
"Greeting Death as an old friend, they departed this life as equals"

Arantor

Correct, those are exactly the functions you want.

loadTemplate('Xyz') -> looks for Xyz.template.php in the current theme, then in a parent theme if the current theme is listed as having a parent theme, then in the default theme, then dies with a fatal error if not found.

loadLanguage('Xyz') -> looks for Xyz.language.php in the following order:
1. user current language in the current theme
2. site default language in the current theme
3. user current language in the parent theme if the theme is defined as having a parent
4. site default language in the parent theme if the theme is defined as having a parent
5. user current language in the default theme
6. site default language in the default theme

Lastly if the user and site languages are not English:
7. English in the current theme
8. English in the default theme


loadCSSFile is a bit trickier because it's multi-use. Mostly this comes down to whether you're loading something solely for the current theme or something for every theme that can be expected to be there, plus whether it's already minified or to be bundled in the current attempt for minification.

Probably one of the more instructive examples:
loadCSSFile('responsive.css', array('force_current' => false, 'validate' => true, 'minimize' => true, 'order_pos' => 9000), 'smf_responsive');
First param is the CSS file name, second is a bunch of options, third is a reference name for the CSS file.

In this case: load responsive.css, force_current = false -> allow loading it from current theme if it has one, otherwise load from default; validate -> check it exists, don't just assume it does (not all themes will have one); minimize -> this should be included in minification; order_pos -> a number indicating where in the running order you should do it, lower = earlier (index.css is 1, default is 3000)

Other options of note:
external - if true, the stylesheet to load is not relative to the theme path but a full URL
seed - a possible override for the usual ? parameter used for cache busting

Antechinus

Ok, thanks. I can probably sort it out from there.

CSS would have to be done just for default. Trying to match the CSS of every custom theme with a mod would drive anyone insane. I can probably do it with just layout CSS and no colours anyway.

Not worth minifying in advance, as it would only be a tiny file. Better to bundle it with everything else and let SMF handle minification. Then it's easier for people to mess with if they need to.

Sesquipedalian

You can use this as a guide. 

Obviously, the functions that say "Called by: integrate_*" in their documentation are meant to be called via integration hooks.

<?php

/**
 * Gives the admin a way to control settings.
 * 
 * This is the part that shows the settings, save changes, etc.
 *
 * Called by: ModifyModSettings
 */
function examplemod_settings($return_config false)
{
// Go away, hackers
isAllowedTo('admin_forum');

loadLanguage('ExampleMod');

$config_vars = array(
array('check''examplemod_enabled''help' => 'examplemod_enabled_help'),
);

// Toss a bone to the admin search
if ($return_config)
return $config_vars;

// Template stuff
$context['sub_template'] = 'show_settings';
$context['page_title'] = $txt['examplemod_admin_title'];
$context['settings_title'] = $txt['examplemod_admin_title'];
$context['post_url'] = $scripturl '?action=admin;area=modsettings;save;sa=examplemod';

// Saving?
if (isset($_REQUEST['save']))
{
checkSession();

saveDBSettings($config_vars);

$_SESSION['adm-save'] = true;
writeLog();

redirectexit('action=admin;area=modsettings;sa=examplemod');
}

prepareDBSettingContext($config_vars);
}

/**
 * Adds a subaction to admin_modifications for ExampleMod.
 * 
 * This is what adds examplemod_settings() to the list of functions that may be
 * called from ModifyModSettings().
 *
 * Called by: integrate_modify_modifications
 */
function examplemod_modify_modifications(&$subActions)
{
$subActions['examplemod'] = 'examplemod_settings';
}

/**
 * Adds ExampleMod to the admin menu's list of areas.
 * 
 * This is what creates a menu item for the admin to click on.
 *
 * Called by: integrate_admin_areas
 */
function examplemod_admin_areas(&$menuData)
{
global $txt;

loadLanguage('ExampleMod');

$menuData['config']['areas']['modsettings']['subsections']['examplemod'] = array($txt['examplemod_admin_title']);
}

/**
 * Adds ExampleMod info to the admin search.
 *
 * Called by: integrate_admin_search
 */
function examplemod_admin_search(&$language_files, &$include_files, &$settings_search)
{
$language_files[] = 'ExampleMod';
$include_files[] = 'ExampleMod';
$settings_search[] = array('examplemod_settings''area=modsettings;sa=examplemod');
}
I promise you nothing.

Sesqu... Sesqui... what?
Sesquipedalian, the best word in the English language.


Advertisement: