Skip to content

[6.x] Add support for custom link types#15004

Open
duncanmcclean wants to merge 12 commits into
6.xfrom
link-types
Open

[6.x] Add support for custom link types#15004
duncanmcclean wants to merge 12 commits into
6.xfrom
link-types

Conversation

@duncanmcclean

@duncanmcclean duncanmcclean commented Jul 14, 2026

Copy link
Copy Markdown
Member

This pull request implements support for registering custom link types on the Link fieldtype, and unifies Bard's link button to use the same system.

Previously, the Link fieldtype only supported linking to a URL, Entry, Asset, or (in a collection with a structure) the first child page. There was no way for addons, like Runway and Forms Pro to add their own link types.

Addons can now register a link type from their service provider:

use Statamic\Fieldtypes\Link;

Link::extend('form', \App\FormLinkType::class);

A LinkType controls how the type is displayed, how a stored value resolves back to a real link, and what fieldtype powers its picker:

use Statamic\Facades\Form;
use Statamic\Fields\Field;
use Statamic\Fieldtypes\Link\LinkType;

class FormLinkType extends LinkType
{
    public function resolve(string $id, $parent = null, bool $localize = false): mixed
    {
        $form = Form::find($id);

        if (! $form) {
            return;
        }

        return "/forms/{$form->handle()}";
    }

    public function fieldtype(Field $field): ?array
    {
        return ['type' => 'form', 'max_items' => 1];
    }
}

Once registered, the Link fieldtype will show the link type in the dropdown and render a picker using the provided fieldtype.

This PR also migrates the native Entry and Asset options onto the same LinkType system, so there's a single code path for native and third-party types alike.

Bard

Bard's link button previously had its own, entirely separate implementation for resolving entry::/asset:: references (via statamic:// URIs and the Data repository) and its own hardcoded Entry/Asset picker.

This PR unifies it onto the same Link::extend() registry — any custom link type registered for the Link fieldtype is now automatically available in Bard's link button too, with no extra wiring needed.


Closes statamic/ideas#1181
Docs: statamic/docs#1949

…eldtype

Previously every registered link type in Bard was routed through a
hardcoded relationship input (or a bespoke asset selector), so custom
link types with a non-relationship picker never actually worked in
Bard. Now Bard dynamically renders each type's own fieldtype component,
same as the Link fieldtype already does.

Guard against CollectionNotFoundException since linkTypesForToolbar
now eagerly preloads every visible type on every Bard field render,
which would otherwise crash on installs with no collections yet.
Link::extend() appends config fields onto Fieldtype's shared static,
which was leaking into later tests.
Matches the Link fieldtype's behavior of opening the selector
immediately when switching to a type with nothing selected yet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ability to add additional "link types" to the Link Fieldtype / Bard Link Toolbar

1 participant