Settings

An app can register both admin settings as well as personal settings. Settings are divided into sections to group similar settings together. For example, in the Sharing section are only settings (built-in and of apps) related to sharing.

Settings form

For the settings to show up, three things are necessary:

  1. A class implementing \OCP\Settings\ISettings
  2. A template
  3. The implementing class specified in the app’s info.xml

Below is an example for an implementor of the ISettings interface. It is based on the survey_client solution.

  1. <?php
  2. namespace OCA\YourAppNamespace\Settings;
  3. use OCA\YourAppNamespace\Collector;
  4. use OCP\AppFramework\Http\TemplateResponse;
  5. use OCP\BackgroundJob\IJobList;
  6. use OCP\IConfig;
  7. use OCP\IDateTimeFormatter;
  8. use OCP\IL10N;
  9. use OCP\Settings\ISettings;
  10. class AdminSettings implements ISettings {
  11. /** @var Collector */
  12. private $collector;
  13. /** @var IConfig */
  14. private $config;
  15. /** @var IL10N */
  16. private $l;
  17. /** @var IDateTimeFormatter */
  18. private $dateTimeFormatter;
  19. /** @var IJobList */
  20. private $jobList;
  21. /**
  22. * Admin constructor.
  23. *
  24. * @param Collector $collector
  25. * @param IConfig $config
  26. * @param IL10N $l
  27. * @param IDateTimeFormatter $dateTimeFormatter
  28. * @param IJobList $jobList
  29. */
  30. public function __construct(Collector $collector,
  31. IConfig $config,
  32. IL10N $l,
  33. IDateTimeFormatter $dateTimeFormatter,
  34. IJobList $jobList
  35. ) {
  36. $this->collector = $collector;
  37. $this->config = $config;
  38. $this->l = $l;
  39. $this->dateTimeFormatter = $dateTimeFormatter;
  40. $this->jobList = $jobList;
  41. }
  42. /**
  43. * @return TemplateResponse
  44. */
  45. public function getForm() {
  46. $lastSentReportTime = (int) $this->config->getAppValue('survey_client', 'last_sent', 0);
  47. if ($lastSentReportTime === 0) {
  48. $lastSentReportDate = $this->l->t('Never');
  49. } else {
  50. $lastSentReportDate = $this->dateTimeFormatter->formatDate($lastSentReportTime);
  51. }
  52. $lastReport = $this->config->getAppValue('survey_client', 'last_report', '');
  53. if ($lastReport !== '') {
  54. $lastReport = json_encode(json_decode($lastReport, true), JSON_PRETTY_PRINT);
  55. }
  56. $parameters = [
  57. 'is_enabled' => $this->jobList->has('OCA\Survey_Client\BackgroundJobs\MonthlyReport', null),
  58. 'last_sent' => $lastSentReportDate,
  59. 'last_report' => $lastReport,
  60. 'categories' => $this->collector->getCategories()
  61. ];
  62. return new TemplateResponse('yourappid', 'admin', $parameters);
  63. }
  64. /**
  65. * @return string the section ID, e.g. 'sharing'
  66. */
  67. public function getSection() {
  68. return 'survey_client';
  69. }
  70. /**
  71. * @return int whether the form should be rather on the top or bottom of
  72. * the admin section. The forms are arranged in ascending order of the
  73. * priority values. It is required to return a value between 0 and 100.
  74. */
  75. public function getPriority() {
  76. return 50;
  77. }
  78. }

The parameters of the constructor will be resolved and an instance created automatically on demand, so that the developer does not need to take care of it.

getSection is supposed to return the section ID of the desired admin section. Currently, built-in values are server, sharing, encryption, logging, additional and tips-tricks. Apps can register sections of their own (see below), and also register into sections of other apps.

getPriority is used to order forms within a section. The lower the value, the more on top it will appear, and vice versa. The result depends on the priorities of other settings.

