Update forum settings when developing add-ons

PHP:
$editorToolbarConfig = $this->app->options()->editorToolbarConfig;
        $optionValue = [
            'toolbarButtons'   => null,
            'toolbarButtonsMD' => null,
            'toolbarButtonsSM' => null,
            'toolbarButtonsXS' => null
        ];

        foreach ($editorToolbarConfig AS $type => $toolbar)
        {
            $toolbar = implode('|', $toolbar);
            $toolbar = preg_replace("/(xfInsert)/", '$0|-vs|mh_hide ', $toolbar);
            $toolbar = explode('|', $toolbar);
            $optionValue[$type] = $toolbar;
        }

        /** @var Option $optionRepo */
        $optionRepo = \XF::repository('XF:Option');
        $optionRepo->updateOption('editorToolbarConfig', $optionValue);

        \XF::repository('XF:Editor')->rebuildEditorDropdownCache();
Thus, we added our button to the editor and updated our editorToolbarConfig setting
Thus, this function works as follows as arguments:
  • Setting name
  • Value
In addition to a specific option, you can update a specific group of settings by calling the updateOptions function
A simple example of updating settings:
PHP:
$options = \XF\Util\Arr::arrayFilterKeys(
            $this->filter('options', 'array'),
            ['boardTitle', 'boardUrl', 'contactEmailAddress', 'homePageUrl', 'collectServerStats'],
            true
        );
        if (!empty($options['contactEmailAddress']))
        {
            $options['defaultEmailAddress'] = $options['contactEmailAddress'];
        }
        if (!empty($options['boardUrl']))
        {
            $options['options']['boardUrl'] = rtrim($options['boardUrl'], '/');
        }

        /** @var \XF\Repository\Option $optionRepo */
        $optionRepo = $this->repository('XF:Option');

        // if applicable, updating collectServerStats will enqueue stats collection automatically
        $optionRepo->updateOptions($options);
And we all updated the settings group without any problems.
But maybe we need to update the setting by skipping its check.
PHP:
$serverStatsConfig = $this->app->options()->collectServerStats;
        $serverStatsConfig['last_sent'] = time();
        /** @var \XF\Repository\Option $optionRepo */
        $optionRepo = $this->app->repository('XF:Option');

        // skip verifying the option here as only last_sent will have changed
        $optionRepo->updateOptionSkipVerify('collectServerStats', $serverStatsConfig);
 
Back
Top