Fallback to existing mention label if provider does not return a label (#19000)

* Fallback to existing mention label if provider does not return a label

* Update RichEditorStateCast.php

---------

Co-authored-by: Dan Harrin <git@danharrin.com>
This commit is contained in:
Sandro Gehri
2026-01-21 13:33:36 +00:00
committed by GitHub
co-authored by Dan Harrin
parent f9c9a67ab1
commit 655fc9aaf9
3 changed files with 6 additions and 28 deletions
@@ -294,10 +294,6 @@ class RichContentRenderer implements Htmlable
return;
}
if (filled($node->attrs->label ?? null)) {
return;
}
$char = $node->attrs->char ?? '@';
$mentionsByChar[$char][] = (string) $id;
});
@@ -330,7 +326,7 @@ class RichContentRenderer implements Htmlable
return;
}
$label = $node->attrs->label ?? $labelsByChar[$char][(string) $id] ?? (string) $id;
$label = $labelsByChar[$char][(string) $id] ?? $node->attrs->label ?? (string) $id;
$node->attrs->label = $label;
$url = $provider->getUrl((string) $id, $label);
@@ -53,16 +53,6 @@ class RichEditorStateCast implements StateCast
});
}
if ($this->richEditor->getMentionProviders()) {
$editor->descendants(function (object &$node): void {
if ($node->type !== 'mention') {
return;
}
unset($node->attrs->label);
});
}
return $editor->{$this->richEditor->isJson() ? 'getDocument' : 'getHtml'}();
}
@@ -631,9 +631,7 @@ it('handles multiple mention providers with different chars', function (): void
expect($html)->toContain('#issue-123');
});
it('preserves existing labels and does not refetch them', function (): void {
$fetchCount = 0;
it('falls back to existing label when provider returns no label', function (): void {
$renderer = RichContentRenderer::make([
'type' => 'doc',
'content' => [
@@ -643,9 +641,9 @@ it('preserves existing labels and does not refetch them', function (): void {
[
'type' => 'mention',
'attrs' => [
'id' => '1',
'label' => 'Existing Label',
'id' => '999',
'char' => '@',
'label' => 'Existing Label',
],
],
],
@@ -655,21 +653,15 @@ it('preserves existing labels and does not refetch them', function (): void {
$renderer->mentions([
MentionProvider::make('@')
->getLabelsUsing(function (array $ids) use (&$fetchCount): array {
$fetchCount++;
return ['1' => 'New Label'];
}),
->getLabelsUsing(fn (array $ids): array => []),
]);
$html = $renderer->toUnsafeHtml();
expect($fetchCount)->toBe(0);
expect($html)->toContain('@Existing Label');
expect($html)->not->toContain('New Label');
});
it('falls back to id as label when provider returns no label', function (): void {
it('falls back to id as label when provider returns no label and no label is set', function (): void {
$renderer = RichContentRenderer::make([
'type' => 'doc',
'content' => [