Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to edit InlineMorphTo Field after creation #25

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "digital-creative/nova-inline-morph-to",
"name": "marispro/nova-inline-morph-to",
"description": "A Laravel Nova field for displaying morphTo relationship inline.",
"keywords": [
"laravel",
Expand Down
99 changes: 42 additions & 57 deletions src/InlineMorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class InlineMorphTo extends Field
*/
public $component = 'inline-morph-to';

public $typeUpdateable = false;

/**
* Create a new field.
*
Expand All @@ -37,14 +39,26 @@ class InlineMorphTo extends Field
*/
public function __construct($name, $attribute = null, callable $resolveCallback = null)
{

parent::__construct($name, $attribute, $resolveCallback);

$this->meta = [
'resources' => [],
'listable' => true
];
}


/**
* Specify if a relation can be changed.
*
* @param bool $value
* @return $this
*/
public function typeUpdateable($value = true): InlineMorphTo
{
$this->typeUpdateable = $value;

return $this;
}

/**
Expand All @@ -59,7 +73,6 @@ public function __construct($name, $attribute = null, callable $resolveCallback
*/
public function types(array $types): self
{

$types = collect($types)->map(function (string $resource, $key) {

/**
Expand All @@ -73,13 +86,11 @@ public function types(array $types): self
'label' => is_numeric($key) ? $resource::label() : $key,
'fields' => $this->resolveFields($resourceInstance)
];

});

$this->withMeta([ 'resources' => $types->values() ]);
$this->withMeta(['resources' => $types->values()]);

return $this;

}

private function resolveFields(Resource $resourceInstance): Collection
Expand All @@ -93,22 +104,21 @@ private function resolveFields(Resource $resourceInstance): Collection

switch (get_class($controller)) {

case CreationFieldController::class :
case CreationFieldController::class:
return $resourceInstance->creationFields($request);

case UpdateFieldController::class :
case UpdateFieldController::class:
return $resourceInstance->updateFields($request);

case ResourceShowController::class :
case ResourceShowController::class:
return $resourceInstance->detailFields($request);

case ResourceIndexController::class :
case ResourceIndexController::class:
return $resourceInstance->indexFields($request);

}

return $resourceInstance->availableFields($request);

}

/**
Expand All @@ -130,15 +140,10 @@ public function resolveForDisplay($resource, $attribute = null)
parent::resolveForDisplay($resource, $attribute);

if ($relationInstance = $resource->$attribute) {

foreach ($this->getFields($relationInstance) as $field) {

$field->resolveForDisplay($relationInstance);

}

}

}

/**
Expand All @@ -160,23 +165,16 @@ public function resolve($resource, $attribute = null)
parent::resolve($resource, $attribute);

if ($relationInstance = $resource->$attribute) {

foreach ($this->getFields($relationInstance) as $field) {

if ($field->computed()) {

$field->computedCallback = $field->computedCallback->bindTo(
Nova::newResourceFromModel($relationInstance)
);

}

$field->resolve($relationInstance);

}

}

}

/**
Expand All @@ -195,28 +193,21 @@ protected function resolveAttribute($resource, $attribute)
*/

if ($relationInstance = $resource->$attribute) {

$resource = Nova::resourceForModel($relationInstance);

foreach ($this->getFields($relationInstance) as $field) {

if ($field instanceof HasOne ||
$field instanceof HasMany ||
$field instanceof BelongsToMany) {

$field->meta[ 'inlineMorphTo' ] = [
$field->meta['inlineMorphTo'] = [
'viaResourceId' => $relationInstance->id,
'viaResource' => $resource::uriKey()
];

}

}

return $resource;

}

}

public function fill(NovaRequest $request, $model)
Expand All @@ -228,58 +219,58 @@ public function fill(NovaRequest $request, $model)
* @var Resource $resource
* @var Field $field
*/

$resourceClass = $request->input($this->attribute);
$relatedInstance = $model->{$this->attribute} ?? $resourceClass::newModel();

if ($this->typeUpdateable) {
if ($model->{$this->attribute} !== null && get_class($model->{$this->attribute}) == get_class($resourceClass::newModel())) {
// same related model
$relatedInstance = $model->{$this->attribute};
} else {
// model has changed
$relatedInstance = $resourceClass::newModel();
}
} else {
$relatedInstance = $model->{$this->attribute} ?? $resourceClass::newModel();
}


$resource = new $resourceClass($relatedInstance);

if ($relatedInstance->exists) {

if ($relatedInstance->exists) {
$resource->validateForUpdate($request);

} else {

$resource->validateForCreation($request);

}

$fields = $this->getFields($relatedInstance);
$callbacks = [];

foreach ($fields as $field) {

$callbacks[] = $field->fill($request, $relatedInstance);

}

$relatedInstance->saveOrFail();

$model->{$this->attribute}()->associate($relatedInstance);

return function () use ($callbacks) {

return function () use ($callbacks) {
foreach ($callbacks as $callback) {

if (is_callable($callback)) {

call_user_func($callback);

}

}

};

}

private function getFields(Model $model): Collection
{
$resourceClass = Nova::resourceForModel($model);

return $this->meta[ 'resources' ]->where('className', $resourceClass)->first()[ 'fields' ];
return $this->meta['resources']->where('className', $resourceClass)->first()['fields'];
}

public function jsonSerialize()
public function jsonSerialize(): array
{

/**
Expand All @@ -291,22 +282,16 @@ public function jsonSerialize()
/**
* Temporarily remap the route resource key so every sub field thinks its being resolved by its original parent
*/
foreach ($this->meta[ 'resources' ] as $resource) {

$resource[ 'fields' ] = $resource[ 'fields' ]->transform(function ($field) use ($request, $resource) {

$request->route()->setParameter('resource', $resource[ 'uriKey' ]);
foreach ($this->meta['resources'] as $resource) {
$resource['fields'] = $resource['fields']->transform(function ($field) use ($request, $resource) {
$request->route()->setParameter('resource', $resource['uriKey']);

return $field->jsonSerialize();

});

}

$request->route()->setParameter('resource', $originalResource);

return parent::jsonSerialize();

}

}