diff --git a/doc/Addons.md b/doc/Addons.md index 708a2e0f04..cf4fbdab12 100644 --- a/doc/Addons.md +++ b/doc/Addons.md @@ -242,7 +242,57 @@ Called when the Settings pages are submitted. ### addon_settings Called when generating the HTML for the addon settings page. -`$b` is the (string) HTML of the addon settings page before the final `` tag. +`$data` is an array containing: + +- **addon** (output): Required. The addon folder name. +- **title** (output): Required. The addon settings panel title. +- **href** (output): Optional. If set, will reduce the panel to a link pointing to this URL, can be relative. Incompatible with the following keys. +- **html** (output): Optional. Raw HTML of the addon form elements. Both the `
` tags and the submit buttons are taken care of elsewhere. +- **submit** (output): Optional. If unset, a default submit button with `name="-submit"` will be generated. + Can take different value types: + - **string**: The label to replace the default one. + - **associative array**: A list of submit button, the key is the value of the `name` attribute, the value is the displayed label. + The first submit button in this list is considered the main one and themes might emphasize its display. + +#### Examples + +##### With link +```php +$data = [ + 'addon' => 'advancedcontentfilter', + 'title' => DI::l10n()->t('Advanced Content Filter'), + 'href' => 'advancedcontentfilter', +]; +``` +##### With default submit button +```php +$data = [ + 'addon' => 'fromapp', + 'title' => DI::l10n()->t('FromApp Settings'), + 'html' => $html, +]; +``` +##### With no HTML, just a submit button +```php +$data = [ + 'addon' => 'opmlexport', + 'title' => DI::l10n()->t('OPML Export'), + 'submit' => DI::l10n()->t('Export RSS/Atom contacts'), +]; +``` +##### With multiple submit buttons +```php +$data = [ + 'addon' => 'catavar', + 'title' => DI::l10n()->t('Cat Avatar Settings'), + 'html' => $html, + 'submit' => [ + 'catavatar-usecat' => DI::l10n()->t('Use Cat as Avatar'), + 'catavatar-morecat' => DI::l10n()->t('Another random Cat!'), + 'catavatar-emailcat' => DI::pConfig()->get(local_user(), 'catavatar', 'seed', false) ? DI::l10n()->t('Reset to email Cat') : null, + ], +]; +``` ### addon_settings_post Called when the Addon Settings pages are submitted. diff --git a/mod/settings.php b/mod/settings.php index 1643b19fc3..54484d8a90 100644 --- a/mod/settings.php +++ b/mod/settings.php @@ -62,9 +62,10 @@ function settings_post(App $a) } if ((DI::args()->getArgc() > 1) && (DI::args()->getArgv()[1] == 'addon')) { - BaseModule::checkFormSecurityTokenRedirectOnError('/settings/addon', 'settings_addon'); + BaseModule::checkFormSecurityTokenRedirectOnError(DI::args()->getQueryString(), 'settings_addon'); Hook::callAll('addon_settings_post', $_POST); + DI::baseUrl()->redirect(DI::args()->getQueryString()); return; } @@ -438,11 +439,27 @@ function settings_content(App $a) if ((DI::args()->getArgc() > 1) && (DI::args()->getArgv()[1] === 'addon')) { $addon_settings_forms = []; - foreach (DI::dba()->selectToArray('hook', ['file', 'function'], ['hook' => 'addon_settings']) as $hook) { - $data = ''; + $data = []; Hook::callSingle(DI::app(), 'addon_settings', [$hook['file'], $hook['function']], $data); - $addon_settings_forms[] = $data; + + if (!empty($data['href'])) { + $tpl = Renderer::getMarkupTemplate('settings/addon/link.tpl'); + $addon_settings_forms[] = Renderer::replaceMacros($tpl, [ + '$addon' => $data['addon'], + '$title' => $data['title'], + '$href' => $data['href'], + ]); + } elseif(!empty($data['addon'])) { + $tpl = Renderer::getMarkupTemplate('settings/addon/panel.tpl'); + $addon_settings_forms[$data['addon']] = Renderer::replaceMacros($tpl, [ + '$addon' => $data['addon'], + '$title' => $data['title'], + '$open' => (DI::args()->getArgv()[2] ?? '') === $data['addon'], + '$html' => $data['html'] ?? '', + '$submit' => $data['submit'] ?? DI::l10n()->t('Save Settings'), + ]); + } } $tpl = Renderer::getMarkupTemplate('settings/addons.tpl'); diff --git a/view/templates/settings/addon/link.tpl b/view/templates/settings/addon/link.tpl new file mode 100644 index 0000000000..4fb8178ccf --- /dev/null +++ b/view/templates/settings/addon/link.tpl @@ -0,0 +1 @@ +

{{$title}}

\ No newline at end of file diff --git a/view/templates/settings/addon/panel.tpl b/view/templates/settings/addon/panel.tpl new file mode 100644 index 0000000000..f401f01231 --- /dev/null +++ b/view/templates/settings/addon/panel.tpl @@ -0,0 +1,33 @@ + +

{{$title}}

+
+
+ +

{{$title}}

+
+ {{$html nofilter}} +{{if $submit}} + +{{/if}} +
diff --git a/view/templates/settings/addons.tpl b/view/templates/settings/addons.tpl index e30732d17a..03313c9244 100644 --- a/view/templates/settings/addons.tpl +++ b/view/templates/settings/addons.tpl @@ -1,8 +1,8 @@

{{$title}}

-{{foreach $addon_settings_forms as $addon_settings_form}} +{{foreach $addon_settings_forms as $addon => $addon_settings_form}} - + {{$addon_settings_form nofilter}} diff --git a/view/theme/frio/templates/settings/addon/link.tpl b/view/theme/frio/templates/settings/addon/link.tpl new file mode 100644 index 0000000000..bf430db46b --- /dev/null +++ b/view/theme/frio/templates/settings/addon/link.tpl @@ -0,0 +1 @@ + diff --git a/view/theme/frio/templates/settings/addon/panel.tpl b/view/theme/frio/templates/settings/addon/panel.tpl new file mode 100644 index 0000000000..561d0fc9f3 --- /dev/null +++ b/view/theme/frio/templates/settings/addon/panel.tpl @@ -0,0 +1,36 @@ + +
+
+ {{$html nofilter}} +
+ +
diff --git a/view/theme/frio/templates/settings/addons.tpl b/view/theme/frio/templates/settings/addons.tpl index f6e2d61086..f150303eaf 100644 --- a/view/theme/frio/templates/settings/addons.tpl +++ b/view/theme/frio/templates/settings/addons.tpl @@ -2,17 +2,15 @@ {{* include the title template for the settings title *}} {{include file="section_title.tpl" title=$title}} -{{foreach $addon_settings_forms as $addon_settings_form}} - -
- - {{$addon_settings_form nofilter}} -
- +
+{{foreach $addon_settings_forms as $addon => $addon_settings_form}} +
+ + {{$addon_settings_form nofilter}} +
{{foreachelse}} - - - + {{/foreach}} +
\ No newline at end of file