Can't manage to make table-field plugin to work. I need your help!

Hi everyone,

For a client website I really need to use the table-field plugin developed by ragi96.
However, even if I’m trying really hard, I always get an error.

I downloaded the plugin and put it in the site/plugins folder, named table-field as recomended.
As I need my client to have the possibility to add one or more tables, I put the tag table in a structure.
My blueprint looks like this:

title: Impianti

status:
  draft: true
  unlisted: false
  listed: true

columns:

  left:
    width: 2/3
    sections:

      content:
        type: fields

        fields:

          systems:
            label: Lista degli impianti
            type: structure
            columns:
              title:
                width: 3/4
              active:
                width: 1/4
            fields:
              title:
                label: Nome
                type: text
              systemDesc:
                label: Descrizione
                type: markdown
                size: medium

              systemSpec:
                label: Lista dei dati tecnici
                type: structure
                fields:
                  title:
                    label: Titoletto
                    type: text
                  techData:
                    label: Dati tecnici
                    type: markdown
                    size: medium
                  table:
                    label: Test tabella no. 1
                    type: table
                    options:
                      maxColumns: 3
              
              gallery:
                label: Galleria di immagini
                type: files
                multiple: true
                layout: cards
                image:
                  ratio: 1/1
                  cover: true
                uploads: image
              active:
                label: Impianto è attivo?
                type: toggle
                default: true

  right:
    width: 1/3
    sections:

      images:
        headline: Immagini e schemi
        type: files
        template: image

My template looks like this:

<?php snippet('header', ['submenu' => true, 'sections' => $page->systems()->toStructure()]) ?>

<!-- Sezione IMPIANTI
================================================ -->
<?php if($page->systems()->isNotEmpty()): ?>
    <?php foreach($page->systems()->toStructure() as $system): ?>

        <section>

            <div class="system">
                <span id="<?php echo $system->title() ?>" class="anchor"></span>

                <h2 class="h1"><?php echo $system->title()->html() ?></h2>
                <h2><?php echo $system->subName()->html() ?></h2>

                <?php echo $system->systemDesc()->kt() ?>

                <?php if($system->systemSpec()->isNotEmpty()): ?>
                    <?php foreach($system->systemSpec()->toStructure() as $techData): ?>
                        <strong><?php echo $techData->title()->html() ?></strong>

                        <?php $table = $techData->table()->toTable() ?>

                        <?php foreach($table as $tableRow): ?>
                            <?php foreach($tableRow as $tableCell): ?>
                                <td><?php echo $tableCell ?></td>
                            <?php endforeach ?>
                        <?php endforeach ?>
                        
                    <?php endforeach ?>
                <?php endif ?>

                <?php snippet('gallery', ['collection' => $system->gallery()->toFiles()->sortBy('sort', 'asc'), 'fancyGroup' => $system->title()->html()]) ?>

            </div>

        </section>

    <?php endforeach ?>
<?php endif ?>

<?php snippet('footer') ?>

Whatever I try to do I get the error
Invalid argument supplied for foreach()
and I really don’t understand why.

I’m also getting tired to try and I probably better stop and ask rather than keeping wasting time.

Thanks to anyone who can help me out!
Sara

What do you get if you dump $tableRow? It’s probably not an iterable structure.

Shit, I posted the wrong screenshot (it’s Friday).
Here attached.

I actually get an error at the beginning of the foreach.

If I dump the $table everything looks fine. :woman_shrugging:t2:

That array looks ok, but since you are can not rely on $table or $tableRow to be an array and you are using this inside another foreach loop, you should make sure you always have an array. So to be on the save side:

<?php if($page->systems()->isNotEmpty()): ?>
    <?php foreach($page->systems()->toStructure() as $system): ?>

        <section>

            <div class="system">
                <span id="<?php echo $system->title() ?>" class="anchor"></span>

                <h2 class="h1"><?php echo $system->title()->html() ?></h2>
                <h2><?php echo $system->subName()->html() ?></h2>

                <?php echo $system->systemDesc()->kt() ?>

                <?php if($system->systemSpec()->isNotEmpty()): ?>
                    <?php foreach($system->systemSpec()->toStructure() as $techData): ?>
                        <strong><?php echo $techData->title()->html() ?></strong>

                        <?php $table = $techData->table()->toTable() ?>
                        <?php if (is_array( $table ) : ?>
                        <?php foreach($table as $tableRow): ?>
                             <?php if (is_array( $tableRow ) : ?>
                                <?php foreach($tableRow as $tableCell): ?>
                                    <td><?php echo $tableCell ?></td>
                                 <?php endforeach ?>
                           <?php endif; ?>
                        <?php endforeach ?>
                        <?php endif; ?>
                        
                    <?php endforeach ?>
                <?php endif ?>

                <?php snippet('gallery', ['collection' => $system->gallery()->toFiles()->sortBy('sort', 'asc'), 'fancyGroup' => $system->title()->html()]) ?>

            </div>

        </section>

    <?php endforeach ?>
<?php endif ?>

This is the sort of problem you can more easily debug with a proper debugging tool like Xdebug.

While the first table that exists is dumped and everything looks great, the next non existing tables are not shown because you run into the error.