diff --git a/packages/forms/src/Components/RichEditor/RichContentAttribute.php b/packages/forms/src/Components/RichEditor/RichContentAttribute.php index 9cb785bb00..fa17795a60 100644 --- a/packages/forms/src/Components/RichEditor/RichContentAttribute.php +++ b/packages/forms/src/Components/RichEditor/RichContentAttribute.php @@ -109,7 +109,13 @@ class RichContentAttribute implements Htmlable public function toHtml(): string { - return RichContentRenderer::make($this->model->getAttribute($this->name)) + $content = $this->model->getAttribute($this->name); + + if (blank($content)) { + return ''; + } + + return RichContentRenderer::make($content) ->plugins($this->getPlugins()) ->customBlocks($this->customBlocks) ->mergeTags($this->mergeTags) diff --git a/packages/schemas/src/Components/Concerns/HasState.php b/packages/schemas/src/Components/Concerns/HasState.php index b6cebfe583..24137fce22 100644 --- a/packages/schemas/src/Components/Concerns/HasState.php +++ b/packages/schemas/src/Components/Concerns/HasState.php @@ -930,6 +930,41 @@ trait HasState */ public function getConstantStateFromRecord(Model $record): mixed { + $relationship = $this->getStateRelationship($record); + + if ($relationship) { + $relationshipAttribute = $this->getStateRelationshipAttribute(); + + $state = collect($this->getStateRelationshipResults($record)) + ->reduce( + function (Collection $carry, Model $record) use ($relationshipAttribute): Collection { + if ( + ($record instanceof HasRichContent) && + $record->hasRichContentAttribute($relationshipAttribute) + ) { + $state = $record->getRichContentAttribute($relationshipAttribute); + } else { + $state = data_get($record, $relationshipAttribute); + } + + if (blank($state)) { + return $carry; + } + + return $carry->push($state); + }, + initial: collect(), + ) + ->when($this->isDistinctList(), fn (Collection $state) => $state->unique()) + ->values(); + + if (! $state->count()) { + return null; + } + + return $state->all(); + } + $name = $this->getConstantStatePath(); if ( @@ -941,34 +976,7 @@ trait HasState $state = data_get($record, $name); } - if ($state !== null) { - return $state; - } - - if (! $this->hasStateRelationship($record)) { - return null; - } - - $relationship = $this->getStateRelationship($record); - - if (! $relationship) { - return null; - } - - $relationshipAttribute = $this->getStateRelationshipAttribute(); - - $state = collect($this->getStateRelationshipResults($record)) - ->filter(fn (Model $record): bool => array_key_exists($relationshipAttribute, $record->attributesToArray())) - ->pluck($relationshipAttribute) - ->filter(fn ($state): bool => filled($state)) - ->when($this->isDistinctList(), fn (Collection $state) => $state->unique()) - ->values(); - - if (! $state->count()) { - return null; - } - - return $state->all(); + return $state; } public function getStatePathForRelationship(): ?string