Extensions (PHP) · Content types
Content types
A content type is a model members create and read, like a pin, a recipe or a logbook note. When your model describes itself through the content contracts, the board's own features work on it: members can follow it, react, report it and find it in search, and moderators see it in Needs attention.
Make one
php artisan fs:make content-type Note
This adds src/Models/Note.php with every feature switched on, src/Search/NoteSearch.php and a migration for its table. Run fs:sync and open Developer, Content types in the admin panel to see it listed with what it can do.
The Content contract
Every type implements App\Content\Contracts\Content. It answers five questions the board asks everywhere, in alerts, reports, search and the moderation queue.
public function contentAuthor(): ?User;
public function contentTitle(): ?string;
public function contentExcerpt(int $length = 160): string;
public function contentUrl(): ?string;
public function contentLabel(): string;
Adding a feature
Implement the contract and use its trait. Most features need one or two methods of your own.
| Feature | Contract | Trait | You add |
|---|---|---|---|
| Follow | Followable |
HasFollowers |
canBeFollowedBy(). Members follow it and get alerts; alerts re-check access. |
| Reactions | Reactable |
HasReactions |
Nothing else. Show it with the reactions component and the Reactions service. |
| Emoji reactions | EmojiReactable |
HasEmojiReactions |
Nothing else. Quick emoji reactions for chat-style content. |
| Report | Reportable |
- | canBeReportedBy(). It then appears in the report modal and the report centre. |
| Mentions | Mentionable |
SendsMentionAlerts |
mentionBody() and isVisibleTo(). Mentioned members get one alert each. |
| Moderation | Moderatable |
HasModerationState |
moderatedBy() and moderationUrl(). Waiting items show in Needs attention. |
| Search | SearchableContent |
HasContentSearch |
listedFor() and searchColumns(), then a search source that extends ContentSearchSource. |
The board checks for the contract, not the trait, so a model that uses a trait without its contract gets none of the feature. fs:check points this out.
Search
A class in src/Search that extends ContentSearchSource is picked up on its own. It names the model, a key, a label and an icon; matching, visibility, dates and results come from the model.
class NoteSearch extends ContentSearchSource
{
public function model(): string
{
return Note::class;
}
public function key(): string
{
return 'harbour-logbook-notes';
}
public function label(): string
{
return __('Notes');
}
public function icon(): string
{
return 'fa-regular fa-file-lines';
}
}
Hiding content from some members
A class in src/Visibility that implements ContentVisibilityRule can hide any content, yours or the board's, from a member. The board asks it before showing a content page, before sending a follow or mention alert and before listing a search result. Return false for anything your rule does not concern, and keep it fast, because it runs often.
class MembersOnlyNotes implements ContentVisibilityRule
{
public function hides(Content&Model $item, ?User $viewer): bool
{
return $item instanceof Note && $viewer === null;
}
}