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

[7.x] Publish Form Updates #509

Merged
merged 6 commits into from
Jun 4, 2024
Merged
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
379 changes: 247 additions & 132 deletions resources/js/components/Publish/PublishForm.vue

Large diffs are not rendered by default.

37 changes: 0 additions & 37 deletions resources/js/components/statamic/HasPreferences.js

This file was deleted.

110 changes: 0 additions & 110 deletions resources/js/components/statamic/SaveButtonOptions.vue

This file was deleted.

20 changes: 10 additions & 10 deletions resources/views/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@

@section('content')
<runway-publish-form
:breadcrumbs="{{ $breadcrumbs->toJson() }}"
:initial-actions="{{ json_encode($actions) }}"
:initial-blueprint='@json($blueprint)'
:initial-meta='@json($meta)'
:initial-values='@json($values)'
initial-title="{{ $title }}"
method="{{ $method }}"
:resource-has-routes="{{ $resourceHasRoutes ? 'true' : 'false' }}"
:is-creating="true"
publish-container="base"
:initial-actions='@json($actions)'
method="post"
:resource='@json($resource->toArray())'
:resource-has-routes="{{ $str::bool($resourceHasRoutes) }}"
initial-title="{{ $title }}"
:initial-blueprint='@json($blueprint)'
:initial-values='@json($values)'
:initial-meta='@json($meta)'
:breadcrumbs='{{ $breadcrumbs->toJson() }}'
:can-manage-publish-state="{{ $str::bool($canManagePublishState) }}"
create-another-url="{{ cp_route('runway.create', ['resource' => $resource->handle()]) }}"
listing-url="{{ cp_route('runway.index', ['resource' => $resource->handle()]) }}"
initial-listing-url="{{ cp_route('runway.index', ['resource' => $resource->handle()]) }}"
:can-manage-publish-state="{{ $str::bool($canManagePublishState) }}"
published-column="{{ $publishedColumn }}"
></runway-publish-form>
@endsection
28 changes: 13 additions & 15 deletions resources/views/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,25 @@

@section('content')
<runway-publish-form
:breadcrumbs="{{ $breadcrumbs->toJson() }}"
initial-reference="{{ $currentModel['reference'] }}"
:initial-actions="{{ json_encode($actions) }}"
:initial-blueprint='@json($blueprint)'
:initial-meta='@json($meta)'
:initial-values='@json($values)'
initial-title="{{ $title }}"
method="{{ $method }}"
:resource-has-routes="{{ $resourceHasRoutes ? 'true' : 'false' }}"
permalink="{{ $permalink }}"
:is-creating="false"
publish-container="base"
:read-only="{{ $resource->readOnly() ? 'true' : 'false' }}"
:initial-actions='@json($actions)'
method="patch"
:resource='@json($resource->toArray())'
create-another-url="{{ cp_route('runway.create', ['resource' => $resource->handle()]) }}"
listing-url="{{ cp_route('runway.index', ['resource' => $resource->handle()]) }}"
:resource-has-routes="{{ $str::bool($resourceHasRoutes) }}"
initial-title="{{ $title }}"
initial-reference="{{ $reference }}"
:initial-blueprint='@json($blueprint)'
:initial-values='@json($values)'
:initial-meta='@json($meta)'
initial-permalink="{{ $permalink }}"
:initial-read-only="{{ $str::bool($readOnly) }}"
:breadcrumbs="{{ $breadcrumbs->toJson() }}"
:can-edit-blueprint="{{ Auth::user()->can('configure fields') ? 'true' : 'false' }}"
:can-manage-publish-state="{{ $str::bool($canManagePublishState) }}"
create-another-url="{{ cp_route('runway.create', ['resource' => $resource->handle()]) }}"
initial-listing-url="{{ cp_route('runway.index', ['resource' => $resource->handle()]) }}"
:initial-item-actions="{{ json_encode($itemActions) }}"
item-action-url="{{ cp_route('runway.actions.run', ['resource' => $resource->handle()]) }}"
published-column="{{ $publishedColumn }}"
></runway-publish-form>

<script>
Expand Down
11 changes: 6 additions & 5 deletions src/Fieldtypes/BaseFieldtype.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@ class BaseFieldtype extends Relationship
protected $formComponent = 'runway-publish-form';

protected $formComponentProps = [
'initialReference' => 'reference',
'initialBlueprint' => 'blueprint',
'initialValues' => 'values',
'initialMeta' => 'meta',
'initialTitle' => 'title',
'initialActions' => 'actions',
'method' => 'method',
'resourceHasRoutes' => 'resourceHasRoutes',
'permalink' => 'permalink',
'resource' => 'resource',
'breadcrumbs' => 'breadcrumbs',
'initialActions' => 'actions',
'method' => 'method',
'initialReadOnly' => 'readOnly',
'initialPermalink' => 'permalink',
'canManagePublishState' => 'canManagePublishState',
'publishedColumn' => 'publishedColumn',
'resourceHasRoutes' => 'resourceHasRoutes',
];

