How to add a random number to a link class with a plugin?

I’m trying to get KirbyText links to work so that each link gets a random number added to its class name. So that if there’s a text file with the text ”(mylink: # text: click here class: test)”, the resulting html link would include

class=“test99”

or a similar result. I’ve made a plugin based on the KirbyText link, which has this section:

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

I’ve added this in the beginning of the file:

$mynumber = rand(1, 100);

But now I can’t figure the right way to add that random number to the class. Other than that the plugin functions as it should, as it is just a copy of the original, but as soon as I try to add $mynumber to the line

‘class’ => $tag->class,

it doesn’t work anymore. I’ve tried different ways, but with no success so far.

Which would be the correct way?

You can just concat the two together.

'class’ => $tag->class.$mynumber,.

Be aware though, that nothing is truly random, and there are freak occurances where you may end up with the same class name on two elements. I would atleast raise the number to 1000. If it was me, i would uses the title rather than a number, converted to lowercase and spaces replaced with a dash. It means the CSS is more readable. if you add the page name + the link title together, then in the css it is obvous what page that link is one.

Thanks for your help!

For some reason however, that doesn’t work. When I add the variable like that, the link isn’t recognized as a link anymore, the html page shows everything (the link, the text, etc.) as plain text.

whoops… try putting spaces around the dot.

'class’ => $tag->class . $mynumber, .

I actually tried that already, it doesn’t work either.

Does the Html::a thing work in some special way…?

Can you please post the entire tag you are using?

The problem might be that you are trying to add an integer to a string. Try casting the number to string and then concatenate.

Thank you both for your help. I still can’t get it to work… The whole thing is here:

<?php

$mynumber = rand(1, 100);
$myword = 'hello';

Kirby::plugin('ssn/randomlinkclass', [
	'tags' => [
		'randomlinkclass' => [
			'attr' => [
				'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, [
				'class'  => $tag->class . 'testing',
                'rel'    => $tag->rel,
                'role'   => $tag->role,
                'title'  => $tag->title,
                'target' => $tag->target,
            ]);
        }
	]
]
]);

It works now with the hardcoded word “testing”, but if I try to use either of the variables from the beginning there, either the link shows as plain text or nothing shows up.

Try this…

<?php

Kirby::plugin('ssn/randomlinkclass', [
	'tags' => [
		'randomlinkclass' => [
			'attr' => [
				'class',
				'lang',
				'rel',
				'role',
				'target',
				'title',
				'text',
				],
        'html' => function ($tag) {

            $mynumber = rand(1, 100);
            $myword = 'hello';
        
            if (empty($tag->lang) === false) {
                $tag->value = Url::to($tag->value, $tag->lang);
            }

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

Oh, now it’s working! Many thanks for your help!