Overwriting the Kirbytext link tag

I’m wondering if its possible to overwrite the Kirbytext link tag so that it doesn’t include the Base URL for links like /link. I’d like to use the generated links within a nuxt frontend hosted under a different URL.

Could this be done? Any pointers in the right direction would be appreciated.

Yes, that is possible in a plugin. You can either completely redefine it (using the same name), or reuse parts of it: How to properly overwrite a kirby tag? - #2 by manuelmoreale

For some reason Kirby doesn’t pick up the textproperty

Kirby::plugin('studioscholz/simple-link', [
    'tags' => [
        'link' => [
            'html' => function ($tag) {

                // if (empty($tag->lang) === false) {
                //     // $tag->value = Url::to($tag->value, $tag->lang);
                // }
                return Html::tag('a', $tag->text, [
                    'rel'    => $tag->rel,
                    'class'  => $tag->class,
                    'role'   => $tag->role,
                    'title'  => $tag->title,
                    'target' => $tag->target,
                    'href' => $tag->link,
                ]);
            }
        ]
    ]
]);

Anything I am missing here?

EDIT: Solved by defining the available tags first:

Kirby::plugin('studioscholz/simple-link', [
    'tags' => [
        'link' => [
            'attr' => [
                'rel',
                'class',
                'role',
                'title',
                'target',
                'href',
                'text',
            ],
            'html' => function ($tag) {

                // if (empty($tag->lang) === false) {
                //     // $tag->value = Url::to($tag->value, $tag->lang);
                // }
                return Html::tag('a', $tag->text, [
                    'rel'    => $tag->rel,
                    'class'  => $tag->class,
                    'role'   => $tag->role,
                    'title'  => $tag->title,
                    'target' => $tag->target,
                    'href' => $tag->link,
                ]);
            }
        ]
    ]
]);

the core link kirbytag is very clever with parsing the link. your current implementation only takes fully qualified urls.
consider at least using url($tag->link) to make internal urls easier to enter again.

$field->link() is just a shortcut. The full method name that you would need to overwrite is $field->toLink()

I’m trying to avoid internal links, so I ca write relative links for my nuxt frontend :grin:

1 Like