protected function configFieldItems(): array
Expand Down
46 changes: 17 additions & 29 deletions src/Http/Controllers/CP/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Statamic\Exceptions\NotFoundHttpException;
use Statamic\Facades\Action;
use Statamic\Facades\Scope;
use Statamic\Facades\User;
use Statamic\Fields\Field;
use Statamic\Http\Controllers\CP\CpController;
use StatamicRadPack\Runway\Fieldtypes\BelongsToFieldtype;
Expand All @@ -17,6 +18,7 @@
use StatamicRadPack\Runway\Http\Requests\CP\IndexRequest;
use StatamicRadPack\Runway\Http\Requests\CP\StoreRequest;
use StatamicRadPack\Runway\Http\Requests\CP\UpdateRequest;
use StatamicRadPack\Runway\Http\Resources\CP\Model as ModelResource;
use StatamicRadPack\Runway\Resource;
use StatamicRadPack\Runway\Runway;

Expand Down Expand Up @@ -63,7 +65,6 @@ public function create(CreateRequest $request, Resource $resource)

$viewData = [
'title' => __('Create :resource', ['resource' => $resource->singular()]),
'method' => 'POST',
'breadcrumbs' => new Breadcrumbs([[
'text' => $resource->plural(),
'url' => cp_route('runway.index', [
Expand All @@ -79,10 +80,8 @@ public function create(CreateRequest $request, Resource $resource)
$resource->publishedColumn() => true,
])->all(),
'meta' => $fields->meta(),
'permalink' => null,
'resourceHasRoutes' => $resource->hasRouting(),
'canManagePublishState' => $resource->hasPublishStates(),
'publishedColumn' => $resource->publishedColumn(),
'canManagePublishState' => User::current()->can('edit', $resource),
];

if ($request->wantsJson()) {
Expand Down Expand Up @@ -110,18 +109,15 @@ public function store(StoreRequest $request, Resource $resource)

$this->prepareModelForSaving($resource, $model, $request);

$model->save();
$saved = $model->save();

// Runs anything in the $postCreatedHooks array. See HasManyFieldtype@process for an example
// of where this is used.
$postCreatedHooks->each(fn ($postCreatedHook) => $postCreatedHook($resource, $model));

return [
'data' => $this->getReturnData($resource, $model),
'redirect' => cp_route('runway.edit', [
'resource' => $resource->handle(),
'model' => $model->{$resource->routeKey()},
]),
'data' => (new ModelResource($model->fresh()))->resolve()['data'],
'saved' => $saved,
];
}

Expand All @@ -141,7 +137,8 @@ public function edit(EditRequest $request, Resource $resource, $model)

$viewData = [
'title' => $model->getAttribute($resource->titleField()),
'method' => 'PATCH',
'reference' => $model->reference(),
'method' => 'patch',
'breadcrumbs' => new Breadcrumbs([[
'text' => $resource->plural(),
'url' => cp_route('runway.index', [
Expand All @@ -156,6 +153,7 @@ public function edit(EditRequest $request, Resource $resource, $model)
'blueprint' => $blueprint->toPublishArray(),
'values' => $values,
'meta' => $meta,
'readOnly' => $resource->readOnly(),
'permalink' => $resource->hasRouting() ? $model->uri() : null,
'resourceHasRoutes' => $resource->hasRouting(),
'currentModel' => [
Expand All @@ -164,8 +162,7 @@ public function edit(EditRequest $request, Resource $resource, $model)
'title' => $model->{$resource->titleField()},
'edit_url' => $request->url(),
],
'canManagePublishState' => $resource->hasPublishStates(),
'publishedColumn' => $resource->publishedColumn(),
'canManagePublishState' => User::current()->can('edit', $resource),
'itemActions' => Action::for($model, ['resource' => $resource->handle(), 'view' => 'form']),
];

Expand All @@ -184,29 +181,20 @@ public function update(UpdateRequest $request, Resource $resource, $model)

$this->prepareModelForSaving($resource, $model, $request);

$model->save();
$saved = $model->save();

if ($request->get('from_inline_publish_form')) {
$this->handleInlinePublishForm($resource, $model);
}

return ['data' => $this->getReturnData($resource, $model)];
}
[$values] = $this->extractFromFields($model, $resource, $resource->blueprint());

/**
* Build an array with the correct return data for the inline publish forms.
*/
protected function getReturnData(Resource $resource, Model $model): array
{
return array_merge($model->toArray(), [
'title' => $model->{$resource->titleField()},
'published' => $model->{$resource->publishedColumn()},
'status' => $model->publishedStatus(),
'edit_url' => cp_route('runway.edit', [
'resource' => $resource->handle(),
'model' => $model->{$resource->routeKey()},
return [
'data' => array_merge((new ModelResource($model->fresh()))->resolve()['data'], [
'values' => $values,
]),
]);
'saved' => $saved,
];
}

/**
Expand Down
30 changes: 30 additions & 0 deletions src/Http/Resources/CP/Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace StatamicRadPack\Runway\Http\Resources\CP;

use Illuminate\Http\Resources\Json\JsonResource;

class Model extends JsonResource
{
public function toArray($request)
{
$runwayResource = $this->resource->runwayResource();

$data = [
'id' => $this->resource->getKey(),
'reference' => $this->resource->reference(),
'title' => $this->resource->{$runwayResource->titleField()},
'permalink' => $runwayResource->hasRouting() ? $this->resource->absoluteUrl() : null,
'published' => $this->resource->publishedStatus(),
'edit_url' => cp_route('runway.edit', [
'resource' => $runwayResource->handle(),
'model' => $this->resource->{$runwayResource->routeKey()},
]),
'resource' => [
'handle' => $runwayResource->handle(),
],
];

return ['data' => $data];
}
}
Loading
Loading