-
-
Notifications
You must be signed in to change notification settings - Fork 133
docs: add avatar drivers guide for User extender #498
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
datlechin
wants to merge
9
commits into
flarum:main
Choose a base branch
from
datlechin:docs/avatar-drivers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cd37cf8
docs: add avatar drivers guide for User extender
datlechin fd5938b
wip
datlechin f9df2e7
docs: add avatar drivers guide for User extender
datlechin a0a1d67
docs: update avatars.md to match Flarum documentation style
datlechin c48516c
wip
datlechin 65c1903
wip
datlechin b4b76fa
wip
datlechin 530c31d
Merge branch 'flarum:main' into docs/avatar-drivers
datlechin 510e5f7
docs: address PR review feedback for avatar drivers guide
datlechin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # Avatar Drivers | ||
|
|
||
| Flarum allows new avatar drivers to be added through extenders. By default, Flarum includes one driver: the Default driver (which returns null when no uploaded avatar is present). A Gravatar driver will be available as part of the core extension suite. | ||
|
|
||
| To create your own avatar driver, you will need to create a class implementing `Flarum\User\Avatar\DriverInterface`. This allows extensions to integrate custom avatar providers from external services like Discord, GitHub, Steam, or any custom service. | ||
|
|
||
| ## How It Works | ||
|
|
||
| When Flarum needs to display a user's avatar, it resolves the avatar URL through a fallback chain: | ||
|
|
||
| 1. **Custom uploaded avatar** - If the user has uploaded an avatar, use that file | ||
| 2. **Custom avatar URL** - If a full URL (with `://`) is set in the database | ||
| 3. **Configured avatar driver** - Call the active driver's `avatarUrl()` method | ||
| 4. **Null** - Falls back to default UI avatar (initials) | ||
|
|
||
| Your driver is only called when the user has no uploaded avatar or custom URL. This means users can always override your driver by uploading their own avatar. The `$user->avatar_url` accessor handles this resolution automatically. You can check if a user has a custom avatar using `$user->original_avatar_url`. | ||
|
|
||
| ## Creating a Driver | ||
|
|
||
| The `DriverInterface` has a single method which receives a `User` model and returns either a URL string or null: | ||
|
|
||
| ```php | ||
| namespace Acme\Avatars\Driver; | ||
|
|
||
| use Flarum\User\Avatar\DriverInterface; | ||
| use Flarum\User\User; | ||
|
|
||
| class DiscordAvatarDriver implements DriverInterface | ||
| { | ||
| public function avatarUrl(User $user): ?string | ||
| { | ||
| $discordId = $user->discord_id; | ||
| $avatarHash = $user->discord_avatar_hash; | ||
|
|
||
| if (! $discordId || ! $avatarHash) { | ||
| return null; | ||
| } | ||
|
|
||
| return "https://cdn.discordapp.com/avatars/{$discordId}/{$avatarHash}.png?size=200"; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| The `avatarUrl()` method receives the full User model, so you can access any user properties including custom attributes you have added via [migrations](database.md). | ||
|
|
||
| Drivers should return `null` when they cannot provide an avatar URL. This allows Flarum to fall back to the default avatar display. | ||
|
|
||
| ## Registering a Driver | ||
|
|
||
| To register avatar drivers, use the `Flarum\Extend\User` extender in your extension's `extend.php` file: | ||
|
|
||
| ```php | ||
| use Flarum\Extend; | ||
| use Acme\Avatars\Driver\DiscordAvatarDriver; | ||
|
|
||
| return [ | ||
| // Other extenders | ||
| (new Extend\User()) | ||
| ->avatarDriver('discord', DiscordAvatarDriver::class), | ||
| // Other extenders | ||
| ]; | ||
| ``` | ||
|
|
||
datlechin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| The first parameter is a unique identifier for your driver. This identifier is stored in the `avatar_driver` setting when an administrator selects it from the admin panel, so it should be unique across all extensions. | ||
|
|
||
| ## Admin Configuration | ||
|
|
||
| Once registered, avatar drivers will automatically appear in the admin dashboard under **Basics → Avatar Driver**. The dropdown will only appear when two or more drivers are registered. Administrators can select which driver should be active for users who have not uploaded custom avatars. | ||
|
|
||
| Flarum automatically adds your driver to the admin settings page when you register it. | ||
|
|
||
| > **Note:** The admin dropdown will display the raw driver identifier by default. To show a human-readable label, override `BasicsPage.driverLocale` in your extension's frontend code. | ||
|
|
||
| The selected driver identifier is stored in the `avatar_driver` setting. If the setting is empty or refers to an unregistered driver, Flarum will use the Default driver as a fallback. | ||
|
|
||
| For a minimal example, see Flarum's built-in [DefaultDriver](https://github.com/flarum/framework/blob/main/framework/core/src/User/Avatar/DefaultDriver.php). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.