The Livewire component gives you the option to enable mentions in your comments.
If mentions are enabled your users will be able to start searching for a user they want to mention by typing @
in the comment field.
When a comment with a mention is created users will receive a notification of the created comment.
Out of the box mentions will only autocomplete when using the EasyMDE editor.
##Customising the autocomplete
By default the autocomplete will do a search on the configured name field for you commentator model.
You can customise this by defining your own Resolver class and adding it to the actions.resolve_mentions_autocomplete
key in the comments.php
config file.
For example if you want to add a custom scope to the search you could do the following:
class CustomResolveMentionsAutocompleteAction
{
public function resolve(string $query, $commentable): array
{
$modelClass = Config::commentator();
return $modelClass::where(Config::nameField(), 'like', "%$query%")
->notBanned()
->limit(10)
->get()
->toArray();
}
}
##Rendering mentions
By default we recommend to render the mentions by adding the MentionsTransformer
to the comment_transformers
array in the comments.php
config file.
##Custom rendering of mentions
If you want to customise how these mentions are rendered you can extend and the MentionsTransformer
and add your own logic.
class CustomMentionsTransformer implements MentionsTransformer
{
public function renderMention(CanComment $mentionee): string
{
return $mentionee->commentatorProperties()->name;
}
}
Or render a custom Blade component:
class CustomMentionsTransformer implements MentionsTransformer
{
public function renderMention(CanComment $mentionee): string
{
return Blade::renderComponent(new CustomMentionComponent($mentionee));
}
}
##Updating usernames
Because comments are stored as HTML in the database when a user changes his username this will not reflect in the comments.
However on each save of a comment the configure transformers will be called to render the comment and the username will be updated.
We recommend rolling your own system to suit your needs.
An example of how to do this is dispatching a Job when a user changes his name. This could look something like:
class UpdateUsernameInCommentsJob implements ShouldQueue
{
use Queueable;
public function __construct(protected User $user)
{
}
public function handle(): void
{
$comments = Comment::whereLike('original_text', "%data-mention=\"{$this->user->id}\"%")->get();
$comments->each(function($comment) {
$comment->save();
});
}
}
##Mentions with a custom editor
If you want to use mentions with your own editor you could implement your own system for this.
To be compatible with this package the only thing you need to make sure is that you store the mentions in the following format:
## This is the raw comment
<span data-mentionee="1">John Doe</span> is a great guy!
The data-mentionee
attribute needs to be equal to the id of the user/commentator you are mentioning.
##Mentions and anonymous comments
Mentions will work when anonymous comments are enabled, but you need to make sure the comments.models.commentator
config key is set.