Kirbytags, adding aria-label to links

I would like to add aria-label to my links in the markdown editor using kirbytags.
I have created a plugin in folder site/plugins/tags and added an index.php

<?php

Kirby::plugin('kirby/tags', [
    'tags' => [

    /* Email */
    'email' => [
        'attr' => [
            'aria-label',
            'class',
            'rel',
            'target',
            'text',
            'title',
        ],
        'html' => function ($tag) {
            return Html::email($tag->value, $tag->text, [
                'aria-label'  => $tag->aria,
                'class'  => $tag->class,
                'rel'    => $tag->rel,
                'target' => $tag->target,
                'title' => $tag->title,
            ]);
        }
    ],

    /* Link */
    'link' => [
        'attr' => [
            'aria-label',
            'class',
            'lang',
            'rel',
            'role',
            'target',
            'title',
            'text',
        ],
        'html' => function ($tag) {
            if (empty($tag->lang) === false) {
                $tag->value = Url::to($tag->value, $tag->lang);
            }

            return Html::a($tag->value, $tag->text, [
                'aria-label'  => $tag->aria,
                'rel'    => $tag->rel,
                'class'  => $tag->class,
                'role'   => $tag->role,
                'title'  => $tag->title,
                'target' => $tag->target,
            ]);
        }
    ],

    /* Tel */
    'tel' => [
        'attr' => [
            'aria-label',
            'class',
            'rel',
            'text',
            'title'
        ],
        'html' => function ($tag) {
            return Html::tel($tag->value, $tag->text, [
                'aria-label' => $tag->aria,
                'class' => $tag->class,
                'rel'   => $tag->rel,
                'title' => $tag->title
            ]);
        }
    ],
    ]
]);

But when I use it as below the aria-label gets added to the email address.

(email: info@home.co.uk  aria-label: email home  text: info@home.co.uk)

It’s my first attempt at using kirbytags andd would appreciate some help sorting this out.

Here you are calling aria instead of aria-label, but $tag->aria-label would not be valid, because you cannot use a dash in function/property names. Use aria_label or arialabel for the attribute name.

1 Like