Get specific value of checkboxes options

Hello Kirby community,

I wanted to know if it possible to display something in front-end if a specific checkbox is selected?

  socialLinks:
label: Facebook
type: checkboxes
options: field
  facebook: Facebook
  instagram: Instagram
  twitter: Twitter
  pinterest: Pinterest
  pinterest: Snapchat

Exemple: if Facebook is selected in the panel I would like to display an icon in the front-end.

Thank you in advance.
Have a good day.

Alexis

Theres some guidance on working with checkboxes in the Cookbook. Basically, the checkbox field stores the value as a comma separated list, that you would need to split and loop over, which is explained at the bottom of the cookbook guide. If you set the checkbox value to your icon classes, it should work ok. that way when you loop through you can just echo the icon class into the link.

I am guessing you will also need a way to set the links easily. Personally I do this with url fields, rather then checkboxes, and use logic to see that the textfield has a value (if it doesnt, no icon shows up on the front end). I store this in the site options, and use a snippet like this to display them using fontawesome icons…

<ul class="social">
  <?php if(!$site->socialfaceBookurl()->empty()): ?><li><a title="Facebook Page" target="_blank" href="<?= $site->socialfaceBookurl()->html() ?>"><i class="fab fa-facebook"></i></a></li><?php endif ?>
...
</ul>

And the blueprint using Global Fields (just repeat for however many you need):

type: group
fields:
  socialmedia:
    label: Social Media Links
    type: headline
  socialfaceBookURL:
    label: Facebook Page
    type:  url
    width: 1/3
...

This way, its more portable and you can keep the links from being hardcoded into your templates/snippets, and they can be reused more easily (you can pull the same values into meta tags for example)

I use a simple field method to check if a checkbox is checked.

$kirby->set('field::method', 'contains', function ($field, $needle, $separator = ',') {
  return array_search($needle, str::split($field->value, $separator)) !== false;
});

if ($page->socialLinks()->contains('facebook')) {
  // do stuff
}

But ideally you would probably want to loop through all checked checkboxes and create the icons dynamically. Really depends on what you need to achieve.

foreach ($page->socialLinks()->split() as $key => $value) {
  // do stuff
}