Checkboxes vs. toStructure

Hi,

I am having an issue with checkboxes contained by a structure and the new toStructure() function. My blueprint looks like this:

    fields:
        calendar:
            label: Ereignisse
            type: structure
            fields:
                weekdays:
                    label: Wochentage
                    type: checkboxes
                    columns: 4
                    options:
                        Mo: Montag
                        Di: Dienstag
                        Mi: Mittwoch
                        Do: Donnerstag
                        Fr: Freitag
                        Sa: Samstag
                        So: Sonntag

Using yaml(), my structure looks like this:

print_r($calendar->yaml())
....
[2] => Array
    (
        [summary] => Training 1a & 1b
        [description] => Training der 1. und 2. Mannschaft
        [weekdays[]] => Array
            (
                [0] => Di
                [1] => Do
            )
...

The issue is that if I apply toStructure() on $calendar e.g. to filter like this:

foreach ($calendar->toStructure()->filter(function($entry) {....}) as $event):
    echo $event->weekdays();

the weekdays property is empty. $event->weekdays would not be not syntactically valid.

The indentation seems to be wrong… “options” must be inside weekdays… & not in the same level.

fields:
   weekdays:
     label: Wochentage
     type: checkboxes
     columns: 4
     options:
         Mo: Montag
         Di: Dienstag
         Mi: Mittwoch
         Do: Donnerstag
         Fr: Freitag
         Sa: Samstag
         So: Sonntag

The indentation got messed up as I shorted the post. The blueprint is fine. I edited my post to reflect that.

You can use get() instead:

<?php $calendar = $page->calendar()->toStructure();
    foreach($calendar as $event) {
      foreach($event->get('weekdays[]') as $day) { 
        echo "<p>" . $day . "</p>";
      }
    }

 ?>

Still looks strange …

@texnixe that works, thanks. I can live with that for the moment.

How would you go about parsing the same structure field to populate an li with the name of each selected checkbox field?

1 Like

What do you mean by “with the name of each selected checkbox field”? Do you want to get “Monday” instead of Mo?

No, I tried using this on the site I am working on, but instead of getting the field name I get the count of selected checkboxes on my output.

<?php foreach($page->product()->toStructure() as $product): ?>
  <h2><?php echo $product->product_name(); ?></h2></a>
<p><?php echo $product->product_desc(); ?></p><a href="#" class="product--image">
<img src="<?php echo $page->image($product->product_image())->url() ?>"/></a>
<ul class="product--features--list">
  <?php foreach($product->get('features[]') as $feature) ?>
    <li><?php echo ($feature); ?></li>
<?php endforeach; ?>

And my output if I select two checboxes is feature_2 or if I select 4 is feature_4 instead of the name of the feature

Could you pls. post your blueprint as well @shiftsave?

Yes of course @texnixe here it is:

pages: true
files: true
fields:
  title:
    label: Title
    type:  text
  product:
    label: Products
    type: structure
    fields:
      product_name:
        label: Name
        type: text
      product_desc:
        label: Description
        type: textarea
      product_image:
        label: Image
        type: select
        options: images
      features:
        label: Features
        type: checkboxes
        columns: 2
        options:
          feature_1: Feature 1
          feature_2: Feature 2
          feature_3: Feature 3
          feature_4: Feature 4

Thanks!

I was to blind to see it before, but there are three syntax errors in your code, a missing colon and a missing endforeach and a missing closing ul tag:

<?php foreach($page->product()->toStructure() as $product): ?>
  <h2><?php echo $product->product_name(); ?></h2></a>
  <p><?php echo $product->product_desc(); ?></p><a href="#" class="product--image">
  <img src="<?php echo $page->image($product->product_image())->url() ?>"/></a>
  <ul class="product--features--list">
    <?php foreach($product->get('features[]') as $feature) : ?>
      <li><?php echo ($feature); ?></li>
   <?php endforeach; ?>
</ul>
<?php endforach ?>

I would suggest to turn on debugging in your config.php:

c::set('debug', true);

Thanks! I’ll try this and let you know how t goes.

Again, thanks for all your help!