Skip to content

Commit

Permalink
fix: BelongsToMany and add onRequestValue
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-to committed Feb 1, 2025
1 parent b7ff093 commit e9d5758
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/DependencyInjection/MoonShine.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use MoonShine\Contracts\Core\DependencyInjection\StorageContract;
use MoonShine\Core\Core;
use MoonShine\Core\Storage\FileStorage;
use MoonShine\Laravel\Fields\Relationships\ModelRelationField;

/**
* @extends Core<MoonShineConfigurator>
Expand Down Expand Up @@ -58,4 +59,11 @@ public function getStorage(...$parameters): StorageContract
{
return app()->make(StorageContract::class, $parameters) ?? new FileStorage();
}

public function flushState(): void
{
parent::flushState();

ModelRelationField::$excludeInstancing = [];
}
}
40 changes: 29 additions & 11 deletions src/Fields/Relationships/ModelRelationField.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ abstract class ModelRelationField extends Field implements HasResourceContract

protected bool $isMorph = false;

public static array $excludeInstancing = [];

/**
* @throws Throwable
*/
Expand Down Expand Up @@ -76,7 +78,7 @@ public function __construct(
->singular()
->snake()
->append('_id')
->value()
->value(),
);
}

Expand All @@ -89,11 +91,27 @@ public function __construct(
}

// required to create field entities and load assets
if ($this instanceof HasFieldsContract && ! $this->isMorph()) {
if ($this instanceof HasFieldsContract && !$this->isExcludeInstancing() && ! $this->isMorph()) {
$this->excludeInstancing();
$this->getResource()?->getFormFields();
}
}

public function excludeInstancing(): void
{
self::$excludeInstancing[$this->getExcludeInstanceName()] = true;
}

private function getExcludeInstanceName(): string
{
return class_basename($this) . $this->getRelationName();
}

private function isExcludeInstancing(): bool
{
return isset(self::$excludeInstancing[$this->getExcludeInstanceName()]);
}

/**
* @param ?class-string<ModelResource> $classString
* @throws Throwable
Expand All @@ -112,13 +130,13 @@ protected function findResource(?string $classString = null): ModelResource
->singular()
->append('Resource')
->kebab()
->value()
->value(),
);

if (\is_null($resource) && $this->isMorph()) {
/** @var ModelResource $resource */
$resource = moonshine()->getResources()->findByUri(
moonshineRequest()->getResourceUri()
moonshineRequest()->getResourceUri(),
);
}

Expand All @@ -127,9 +145,9 @@ protected function findResource(?string $classString = null): ModelResource
function (?ModelResource $resource): void {
throw_if(
\is_null($resource),
FieldException::resourceRequired(static::class, $this->getRelationName())
FieldException::resourceRequired(static::class, $this->getRelationName()),
);
}
},
);
}

Expand All @@ -156,15 +174,15 @@ protected function resolveFill(array $raw = [], ?DataWrapperContract $casted = n

if ($this->isToOne()) {
$this->setColumn(
$this->getRelation()?->getForeignKeyName() ?? ''
$this->getRelation()?->getForeignKeyName() ?? '',
);

$this->setRawValue(
$raw[$this->getColumn()] ?? null
$raw[$this->getColumn()] ?? null,
);

$this->setFormattedValue(
data_get($data, $this->getResourceColumn())
data_get($data, $this->getResourceColumn()),
);
}

Expand All @@ -185,8 +203,8 @@ public function toFormattedValue(): mixed
$this->getFormattedValueCallback(),
$value ?? $this->getRelation()?->getModel(),
$this->getRowIndex(),
$this
)
$this,
),
);
}

Expand Down
5 changes: 5 additions & 0 deletions src/Traits/Fields/BelongsToOrManyCreatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Closure;
use MoonShine\Contracts\UI\ActionButtonContract;
use MoonShine\Laravel\Buttons\BelongsToOrManyButton;
use MoonShine\Laravel\Fields\Relationships\BelongsToMany;
use Throwable;

trait BelongsToOrManyCreatable
Expand Down Expand Up @@ -43,6 +44,10 @@ public function getCreateButton(): ?ActionButtonContract
return null;
}

if ($this->getParent() instanceof BelongsToMany) {
return null;
}

$button = BelongsToOrManyButton::for($this, button: $this->creatableButton);

return $button->isSee()
Expand Down

0 comments on commit e9d5758

Please sign in to comment.