fix: Blank and relationship rich content attributes

This commit is contained in:
Dan Harrin
2025-09-16 09:52:59 +01:00
parent cc74ed81bf
commit dabc90b401
2 changed files with 43 additions and 29 deletions
@@ -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)
@@ -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