Check for Page Field in Plugin?

I’m making a huge mistake and trying to dip my toe into plugins to do something globally.

Is there a way to check to see if a field exists on a page from a plugin?

I tried the following:

<?php

    if ( $page->primaryColor() ): {
        echo "The primaryColor field exists on this page.";
    } else {
        echo "Nope.";
    }

?>

My “plugin” is loading / working fine (I started with a simple hello world message and it worked).

That is the sort of code you would use in a template or snippet. Adding that into the index.php of your plugin doesn’t make sense as the current page is not known to the plugin.

Could you try and tell us what you are trying to achieve with this plugin?

Well I created a snippet with a simple if / else if / else statement that checked for the presence of a field, but the snippet didn’t seem to work?

If I put the exact if / then / else statement inline in an individual template, it worked fine, but for some reason if I included it as a snippet like I’m doing with various other elements on my site it failed.

Simply put, I’m trying to accomplish this: “If a field is present on the page you’re currently viewing, set a variable to the value in that field. If that field is not present, use this default value instead.”

Then I’m echoing that variable in certain areas of the page.

There is an easier way of achieving this.

<?= $page->primaryColor()->or('blue'); ?>

If the field is empty, it falls back to whatever you pass to the or() field method. Instead of a hardcoded string, you can also fall back to another field:

<?= $page->primaryColor()->or($site->defaultColor()); ?>

You can also store this in a variable, for example in your controller:

$color = $page->primaryColor()->or($site->defaultColor());

If you think the code is too long to be used as is, you can create a custom page method in a plugin or a page model…

1 Like

Are you kidding me? The syntax of Kirby is delightful. Thank you!

1 Like

You will get another chance to create a plugin, I’m sure, so don’t be sad :hugs:

1 Like

So - I’m having the same issue I had earlier. That code you gave me definitely works:

<?php

    $setColor = $page->primaryColor()->or($site->defaultColor());
    echo $setColor;

?>

It’s cleanly printing out the setColor variable like a champ. But when I try to echo that setColor variable somewhere else, it blows up. Is this an issue with the way I’m trying to use PHP in general, or am I doing something Kirby doesn’t like?

<?php echo $setColor ?>

:point_up: - Doesn’t work.

But where are you trying to echo that? Of course you can’t just echo a variable anywhere but only where it is defined. Could you please post the complete context?

That’s what is confusing me - I created a snippet; color-check.php in my snippets folder.

Then, immediately following my tag, I’m loading it in:

<?php snippet('color-check') ?>

I then try the echo business further down the page and it explodes.

… I think I just realized what I need to do. Just include that entire snippet everywhere I want the output. :expressionless:

And what is in that snippet? The two lines of code from above? I’m missing the complete context.

You are calling the snippet in a template, right?

Yeah the Snippet contains:

<?php

    $setColor = $page->primaryColor()->or($site->defaultColor());
    echo $setColor;

?>

I have it working now. I’m an idiot and was implementing an extra step that didn’t need to exist.

In any case there is no need to define the variable just to echo it immediately. afterwards. And I have my doubt regarding the usefulness of putting this bit of code into a snippet at all, anyway.

I’m sure you’ll find this appalling, but I love having a cleaner appearance in my template. So using a snippet is just nicer to look at. Let alone the fact that I’m referencing it in a million places. :smiley:

But then I’d define the variable in a. controller and just echo it in the template.

I guess I’m not sure I understand the difference.

However, if you really need it all over the place, maybe not.

A controller contains the logic for a page type:

https://getkirby.com/docs/guide/templates/controllers

1 Like