I’m trying to follow the KirbyTags page: KirbyTags | Kirby
but am unclear on what all the elements do within the code provided. I tried to convert from an older Kirby2 custom kirbytag, but am unable to get the item to be called.
Expect to use via:
(coverbg: background.jpg)
<?php
Kirby::plugin('user/coverbg', [
'tags' => [
'coverbg' => [
'html' => function($tag) {
$input=$tag->value;
$html = '<div class="bg-fill" style="background: url(\'';
$html = $html.$tag->page()->image($input)->url();
$html = $html.'\'); background-size: cover; background-position:center;"></div>';
return $html;
}
]
]
]);
What am I missing?
bruno
February 10, 2021, 7:15pm
2
Maybe have a look at the source code of the (image: …)
Kirbytag: kirby/tags.php at 3.5.2 · getkirby/kirby · GitHub
Edit: Not sure but I think $tag->page()
should be $tag->parent()
Edit2: Ah, @texnixe also just wrote that :~)
texnixe
February 10, 2021, 7:19pm
3
Should be $tag->parent()
and you need an if statement here to make sure image()
returns an image before calling the url method.
I’ve made the suggested changes, but it doesn’t seem like the tag is being accessed at all.
<?php
Kirby::plugin('user/coverbg', [
'tags' => [
'coverbg' => [
'html' => function($tag) {
$input=$tag->value;
if(!is_null(image($input))){
$html = '<div class="bg-fill" style="background: url(\'';
$html = $html.$tag->parent()->image($input)->url();
$html = $html.'\'); background-size: cover; background-position:center;"></div>';
return $html;
}
else
return "No Image found.";
}
]
]
]);
On my page, it still shows up as
(coverbg: background.jpg)
I double checked the call and it shows up as:
<?php echo kirbytext($page->text()); ?>
texnixe
February 10, 2021, 9:25pm
5
Hm, I just tested it and it works, although I’d change the code a little:
'tags' => [
'coverbg' => [
'html' => function($tag) {
$input = $tag->value;
if( $image = $tag->parent()->image($input)){
$html = '<div class="bg-fill" style="background: url(\'';
$html .= $image->url();
$html .= '\'); background-size: cover; background-position:center;"></div>';
return $html;
}
else
return "No Image found.";
}
]
]
(and would probably use a snippet because I don’t like concatenated html, but that’s just my personal tick).
Thanks for putting that together. I copy-pasted it into the file, but unfortunately it’s still not working, as in it shows up as
(coverbg: background.jpg)
Are there any other troubleshooting steps that I could do to identify the issue?
texnixe
February 10, 2021, 10:33pm
7
Is the field name correct? What is the context of that line of code? What is your field type?
KirbyUser:
<?php echo kirbytext($page->text()); ?>
On a sidenote, the kirbytext()
helper is mainly intended for strings, with fields you might want to use the field method instead
<?php echo $page->text()->kt(); // or ->kirbytext() ?>
Text() is a Textarea field. The code is supposed to output the inserted text/markdown/kirbytext and display it onto the page.
It does indeed output all the text/headings/formatting correctly besides the (coverbg) that I’m trying to create a custom kirbytag for.
texnixe
February 10, 2021, 11:18pm
9
Since the code should be working, then it looks as if the plugin is not loaded at all.
Could you please put this code into a template (mayby home.php) and check if your plugin shows up in the array:
<?php dump($kirby->plugins())?>
Correct, no plugins are showing up here.The file location currents sits in site/plugins/tags/work-bg/
texnixe
February 10, 2021, 11:24pm
11
Ok, that won’t work.
Plugins need a folder in /site/plugins
, e.g. /site/plugins/tags
and in that folder needs to be an index.php, with your code from above in that index.php. No nested folders, unless of course they contain stuff that is registered in your index.php
https://github1s.com/getkirby/getkirby.com/blob/HEAD/site/plugins/keycdn/index.php
Awesome, that fixed my problem!
It would be helpful if there was additional clarification in the docs for it.