Skip to content

Commit

Permalink
Add link to front-end page functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarpsvo committed Oct 16, 2019
1 parent 8d4cf5d commit ccb6fa4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,32 @@ Example:
```php
// in app/Providers/NovaServiceProvider.php

use \OptimistDigital\NovaPageManager\NovaPageManager;

public function boot()
{
\OptimistDigital\NovaPageManager\NovaPageManager::configure([
NovaPageManager::configure([
'templates' => [],
'locales' => [],
'draft' => true
]);
}
```

### Add links to front-end pages

To display a link next to the slug that links to the actual page in the front-end you must pass a function that generates the URL to `NovaPageManager::pagePreviewUrl()`.

As shown in this example:

```php
use \OptimistDigital\NovaPageManager\NovaPageManager;

NovaPageManager::pagePreviewUrl(function (Page $page) {
return env('FRONTEND_URL') . $page->path;
});
```

### Overwrite package resources

You can overwrite the package resources (Page & Region) by setting the config options in `nova-page-manager.php`.
Expand Down
21 changes: 17 additions & 4 deletions src/Nova/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,26 @@ public function fields(Request $request)
Text::make('Slug', 'slug')
->creationRules('required', "unique:{$tableName},slug,NULL,id,locale,$request->locale")
->updateRules('required', "unique:{$tableName},slug,{{resourceId}},id,published,{{published}},locale,$request->locale")
->hideFromDetail(),
->onlyOnForms(),
Text::make('Slug', function () {
$previewToken = $this->childDraft ? $this->childDraft->preview_token : $this->preview_token;
$previewPart = $previewToken ? '?preview=' . $previewToken : '';
return $this->slug . $previewPart;
})->onlyOnDetail(),
ParentField::make('Parent', 'parent_id'),
$getPagePreviewUrlFn = NovaPageManager::getPagePreviewUrlFn();
$path = $this->resource->path;
$frontendLink = isset($getPagePreviewUrlFn) ? call_user_func($getPagePreviewUrlFn, $this->resource) . $previewPart : null;


if (isset($frontendLink)) {
return <<<HTML
<div class='whitespace-no-wrap'>
<span class='bg-40 text-sm py-1 px-2 rounded-lg'>$path</span>
<a target='_blank' href='$frontendLink' class='text-sm py-1 px-2 text-primary no-underline dim font-bold'>View</a>
</div>
HTML;
}
return "<span class='bg-40 text-sm py-1 px-2 rounded-lg whitespace-no-wrap'>$path</span>";
})->asHtml()->exceptOnForms(),
ParentField::make('Parent', 'parent_id')->hideFromIndex(),
TemplateField::make('Template', 'template'),
LocaleField::make('Locale', 'locale', 'locale_parent_id')
->locales(NovaPageManager::getLocales())
Expand Down
11 changes: 11 additions & 0 deletions src/NovaPageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class NovaPageManager extends Tool
private static $templates = [];
private static $locales = [];
private static $draftEnabled = false;
private static $getPageFrontendUrlFn;

/**
* Perform any tasks that need to happen when the tool is booted.
Expand Down Expand Up @@ -79,6 +80,16 @@ public static function getRegionsTableName(): string
return config('nova-page-manager.table', 'nova_page_manager') . '_regions';
}

public static function pagePreviewUrl(\Closure $fn)
{
self::$getPageFrontendUrlFn = $fn;
}

public static function getPagePreviewUrlFn()
{
return self::$getPageFrontendUrlFn;
}

public static function draftEnabled(): bool
{
return self::$draftEnabled;
Expand Down

0 comments on commit ccb6fa4

Please sign in to comment.