Nextcloud will look for the templates in a template folder located in your apps root directory. It should always end on .php, in this case templates/admin.php would be the final relative path.

  1. <?php
  2. /** @var $l \OCP\IL10N */
  3. /** @var $_ array */
  4. script('myappid', 'admin'); // adds a JavaScript file
  5. style('survey_client', 'admin'); // adds a CSS file
  6. ?>
  7. <div id="survey_client" class="section">
  8. <h2><?php p($l->t('Your app')); ?></h2>
  9. <p>
  10. <?php p($l->t('Only administrators are allowed to click the red button')); ?>
  11. </p>
  12. <button><?php p($l->t('Click red button')); ?></button>
  13. <p>
  14. <input id="your_app_magic" name="your_app_magic"
  15. type="checkbox" class="checkbox" value="1" <?php if ($_['is_enabled']): ?> checked="checked"<?php endif; ?> />
  16. <label for="your_app_magic"><?php p($l->t('Do some magic')); ?></label>
  17. </p>
  18. <h3><?php p($l->t('Things to define')); ?></h3>
  19. <?php
  20. foreach ($_['categories'] as $category => $data) {
  21. ?>
  22. <p>
  23. <input id="your_app_<?php p($category); ?>" name="your_app_<?php p($category); ?>"
  24. type="checkbox" class="checkbox your_app_category" value="1" <?php if ($data['enabled']): ?> checked="checked"<?php endif; ?> />
  25. <label for="your_app_<?php p($category); ?>"><?php print_unescaped($data['displayName']); ?></label>
  26. </p>
  27. <?php
  28. }
  29. ?>
  30. <?php if (!empty($_['last_report'])): ?>
  31. <h3><?php p($l->t('Last report')); ?></h3>
  32. <p><textarea title="<?php p($l->t('Last report')); ?>" class="last_report" readonly="readonly"><?php p($_['last_report']);?></textarea></p>
  33. <em class="last_sent"><?php p($l->t('Sent on: %s', [$_['last_sent']])); ?></em>
  34. <?php endif; ?>
  35. </div>

Then, the implementing class should be added to the info.xml. Settings will be registered upon install and update. When settings are added to an existing, installed and enabled app, it should be made sure that the version is increased so Nextcloud can register the class. It is only possible to register one ISettings implementor.

For a more complex example using embedded templates have a look at the implementation of the user_ldap app.

Section

It is also possible that an app registers its own section. This should be done only if there is not fitting corresponding section and the apps settings form takes a lot of screen estate. Otherwise, register to “additional”.

Basically, it works the same way as with the settings form. There are only two differences. First, the interface that must be implemented is \OCP\Settings\IIconSection.

An example implementation of the IIconSection interface:

  1. <?php
  2. namespace OCA\YourAppNamespace\Settings;
  3. use OCP\IL10N;
  4. use OCP\IURLGenerator;
  5. use OCP\Settings\IIconSection;
  6. class AdminSection implements IIconSection {
  7. /** @var IL10N */
  8. private $l;
  9. /** @var IURLGenerator */
  10. private $urlGenerator;
  11. public function __construct(IL10N $l, IURLGenerator $urlGenerator) {
  12. $this->l = $l;
  13. $this->urlGenerator = $urlGenerator;
  14. }
  15. /**
  16. * returns the ID of the section. It is supposed to be a lower case string
  17. *
  18. * @returns string
  19. */
  20. public function getID() {
  21. return 'yourappid'; //or a generic id if feasible
  22. }
  23. /**
  24. * returns the translated name as it should be displayed, e.g. 'LDAP / AD
  25. * integration'. Use the L10N service to translate it.
  26. *
  27. * @return string
  28. */
  29. public function getName() {
  30. return $this->l->t('Translatable Section Name');
  31. }
  32. /**
  33. * @return int whether the form should be rather on the top or bottom of
  34. * the settings navigation. The sections are arranged in ascending order of
  35. * the priority values. It is required to return a value between 0 and 99.
  36. */
  37. public function getPriority() {
  38. return 80;
  39. }
  40. /**
  41. * @return The relative path to a an icon describing the section
  42. */
  43. public function getIcon() {
  44. return $this->urlGenerator->imagePath('yourapp', 'icon.svg');
  45. }
  46. }

Also the section must be registered in the app’s info.xml.

Registering Settings and Sections

As mentioned already both Settings and Sections should be registered in the info.xml of your app This is rather straight forward as you can see in the code snipplet below

  1. ...
  2. <settings>
  3. <admin>OCA\YourAppNamespace\Settings\Admin</admin>
  4. <admin-section>OCA\YourAppNamespace\Settings\AdminSection</admin-section>
  5. <personal>OCA\YourAppNamespace\Settings\Personal</personal>
  6. <personal-section>OCA\YourAppNamespace\Settings\PersonalSection</personal-section>
  7. </settings>
  8. ...