Problem overriding Kirbytag

To override the link Kirbytag, I wrote the following code to test the general setup:

$originalTag = Kirby\Text\KirbyTag::$types['link'];

Kirby::plugin('sanofan/link-kirbytag', [
    'tags' => [
        'link' => [
            'html' => function($tag) use ($originalTag) {
                $result = $originalTag['html']($tag);
                return $result;
            }
        ],
    ],
]);

But now, with a link such as (link: contact/ text: contact us) the returned HTML is NOT

<a href="http://localhost/.../contact/">contact us</a>

But instead

<a href="http://localhost/.../contact/ text: contact us"></a><a href="http://localhost/.../contact/">http://localhost/zoom-petro/contact/</a> text: contact us

That is, the output was duplicated, and the HTML of the second link is messed up.

I’m using Kirby version 3.6.1.1 with PHP version 8.0.13 on localhost.

Do you have any idea what went wrong here?

Cheers,

Stefan

That happens because the attributes are not defined:

<?php
$originalTag = Kirby\Text\KirbyTag::$types['link'];

Kirby::plugin('sanofan/link-kirbytag', [
    'tags' => [
        'link' => [
            'attr' => $originalTag['attr'],
            'html' => function($tag) use ($originalTag) {
                $result = $originalTag['html']($tag);
                return $result;
            }
        ],
    ],
]);
1 Like

Thanks a lot, that works perfectly!

So, if I use the same name as a built-in kirbytag in a plugin, such as link in this case, Kirby will use the plugin code exclusively, allowing me to use parts of the original kirbytag logic via Kirby\Text\KirbyTag::$types['link'].

Is this correct?

Yes, you can reuse the original, but it probably only makes sense in limited use cases. Usually, you will either want to add new attributes or change the html, then it’s probably better to copy the code from the original tag and make your mods where necessary.

1 Like

Thanks, that makes sense.