outerweb/nova-link-picker

一个用于生成链接的 Laravel Nova 字段

v1.1.0 2023-12-10 22:11 UTC

This package is auto-updated.

Last update: 2024-09-10 23:39:43 UTC


README

Latest Version on Packagist Total Downloads

此包提供了一个 Nova 字段,用于生成链接。它显示一个选择字段,显示您希望用作链接的应用程序路由。它还提供了固定选项,如 externalmailtotel。将自动应用路由模型绑定到参数,以便为用户提供具有可用选项的下拉菜单。

截图

编辑

Index

索引

Index

详情

Detail

安装

您可以通过 composer 安装此包

composer require outerweb/nova-link-picker

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="nova-link-picker-config"

这是已发布配置文件的内容

return [
    'link_entity' => \Outerweb\NovaLinkPicker\Entities\Link::class,
    'api_base_url' => '/nova-vendor/outerweb/nova-link-picker',
    'available_options' => [
        'open_in_new_tab' => true,
        'download' => true,
    ]
];

用法

该字段将值存储为数据库中的 JSON 字符串。因此,您需要确保数据库中的列是 json 列(如果您的数据库引擎尚未支持 json,则为 text)。

字段的值看起来像这样

{
    "routeName": string,
    "parameters": [
        [
            "name": string,
            "value": int|string
        ]
    ],
    "options": {
        "openInNewTab": bool,
        "download": bool
    }
}

为了更容易地处理此值,您可以使用 Outerweb\NovaLinkPicker\Casts'LinkPickerCast 转换。

use Outerweb\NovaLinkPicker\Casts\LinkPickerCast;

...

protected $casts = [
    'link' => LinkPickerCast::class,
];

此转换将值转换为 Outerweb\NovaLinkPicker\Entities'Link 实例。

在此实例上,您可以调用以下方法/属性

// Returns the route name as a string
// The routeName value can come from your applications registered routes
// or from the fixed options when it starts with `external.`:
// - `external.url` for an external url
// - `external.mailto` for a mailto link
// - `external.tel` for a tel link
$link->routeName;

// Returns the parameters as an array
$link->parameters;

// Returns the options as an array
$link->options;

// Generate the route (just like you would do with Laravel's `route()` helper)
$link->route();

// Get the target attribute value for the link
$link->target();

// Check if the link is an external link
$link->isExternal();

// Check if the link should be a download link
$link->isDownload();

// Render the link attributes (`href`, `target` and optionally `download`)
// Example: <a {{ $link->renderAttributes() }}>Click me!</a>
$link->renderAttributes();

将以下语法添加到您希望在下拉字段中可用的路由中

Route::get('my-route', function () {
    // ...
})->name('my-route')
    ->where('nova-link-picker', 'true');

默认情况下,路由的标签将是路由名称。如果路由包含点,则标签将使用 ">" 字符分割部分。例如:my-route 将是 My route,而 my-route.sub-route 将是 My route > Sub route。您始终可以通过向您的路由添加以下内容来自定义命名路由的标签:

Route::get('my-route', function () {
    // ...
})->name('my-route')
    ->where('nova-link-picker', 'true')
    ->where('nova-link-picker-label', __('My custom label'));

将字段添加到您的 Nova 资源中

use Outerweb\NovaLinkPicker\Nova\Fields\LinkPicker;

...

public function fields(Request $request)
{
    return [
        LinkPicker::make('Link')
            // This will Str::limit() the value on the Nova index table
            ->abbreviated()
            // This will hide the `download` option
            ->withoutDownload()
            // This will hide the `open in new tab` option
            ->withoutOpenInNewTab(),
    ];
}

覆盖

有时您的应用程序需要一些自定义逻辑。我们已尽力使覆盖默认行为尽可能简单。

您可以通过创建一个扩展 LinkPicker 字段的新字段来覆盖 LinkPicker 字段。这样,您只需覆盖您想要更改的方法。

namespace App\Nova\Fields;

use Outerweb\NovaLinkPicker\Nova\Fields\LinkPicker as BaseLinkPicker;

class LinkPicker extends BaseLinkPicker
{
    // Your custom code here
}

您可以通过创建一个扩展 Link 实体的新实体来覆盖 Link 实体。这样,您只需覆盖您想要更改的方法。

namespace App\Entities;

use Outerweb\NovaLinkPicker\Entities\Link as BaseLink;

class Link extends BaseLink
{
    // Your custom code here
}

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

鸣谢

许可协议

MIT 许可协议 (MIT)。有关更多信息,请参阅 许可文件