Skip to content

[6.0] Addon Settings #1699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 6.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions content/collections/docs/git-automation.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ You are free to define the tracked paths to be considered when staging and commi
'paths' => [
base_path('content'),
base_path('users'),
resource_path('addons'),
resource_path('blueprints'),
resource_path('fieldsets'),
resource_path('forms'),
Expand Down
60 changes: 60 additions & 0 deletions content/collections/extending-docs/addons.md
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,66 @@ if ($addon->edition() === 'pro') {
You don't need to check whether a license is valid, Statamic does that automatically for you.
:::

## Settings

Laravel config files are great for storing application settings, but they're not ideal for settings you might want users to edit through the Control Panel.

You can register a settings blueprint in your addon to give users a friendly interface for managing settings. Drop a blueprint file in `resources/blueprints/settings.yaml` or register it in your service provider like this:

```php
public function bootAddon()
{
$this->registerSettingsBlueprint([
'tabs' => [
'main' => [
'sections' => [
[
'display' => __('API'),
'fields' => [
[
'handle' => 'api_key',
'field' => ['type' => 'text', 'display' => 'API Key', 'validate' => 'required'],
],
// ...
],
],
],
],
],
]);
}
```

Your addon's settings page will show up in the Control Panel under **Tools -> Addons**. Pretty convenient.

You can even reference config options (and by extension environment variables) in your settings blueprint using Antlers, like so: `{{ config:app:url }}`.

Settings are stored as YAML files in `resources/addons` by default, but can be moved to the database if you prefer. Just run the `php please install:eloquent-driver` command and you're all set.

You can retrieve the settings using the `Addon` facade:

```php
use Statamic\Facades\Addon;

$addon = Addon::get('vendor/package');

// Getting settings...
$addon->settings()->get('api_key');

$addon->settings()->values();
$addon->settings()->rawValues(); // Doesn't evaluate Antlers

// Setting values...
$addon->settings()->set('api_key', '{{ config:services:example:api_key }}');

$addon->settings()->values([
'website_name' => 'My Awesome Site',
'api_key' => '{{ config:services:example:api_key }}',
]);

// Saving...
$addon->settings()->save();
```

## Update Scripts

Expand Down
24 changes: 24 additions & 0 deletions content/collections/extending-docs/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ If you're creating an addon, you can quickly [register event listeners or subscr

## Available Events

### AddonSettingsSaved
`Statamic\Events\AddonSettingsSaved`

Dispatched after an addon's settings have been saved.

``` php
public function handle(AddonSettingsSaved $event)
{
$event->addonSettings;
}
```

### AddonSettingsSaving
`Statamic\Events\AddonSettingsSaving`

Dispatched before an addon's settings are saved. You can return `false` to prevent them from being saved.

``` php
public function handle(AddonSettingsSaving $event)
{
$event->addonSettings;
}
```

### AssetContainerBlueprintFound
`Statamic\Events\AssetContainerBlueprintFound`

Expand Down